Reputation: 5933
I am working in a servlet and has this code :
public void doPost(blah blah){
response.setContentType("text/html");
String datasent = request.getParameter("dataSent");
System.out.println(datasent);
try{
FileWriter writer = new FileWriter("C:/xyz.txt");
writer.write("hello");
System.out.println("I wrote");
}catch(Exception ex){
ex.printStackTrace();
}
response.getWriter().write("I am from server");
}
But everytime it is throwing an error saying Access Denied.. Even when there is no lock on that file and there is no file whose name is C:/xyz.txt
what should I do? ;(
java.io.FileNotFoundException: C:\xyz.txt (Access is denied)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(FileOutputStream.java:212)
at java.io.FileOutputStream.<init>(FileOutputStream.java:104)
at java.io.FileWriter.<init>(FileWriter.java:63)
at test.TestServlet.doPost(TestServlet.java:49)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:306)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:108)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:558)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:379)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:243)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:259)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:237)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:281)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)
Upvotes: 1
Views: 8211
Reputation: 2702
Try close FileWriter:
writer.close();
I imagine what happens: One thread in Tomcat creates the file and terminates but not closes its handle, so from file system view file lock is not released. Another thread tries to open it for write but OS still cannot grant write access to the file, because it already has one pending.
This is called resource leak: Garbage Collector does not release resources allocated by programmer manually (here IO handle)
Upvotes: 5
Reputation: 85
try looking at this site seems like thats what your looking for (scroll down some ways)
Upvotes: 0
Reputation: 2524
The exception shows that it is of FileNotFound Exception. Please try to make new file first. Try with following code.
File file = File file("c:/xyz.txt");
if(!file.exists()){ // this will return boolean {true} if file exists.
file.createNewFile(); // create new empty file.
}
FileWriter writer = new FileWriter("C:/xyz.txt", true);
BufferedWriter out = new BufferedWriter(writer);
out.write("your text");
out.close();
Ok no issues.. try to figure out it using different Readers and Writers.
String currentExecutablePath = System.getProperty("user.dir");
String rootPath = currentExecutablePath + "xyz.txt";
File file = new File(rootPath);
ServletOutputStream op = res.getOutputStream();
if(file.exists()){
int length = 0;
res.setContentType("application/octet-stream");
res.setContentLength((int) file.length());
byte[] bbuf = new byte[1000];
DataInputStream in = new DataInputStream(new FileInputStream(file));
while ((in != null) && ((length = in.read(bbuf)) != -1)) {
op.write(bbuf, 0, length);
}
}
Upvotes: -1
Reputation: 9935
During this process, don't open the file.Try as below :
FileWriter writer = new FileWriter("C:/xyz.txt", true);
BufferedWriter out = new BufferedWriter(writer);
out.write("your text");
out.close();
Upvotes: -1