Mehdi
Mehdi

Reputation: 2228

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.

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 ?

Upvotes: 1

Views: 89

Answers (2)

blairmeister
blairmeister

Reputation: 915

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?

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
        }

Upvotes: 0

M S Parmar
M S Parmar

Reputation: 974

It will created under webapps directory i.e. myapp/home/user/Desktop/myfile.txt

Upvotes: 1

Related Questions