Java写到.txt文件,如何实现换行
http://blog.csdn.net/liangoo7/article/details/7882773
java中写.txt文件,实现换行的几种方法:
1.使用java中的转义符"\r\n":
- String str="aaa";
- str+="\r\n";
这样在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");
- w.newline();
3.使用System.getProperty()方法:
- String str = "aaa"+System.getProperty("line.separator");
据我所知,windows和linux/unix平台中应该没有通用的换行符的。针对常用的系统,可以使用如下的转义符实现换行:
windows下的文本文件换行符:\r\n
linux/unix下的文本文件换行符:\r
Mac下的文本文件换行符:\n
========================================
正确的转换方式类似如下:
String s=s.replaceAll("\\n","\r\n"); 如果流往外写文件;参见最下面的程序 这个问题是jdk在不同操作系统的自动甄别问题
这个就是那个换行符:String line = System.getProperty("line.separator"); 不同操作系统取的值是不同的。 我觉得这个问题;你应该告知对方正确的解析方式;而不是修改己方的文件输出。 写文件演示
/**
*
* @author Vava
* 测试目的:测试向一个文件中写入换行
* 测试结果:测试成功
*/
public class WriteToFile {
public static void main(String[] args)throws Exception {
String Url = "/home/vava/aaa.txt";
File file = new File("Url");
String line = System.getProperty("line.separator"); //在这个位置更换为自己想使用的换行符
//File fout = new File("fout.txt"); // 创建文件输出对象
File fout = new File(Url); // 创建文件输出对象
FileWriter out = new FileWriter(fout); // 创建文件字符流 写 对象,传递文件对象
out.write("你好某某某某某某某某");
out.write(line);
out.write("你好某某某某某某某某");
out.write("你好某某某某某某某某");
out.flush();
out.close();
}
Java写到.txt文件,如何实现换行的更多相关文章
- python操作txt文件中数据教程[4]-python去掉txt文件行尾换行
python操作txt文件中数据教程[4]-python去掉txt文件行尾换行 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考文章 python操作txt文件中数据教程[1]-使用pyt ...
- Java中读取txt文件中中文字符时,出现乱码的解决办法
这是我写的一个Java课程作业时,遇到的问题. 问题描述: 我要实现的就是将txt文件中的内容按一定格式读取出来后,存放在相应的数组. 我刚开始运行时发现,英文可以实现,但是中文字符就是各种乱码. 最 ...
- Java 写数据到文件
private boolean writeToFile(BusGpsBean gpsBean) { String dataStr = DateUtil.date2String(new Date(), ...
- Java逐行读写TXT文件
package help; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; imp ...
- java追加写入txt文件
整理了下网上的资料,数据追加写入txt文件有三种方式,见下面代码: 方法一: public void method1() { FileWriter fw = null; try { //如果文件存在, ...
- python爬虫学习(二):定向爬虫例子-->使用BeautifulSoup爬取"软科中国最好大学排名-生源质量排名2018",并把结果写进txt文件
在正式爬取之前,先做一个试验,看一下爬取的数据对象的类型是如何转换为列表的: 写一个html文档: x.html<html><head><title>This is ...
- Java学习——读写txt文件
package HHH; import java.io.*; import static java.lang.System.out; public class OpenFile { public st ...
- 将richTextBox中的内容写入txt文件发现不换行(解决方法),在richTextBox指定位置插入文字
string pathname = dt.ToString().Replace(":", ""); string str = richTextBoxResult ...
- Spark1.6.2 java实现读取txt文件插入MySql数据库代码
package com.gosun.spark1; import java.util.ArrayList;import java.util.List;import java.util.Properti ...
随机推荐
- Aspose.words 替换字符 操作
var path = Server.MapPath("~/doc/demo.doc"); Document doc = new Document(path); DocumentBu ...
- hibernate笔记(二)
目标: 关联映射(hibernate映射) 1. 集合映射 2. 一对多与多对一映射 (重点) 3. 多对多映射 4. inverse/lazy/cascade 1. 集合映射 开发流程: 需求分析/ ...
- 【bzoj1458】士兵占领 有上下界最小流
题目描述 有一个M * N的棋盘,有的格子是障碍.现在你要选择一些格子来放置一些士兵,一个格子里最多可以放置一个士兵,障碍格里不能放置士兵.我们称这些士兵占领了整个棋盘当满足第i行至少放置了Li个士兵 ...
- SNMP OID Reference - NetScaler 10
SNMP OID Reference - NetScaler 10 https://docs.citrix.com/content/dam/docs/en-us/netscaler/10/downlo ...
- Wpremig和Jhadgre的藏宝图(最大流+二分)
Wpremig和Jhadgre的藏宝图 题目链接:https://ac.nowcoder.com/acm/contest/333/M Description: Jhadgre在生日那天收到了一张神秘的 ...
- Failed to resolve:com.android.support:appcompat-v7
http://blog.csdn.net/mhl18820672087/article/details/78385361
- matlab求矩阵的鞍点
function count = andian(a) v = max(a,[],2); count = 0; for i=1:length(v) [r2,c2] = find(a==v(i)); mi ...
- 调整文本输入框placeholder的颜色等样式
input::-webkit-input-placeholder{ color: white !important;}input:-moz-placeholder{ color: whi ...
- HDU2066一个人的旅行---(多起点多终点最短路径)
http://acm.hdu.edu.cn/showproblem.php?pid=2066 一个人的旅行 Time Limit: 1000/1000 MS (Java/Others) Memo ...
- 如何实现用户id生成一个唯一邀请码
#如何实现用户id生成一个唯一邀请码 #创建验证码 function createCode($user_id) { static $source_string = 'E5FCDG3HQA4B1NOPI ...