Java通过Lua执行创建临时文件

时间:2016-10-16 17:35:00

标签: java lua permissions temp teamspeak

我在使用java通过lua创建临时文件时遇到问题。 如果我通过命令行启动jav程序,但是如果程序是由Lua-plugin启动的,则代码可以工作。它不会创建文件。

命令行> java Bot!info

  • 如果文件已经存在,将在Temp目录中创建BotAnswer.txt,它将被覆盖
  • 该文件包含正确的数据

通过Lua执行

  • 错误:Java程序将启动,但不会创建BotAnswer.txt ...如果文件已经存在则没有任何反应

  • 文件丢失或包含错误的数据

  • 如果文件已经存在,则会将旧的和错误的内容发送到聊天

我猜有一些许可错误或类似的东西。

如果你能告诉我如何解决这个问题,对我来说将是一个巨大的帮助。

以下是代码段:

located in C:\Program Files\TeamSpeak 3 Client\plugins\lua_plugin\testmodule
  

Lua

if targetMode == 2 then --targetMode is always 2 for this case
            os.execute("java Bot "  .. message) --Start java program with message as arguments (message = !info)
            if message == "!info" then
                folderName = os.getenv("TEMP")
                fileName = "BotAnswer.txt"
                filePath = io.open(folderName .. "/" .. fileName, "r")
                answer = filePath:read("*all")
                filePath:close()
                os.remove(folderName .. "/" .. fileName)
                ts3.requestSendChannelTextMsg(serverConnectionHandlerID, answer, fromID) --Send the content of BotAnswer.txt to the teamspeak Chat
            end
        end
  

爪哇

public class Bot {

    public static void main(String[] args) {
        Bot myBot = new Bot();
        String command = myBot.getCommand(args);
        String answer = myBot.differentiateCommand(command);
        try {
            myProcessor.writeAnswerToFile(answer);          
        } catch (Exception e) {}
    }

    public String getCommand(String[] args) {
        if(args.length == 0) {
            System.exit(0);
        } 
        if (args[0].startsWith("!") != true) {
            System.exit(0);
        }
        String message = args[0];
        if (message.startsWith("!")) {
            String[] msgArray = message.split("!");
            message = msgArray[1];
        }
        return  message;
    }

    public String differentiateCommand(String command) {
        String answer = "";
        if (command.startsWith("info")) {
            answer = "Tis should be the TeamSpeak answer";
        }
    } 

    public void writeAnswerToFile(String answer)throws IOException {
        String tempDir = System.getenv("TEMP");
        File tempFile = new File(tempDir + "/" + "BotAnswer.txt");
        tempFile.createNewFile();
        FileWriter writer = new FileWriter(tempFile);
        writer.write(answer); 
        writer.flush();
        writer.close();
    }

}

0 个答案:

没有答案
相关问题