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"的更多相关文章

  1. 【转】JAVA输出内容打印到TXT以及不同系统中如何换行

    JAVA输出内容打印到TXT以及不同系统中如何换行 http://xiyang.09.blog.163.com/blog/static/59827615201172552755293/ 2011-08 ...

  2. Java基础 println print 实现输出换行

        JDK :OpenJDK-11      OS :CentOS 7.6.1810      IDE :Eclipse 2019‑03 typesetting :Markdown   code ...

  3. java在文件中输出换行符

    在字符串后面添加\r\n就可以了.   或者使用newline方法: FileOutputStream fos=new FileOutputStream("c:\\11.txt") ...

  4. Java 输出文件通过 BufferedWriter.newline() 方法换行

    最近项目中需要导出文件,其实导出文件是一个挺简单的事情.但是却遇到了很奇怪的问题. 首先导出到文件需要用到 BufferedWriter.而换行则是通过 bw.newline() 方法,问题将出在 n ...

  5. 使用System.getProperty("line.separator")时没有换行问题解决

    项目中要实现替换模版txt文本里面的内容,然后生成新的文档,其中先把模版文本的内容通过创建的 BufferedReader bufReader 使用 readLine() 来一行一行读取,所以在完成替 ...

  6. java 读入换行

    java中实现换行有以下几种方法:1.使用java中的转义符"\r\n": 注意:\r,\n的顺序是不能够对换的,否则不能实现换行的效果. 2.BufferedWriter的new ...

  7. java写入换行符

    写入一个文件,生成文本文档,里面写入1000行字符,但是写出来的没有换行.所以纠结,百度了下,一行完事. String crlf=System.getProperty("line.separ ...

  8. System.getProperty("line.separator") 是什么意思?

    在java中存在一些转义字符,比如"\n"为换行符,但是也有一些JDK自带的一些操作符 比如 : System.getProperty("line.separator&q ...

  9. 【python】禁止print输出换行的方法

    print后用一个逗号结尾就可以禁止输出换行,例子如下 >>> i=0 >>> while i < 3: print i i+=1 0 1 2 禁止输出换行后 ...

随机推荐

  1. gethostbyname() -- 用域名或主机名获取IP地址

    #include <netdb.h>    #include <sys/socket.h> struct hostent *gethostbyname(const char * ...

  2. duilib之源码分析

    <duilib之源码分析>1 stdAfx.h * lengthof(x) 计算数组长度宏 * MAX  两值最大 * MIN  两值最小 * CLAMP(x,a,b) x在a,b之间则取 ...

  3. WPF Multi-Touch 开发:高级触屏操作(Manipulation)

    原文 WPF Multi-Touch 开发:高级触屏操作(Manipulation) 在上一篇中我们对基础触控操作有了初步了解,本篇将继续介绍触碰控制的高级操作(Manipulation),在高级操作 ...

  4. docker学习笔记1:docke环境的查看

    本文的操作是在ubuntu操作系统下的. 一.环境检查 当登录一个安装了docker的机器后,首先我们要检查下docker环境如何. 1.命令:docker -v 上述命令返回安装的docker的版本 ...

  5. mysql 表级锁

    表级锁:分为读锁和写锁: lock tables table_name read;//其他事务只能读,不能加写锁,要等待更新. SESSION 50 执行: mysql> update test ...

  6. TCP/IP笔记 三.运输层(1)——UDP,TCP

    1. 运输层 1.1 两种协议:TCP和UDP. (1)TCP:提供了一种可靠的数据传输服务,TCP是面向连接的,只有链接建立起来后才能通信. (2)UDP:是把数据直接发出去,而不管对方是不是在收信 ...

  7. c语言,数组和指针

    概要: 1.普通数组与指针 2.数组指针 3.指针的数组 数组是一个由(同一类型)连续元素组成的预先分配的内存块:指针是一个对任何位置的元素的引用. 数组自动分配空间,但不能重分配或改变大小:指针必须 ...

  8. 黑马程序猿_Objective C 类与协议

    <a href="http://www.itheima.com"target="blank">ASP.Net+Unity开发</a>.& ...

  9. Struts 2最新0day破坏性漏洞(远程任意代码执行)等的重现方法

    Struts 2的远程任意代码执行和重定向漏洞,是这两天互联网上最重大的安全事件,据说国内互联网企业中,很多电商纷纷中招,应该已经有大规模的用户隐私泄露.这里我们简单总结下怎样在自己机子上重现这些漏洞 ...

  10. string的不可变性

    1.不可变性 代码如下: static void Main(string[] args){string str1 = "a";string str2 = str1;str2 = & ...