java输出换行的标准姿势"line.separator"
java中写.txt文件,实现换行的几种方法:
1.使用java中的转义符"\r\n":
windows下的文本文件换行符:\r\n
linux/unix下的文本文件换行符:\r
Mac下的文本文件换行符:\n
1.String str="aaa";
2.str+="\r\n";
2.BufferedWriter的newline()方法:
FileOutputStream fos=new FileOutputStream("c;\\11.txt");
BufferedWriter bw=new BufferedWriter(fos);
bw.write("你好");
bw.newline();
bw.write("java");
bw.newline();
/**
* Creates a new buffered character-output stream that uses an output
* buffer of the given size.
*
* @param out A Writer
* @param sz Output-buffer size, a positive integer
*
* @exception IllegalArgumentException If sz is <= 0
*/
public BufferedWriter(Writer out, int sz) {
super(out);
if (sz <= 0)
throw new IllegalArgumentException("Buffer size <= 0");
this.out = out;
cb = new char[sz];
nChars = sz;
nextChar = 0;
lineSeparator = (String) java.security.AccessController.doPrivileged(
new sun.security.action.GetPropertyAction("line.separator"));
}
BufferedWriter中newline()方法:
/**
* Writes a line separator. The line separator string is defined by the
* system property <tt>line.separator</tt>, and is not necessarily a single
* newline ('\n') character.
*
* @exception IOException If an I/O error occurs
*/
public void newLine() throws IOException {
write(lineSeparator);
}
3.使用System.getProperty()方法:
String str = "Output:"+System.getProperty("line.separator");
http://www.cnblogs.com/todoit/archive/2012/04/27/2473232.html
PrintWriter在以下以pw代替,在写client与server进行测试的通讯程序时,用pw.println(str)可以把数据发送给客户端,而pw.write(str)却不行!
查看源码发现:
pw.println(str)方法是由write方法与println()方法组成,而println()方法中执行了newLine()方法。
而 newLine()实现中有一条out.write(lineSeparator);
即println(str)方法比write方法中多输出了一个lineSeparator字符;
其中lineSeparator实现为:
lineSeparator = (String) java.security.AccessController.doPrivileged(new sun.security.action.GetPropertyAction("line.separator"));
而line.separator属性跟据每个系统又是不一样的。
println()方法的注释说明中提到:
/**
* Terminates the current line by writing the line separator string. The
* line separator string is defined by the system property
* <code>line.separator</code>, and is not necessarily a single newline
* character (<code>'\n'</code>).
*/
在我机器上(windows)测试,默认的lineSeparator输出的十六进制为13 10即\r\n
这样write方法修改为:write(str+"\r\n")即达到了与println(str)一样的效果了。
http://blog.csdn.net/vhomes/article/details/6650576
java输出换行的标准姿势"line.separator"的更多相关文章
- 【转】JAVA输出内容打印到TXT以及不同系统中如何换行
JAVA输出内容打印到TXT以及不同系统中如何换行 http://xiyang.09.blog.163.com/blog/static/59827615201172552755293/ 2011-08 ...
- Java基础 println print 实现输出换行
JDK :OpenJDK-11 OS :CentOS 7.6.1810 IDE :Eclipse 2019‑03 typesetting :Markdown code ...
- java在文件中输出换行符
在字符串后面添加\r\n就可以了. 或者使用newline方法: FileOutputStream fos=new FileOutputStream("c:\\11.txt") ...
- Java 输出文件通过 BufferedWriter.newline() 方法换行
最近项目中需要导出文件,其实导出文件是一个挺简单的事情.但是却遇到了很奇怪的问题. 首先导出到文件需要用到 BufferedWriter.而换行则是通过 bw.newline() 方法,问题将出在 n ...
- 使用System.getProperty("line.separator")时没有换行问题解决
项目中要实现替换模版txt文本里面的内容,然后生成新的文档,其中先把模版文本的内容通过创建的 BufferedReader bufReader 使用 readLine() 来一行一行读取,所以在完成替 ...
- java 读入换行
java中实现换行有以下几种方法:1.使用java中的转义符"\r\n": 注意:\r,\n的顺序是不能够对换的,否则不能实现换行的效果. 2.BufferedWriter的new ...
- java写入换行符
写入一个文件,生成文本文档,里面写入1000行字符,但是写出来的没有换行.所以纠结,百度了下,一行完事. String crlf=System.getProperty("line.separ ...
- System.getProperty("line.separator") 是什么意思?
在java中存在一些转义字符,比如"\n"为换行符,但是也有一些JDK自带的一些操作符 比如 : System.getProperty("line.separator&q ...
- 【python】禁止print输出换行的方法
print后用一个逗号结尾就可以禁止输出换行,例子如下 >>> i=0 >>> while i < 3: print i i+=1 0 1 2 禁止输出换行后 ...
随机推荐
- SuperSocket源码解析之会话生命周期
一 基本概念 会话(Session)是客户端与服务器进行通信的基本单元,也是一个Socket的封装,在http协议中也有Session机制,其主要作用封装一个通信单元socket,负责服务器与客户端消 ...
- Orleans-Hello World
Orleans-Hello World http://www.rm5u.com/orleans/orleans-intro.html 什么是Orleans? Orleans(奥尔良) ...
- win10 系统下获取系统版本号为6.2的问题(manifest如何写)
近期赶时髦升级了win10,用着挺爽.但是某天在测试一个bug时发现要对win10做特殊处理,于是直接调用了GetVersionEx,并取出版本号进行判断,但是发现得到的版本竟然是6.2.当时就被雷到 ...
- C/C++取出变量的每一位的值(第一次知道还有QBitArray)
前写程序最多也只是字节级别操作,用char和memcpy进行一系列内存操作.此次一个sdk,其状态值直接是每位一个标示,所以需要取出每位进行操作.当然CPP也有丰富的位运算操作,但是虽然也学过,知道意 ...
- ie6背景透明的设置方法 ie6背景颜色透明和png图像透明解决方法
IE6浏览器,让我们又爱又恨.爱它的是,可以让我们写的代码的时候,可以更标准,恨的是,它有太多无厘头的IE6常见bug(详情点击),让我们焦头烂额.现在现在用百度浏览器调查,国内占有率不到6%了,但是 ...
- api 跳转规则
api 配置: <Context docBase="zjzc-web-api" path="/api" reloadable="false&qu ...
- webwervice发布时出错 java.security.PrivilegedActionException
错误信息: 信息: Dynamically creating response wrapper bean Class com.potevio.ws.jaxws.DealReqResponse Exce ...
- Activity跳转时传递Bitmap对象的实现
前言 相信大家可能都了解Activity跳转时我们是能够传递參数的,比方使用Intent传递或者Bundle来传递,把当前Activity的一些信息传递给将要跳转到的新的Activity.可是不知道大 ...
- /bin/bash^M: 坏的解释器: 没有那个文件或目录
在Linux下编译cocos2d-x运行脚本的时候出现”/bin/bash^M: 坏的解释器: 没有那个文件或目录“这样的错误如下图. 解决方法: 使用在终端输入sed -i 's/\r$//' ma ...
- 【Tips】Endnote导入IEEE Xplore文献方法《转载》
1. 在IEEE XPlore中点击“Download Citation”: 2. 选中“Citation & Abstract”和“EndNote,Procite,RefMan”两个选项: ...