简体   繁体   English

应用程序从webapp tomcat7中写入文件

[英]Application write in file from webapp tomcat7

Please, when i deploy an application in tomcat7/webapp/myapp.war and run this application through web browser, i don't find the output file. 请,当我在tomcat7 / webapp / myapp.war中部署应用程序并通过Web浏览器运行该应用程序时,我找不到输出文件。

My application stock some result in file like this: 我的应用程序在文件中存储了一些结果,如下所示:

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
        }

Where is the output file ? 输出文件在哪里?

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

If you want it on your desktop you need to put a forward slash (/) in front of home like so 如果要在桌面上使用它,则需要在家门前加一个斜杠(/),如下所示

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

Update - Try 更新-尝试

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

then check .../tomcat/bin/ directory and have a look? 然后检查... / tomcat / bin /目录,看看吗?

Update 更新

Ok Try this and check your Desktop. 确定尝试此操作并检查您的桌面。 Just tried it myself and it works for me. 我自己尝试过,对我有用。

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
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM