我真的不明白问题出在哪里。我想将字符打印到文本文件中,并且我想使用 printWriter 来做同样的事情。如果文件有“;”,我想用新行替换它,这就是我正在做的事情,
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();
}
但是我的文件正在打印除最后一个字符之外的所有内容。例如输入=
:00004000,FFAD,2 Byte Ch;
:0000FFBD,FFBE,2 Byte Ch;
:0000FFBF,FFFF,2 Byte Ch;
我得到的输出
:00004000,FFAD,2 Byte Ch
:0000FFBD,FFBE,2 Byte Ch
:0000FFBF,FFFF,2 Byte C
最后一个“h”没有被打印出来。
提前致谢