有时候需要将一段内容输出到txt文件,在程序中将内容字符串拼接好,包括 \t 以及 \n 等符号,然后利用Java.io中的writer将字符串写到txt文件中,本以为很简单的一件事,结果打开txt文件一看,格式完全是乱的,txt根本就没有识别我在程序中写的 \n 符号,所以没办法,只能变通一下:
利用BufferedReader的readLine()方法进行换行读入,然后逐行write
OutputStream s = resp.getOutputStream();
OutputStreamWriter writer = new OutputStreamWriter(os, "GBK");
PrintWriter ut = new PrintWriter(writer);
String result = (String)AjaxDispatcher.execute(params);
InputStream is = new ByteArrayInputStream(result.getBytes());
InputStreamReader isr = new InputStreamReader(is);
BufferedReader reader = new BufferedReader(isr);
String str = "";
while((str = reader.readLine()) != null){
out.println(str);
}
out.flush();
...
...
相关阅读: