Java 写文件实现换行
第一种:
写入的内容中利用\r\n进行换行
File file = new File("D:/text");
try {
if(!file.exists())
file.createNewFile();
FileOutputStream out=new FileOutputStream(file,false);
StringBuffer sb=new StringBuffer();
sb.append("10600257100120161201153103010 \r\n");
sb.append("120161201KBS571009886631浙江目录上传120161201094425210009302359591120110422KBS00005595530ZZA571ZZA20161201094435fanzhipeng2000\n");
out.write(sb.toString().getBytes("utf-8"));//注意需要转换对应的字符集
out.flush();
out.close();
/*
FileOutputStream out = new FileOutputStream(file);
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(writerStream, "UTF-8"));
writer.write(json);
writer.close();
*/
} catch (IOException e) {
e.printStackTrace();
}
第二种:
利用BufferedWriter的newline()方法
File file = new File("D:/text");
try {
if(!file.exists())
file.createNewFile();
FileWriter out=new FileWriter (file);
BufferedWriter bw= new BufferedWriter(out);
bw.write("10600257100120161201153103010 ");
bw.newLine();
bw.write("120161201KBS571009886631浙江目录上传120161201094425210009302359591120110422KBS00005595530ZZA571ZZA20161201094435fanzhipeng2000");
bw.newLine();
bw.flush();
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
但是newLine在使用中可能会出现问题:
不同系统的换行符:
windows --> \r\n
Linux --> \r
mac --> \n
我们一般开发是在 windows 下开发,而服务器一般情况下都是 linux。
如果我们使用 newline 函数换行,在本机测试的时候,因为是 windows 环境,换行符是 \r\n ,打开文件时候自然文件是换行处理,没有问题。
当我们部署到服务器时候,服务器是 linux 环境,newline 读取系统换行符是 \r ,导出到文件,文件的换行符是 \r,当我们把这个文件通过浏览器下载到 windows 时候,再打开文件将会出现没有换行的问题。因为 windows 下对于 \r 的解释并不是换行符。
所以,我们在开发时候,如果需要指定文件在某些地方换行,则不能使用 newline 方法。必须手动指定换行符:\r\n 因为按照上面列举的不同系统换行符看,如果字符串的末尾是 \r\n 在三个系统中,查看该文件,都会解释为换行。
简单整理!!
Java 写文件实现换行的更多相关文章
- java写文件实现换行
Java 写文件实现换行 第一种: 写入的内容中利用\r\n进行换行 File file = new File("D:/text"); try { if(!file.exist ...
- python写文件无法换行的问题
python写文件无法换行的问题,用'\n' 不行,直接打印的出来了. 网上查了查,都说是用 ‘\r\n’ ,但是这样打出来,不仅换行了,还加了一个空行. windows平台最后结果是 直接 ...
- java写文件时,输出不完整的原因以及解决方法
在java的IO体系中,写文件通常会用到下面语句 BufferedWriter bo=new BufferedWriter(new FileWriter("sql语句.txt")) ...
- java 写文件解析
import java.io.File; import java.io.FileOutputStream; import java.io.*; public class FileTest { publ ...
- java写文件时,输出不完整的原因以及解决方法close()或flush()
在java的IO体系中,写文件通常会用到下面语句 BufferedWriter bw=new BufferedWriter(new FileWriter("sql语句.txt")) ...
- java写文件时往末尾追加文件(而不是覆盖原文件),的两种方法总结
代码如下: import java.io.FileWriter; import java.io.IOException; import java.io.RandomAccessFile; public ...
- java写文件
randomAccessFile.close(); } e.printStack ...
- java写文件的基本操作
import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOExce ...
- java写文件读写操作(IO流,字符流)
package copyfile; import java.io.*; public class copy { public static void main(String[] args) throw ...
随机推荐
- GitHub Permission to <<repository>> denied to <<username>>
I kept receiving a 403 error saying my usual username couldn’t access the repository with my usual a ...
- c#基础 第三讲
Random r = new Random(); string x, y; while (true) { ...
- 【IIS】IIS 7.0/7.5 无法启动 w3svc 服务
一般情况下,window IIS安装完毕后,会启动C:\inetpub\ 产生 类似C:\inetpub\temp\apppools的文件夹,如果IIS被改动过,此文件夹不会自动生成.需要手动添加. ...
- Mybatis 3.1中 Mapper XML 文件 的学习详解(转载)
MyBatis 真正的力量是在映射语句中.这里是奇迹发生的地方.对于所有的力量,SQL 映射的 XML 文件是相当的简单.当然如果你将它们和对等功能的 JDBC 代码来比较,你会发现映射文件节省了大约 ...
- Spring和quartz整合的入门使用教程
Quartz的maven依赖 <!-- quartz 的jar --> <dependency> <groupId>org.quartz-scheduler< ...
- js实现购物车(源码)
首先是页面布局html+css部分 <!doctype html><html lang="en"> <head> <meta chars ...
- Java进阶SQL函数、网页定时刷新与自定义JSTL函数
一.SQL函数 能够在SQL语句中调用的函数(方法) ,用来实现一些小功能 聚合函数 能够把多行数据聚合成一个值(统计) count() 计数,计算数据条数 max() 计算最大值 m ...
- ZOJ 3932 Handshakes
Last week, n students participated in the annual programming contest of Marjar University. Students ...
- easyui tree操作
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...
- 浏览器端js处理or直接冗余至服务器php处理?
w交给客户端浏览器js处理,减少向服务器的提交字节.精简处理逻辑.