应用程序从webapp tomcat7

时间:2016-01-07 11:25:13

标签: java file tomcat7 printwriter

当我在tomcat7 / webapp / myapp.war中部署应用程序并通过Web浏览器运行此应用程序时,我找不到输出文件。

我的应用程序库存了一些结果如下文件:

try {
            PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("/home/user/Desktop/myfile.txt", true)));
            out.println("some text");
            out.close();
        } catch (IOException e) {
            //exception handling left as an exercise for the reader
        }

输出文件在哪里?

2 个答案:

答案 0 :(得分:1)

它将在webapps目录下创建,即myapp / home / user / Desktop / myfile.txt

答案 1 :(得分:0)

如果你想在桌面上使用它,你需要在家门前放一个正斜杠(/)

PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("/home/user/Desktop/myfile.txt", true)));

更新 - 尝试

PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("myfile.txt", true)));

然后检查... / tomcat / bin /目录并查看一下?

更新

确定试试这个并检查您的桌面。我自己尝试过,它对我有用。

try {
        File file = new File("/home/user/Desktop/myfile.txt");
      // creates the file
      file.createNewFile();
      // creates a FileWriter Object
      FileWriter writer = new FileWriter(file); 
      // Writes the content to the file
      writer.write("This\n is\n an\n example\n"); 
      writer.flush();
      writer.close();    
        } catch (IOException e) {
            //exception handling left as an exercise for the reader
        }