I really not getting where the problem is.
I wanted to print the characters to a text file and im uisng printWriter to do the same.
if the file has a “;”, i want to replace it with a new line and this is what im doing,
public static void downloadFile_txt(String sourceFilePathName, String contentType, String destFileName, HttpServletResponse response) throws IOException, ServletException
{
File file = new File(sourceFilePathName);
//FileInputStream fileIn = new FileInputStream(file);
FileReader fileIn = new FileReader(file);
long fileLen = file.length();
response.setContentType(contentType);
response.setContentLength((int)fileLen);
response.setHeader("Content-Disposition", String.valueOf((new StringBuffer("attachment;")).append("filename=").append(destFileName)));
PrintWriter pw = response.getWriter();
// Loop to read and write bytes.
int c=-1;
while ((c = fileIn.read()) != -1)
{
if(c!=59)
{
pw.print((char)c);
}
else
{
pw.println();
}
}
pw.flush();
pw=null;
fileIn.close();
}
But my file is priting everything except for the last character.
Eg.input =
:00004000,FFAD,2 Byte Ch;
:0000FFBD,FFBE,2 Byte Ch;
:0000FFBF,FFFF,2 Byte Ch;
output which im getting
:00004000,FFAD,2 Byte Ch
:0000FFBD,FFBE,2 Byte Ch
:0000FFBF,FFFF,2 Byte C
the last “h” is not getting printed.
Thanks in advance
A
pw.flush();might help you.outputs
as expected.
(Don’t handle your
IOExceptions like this – and close your readers and writers – this is for demonstration only!)edit: now your code doesn’t even compile (two vars called
fileIn?)!Even when run through the servlet code you’re now mentioning, I can’t reproduce your problem, and the output is as you would expect. So this is me giving up. I’m starting to suspect either the final
;isn’t in your source file, or there is yet more processing your app is doing that you’re not showing us.