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);
}
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.