• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

No output to file

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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;
}
}
}
}
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
It's hard to fight evil. The little things, like a nice sandwich, really helps. Right tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic