• Post Reply Bookmark Topic Watch Topic
  • New Topic

No output to file  RSS feed

 
shivashankar matam
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can any one tell why there is output written to file
This code receive the file path and display its contents on jsp. but nothing is displaying. when i try to write to a file i found empty file. please let me know whats the error is :


import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class DisplayLog extends HttpServlet{

private String fileOutput;
HttpSession sessionid;

public void service(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException
{

response.setContentType("text/html");
// PrintWriter out = response.getWriter();
sessionid=request.getSession();
FileWriter first = new FileWriter(new File("first1.txt"));

String path=request.getParameter("file");

first.write("The path:"+path);

Scanner scan = null;
StringBuffer buffer;
first.write("\nThe file output is");
first.close();
try {
//String p1= "c:/example/log1.txt";
buffer = new StringBuffer();
//scan = new Scanner(new File(p1), "UTF-8");
scan = new Scanner(new File(path));
String readdata = "";
while (scan.hasNext() && (readdata = scan.nextLine()) != null) {
buffer.append(readdata).append('\n');
}
fileOutput = buffer.toString();

first.write(fileOutput);
first.close();

request.setAttribute("filecontent", fileOutput);

} catch (Exception e) {
e.printStackTrace();
} finally {
//
RequestDispatcher rd = request.getRequestDispatcher("logcontent.jsp");
rd.forward(request, response);

if(scan != null) {
scan.close();
scan = null;
}
}
}
}
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In web applications you need to use absolute paths. Relative paths (like "first1.txt", and -maybe- the value of "path") won't work.

In the future, please UseCodeTags when posting code of any length. It's unnecessarily hard to read as it is, making it less likely that people will do so.
 
shivashankar matam
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI,

Thanks for you reply
the code line first.write("The path:"+path); this will output in to file first1.txt
but when i write the contents of file using first.write(fileOutput); the file dirst1.txt show empty


Thanks & Regards
shiva
 
Ankit Garg
Sheriff
Posts: 9608
36
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


shivashankar matam wrote:the code line first.write("The path:"+path); this will output in to file first1.txt
but when i write the contents of file using first.write(fileOutput); the file dirst1.txt show empty


The text in bold completely confused me. I think you are trying to say first1.txt. Well when you call first.write(fileOutput);, at that time you've already closed the file writer at line 33 in the code. Also as Ulf said, you are using a path from the request parameter to read a file. Can you tell us the path that you pass as the parameter to this servlet...
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!