1

I'm having real trouble generating a text file per 'operation' to store operation specific information, no matter what approach I take, I can't create a text file on the web server, let alone store information within it, the first example is ideally what I'd like (create and store per operation)

Note - I'm trying to do this in a servlet currently for testing purposes. Help much appreciated.

 try {
               FileWriter fstream = new FileWriter("out.txt");
                BufferedWriter out = new BufferedWriter(fstream);
                out.write("test");
                out.close();
            } catch (Exception ex) {
                 System.out.println(ex);
            }

Different Approach

 try {

            URL                url; 
            URLConnection      urlConn; 
            DataOutputStream   dos;
            DataInputStream dis;

            url = new URL("http://localhost:8080/" + request.getContextPath() + "/tmp/myfile.txt");
            System.out.println(url);
            urlConn = url.openConnection(); 
            urlConn.setDoInput(true); 
            urlConn.setDoOutput(true); 
            urlConn.setUseCaches(false); 
            urlConn.setRequestProperty ("Content-Type", "text/plain");

            dos = new DataOutputStream (urlConn.getOutputStream()); 
            dos.writeUTF("test");
            dos.flush(); 
            dos.close();

            //to test
            dis = new DataInputStream(urlConn.getInputStream()); 
            String s = dis.readLine(); 
            System.out.println(s);
            dis.close(); 

     } catch(Exception ex) {
         System.out.println(ex);
     }
2
  • Does your have permission to write the file to the filesystem? Commented Mar 12, 2012 at 21:50
  • 1
    I'm developing locally on Windows at the moment, I'll try CHMOD some folders now, thanks! Commented Mar 12, 2012 at 21:52

1 Answer 1

1

Not sure why you'd want to do this like this; there's already robust logging solutions, and databases.

In the first case, it's almost certainly creating a file if you're not getting exceptions, you just don't know where it is. Use either an absolute path to a known-accessible location, or something relative to the application itself using .getRealPath.

(Noting that trying to write to an app-relative path won't work if you're deploying a war.)

In the second case, not sure why you thought that would work.

Sign up to request clarification or add additional context in comments.

2 Comments

I've managed generate/locate the filepath using .getRealPath. I'm already using a database to log data, I just need to use a text file too. No doubt I'll encounter permission issues when on a server, but until then this will have to do, thanks!
@Ash No problem; glad you got it working. Still seems a bit weird to me, though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.