java文件写入怎样换行

不靠谱的哥哥 2010-06-06 10:36:10
import java.io.*;

import javax.swing.JOptionPane;

public class test {

public static void main(String []args){


try{

FileInputStream fis=new FileInputStream("namefile.txt");

InputStreamReader dis=new InputStreamReader(fis);

BufferedReader reader=new BufferedReader(dis);

String s,str="";


while((s=reader.readLine())!=null){

str+=s;

str+="\r\n";

System.out.println(str);

}


dis.close();

FileOutputStream fos=new FileOutputStream("namefile.txt");

DataOutputStream dos=new DataOutputStream(fos);

//BufferedWriter writer=new BufferedWriter(dos);

String ss="TonHankes";

str=str+ss;

dos.writeChars(str);

dos.close();

}

catch(IOException e){

System.out.println(e);

}

}

}

这是一个我写的关于文件写入的代码,但不知道怎么实现换行,"\r\n"也不奏效
跪求高手解答
...全文
2863 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhouwolf 2010-09-14
  • 打赏
  • 举报
回复
1楼的不行。
cz_study_java 2010-09-14
  • 打赏
  • 举报
回复
学习一下
zhuyouyong 2010-06-07
  • 打赏
  • 举报
回复
顶[Quote=引用 1 楼 gulang76 的回复:]
Java code
FileOutputStream fos = new FileOutputStream( "d:\\test.txt ");
String nextLine = System.getProperty( "line.separator ");
String testStr = "abcd "+nextLine+ "efgh "+n……
[/Quote]
So_So 2010-06-07
  • 打赏
  • 举报
回复
File file = new File("src/ftp.txt");
FileReader fd = new FileReader(file);
BufferedReader br = new BufferedReader(fd);
StringBuilder sb = new StringBuilder();
String str = "";
while ((str = br.readLine()) != null)
{
sb.append(str);
sb.append("\n");
}
int lenth = 100;
String str2=sb.toString();
sb.setLength(0);
for (int i = 1; i <= lenth; i++)
{
str=str2.replaceAll(temp1,i+"");
sb.append(str);
sb.append("\n");
}
FileOutputStream out=new FileOutputStream(file);
out.write(sb.toString().getBytes());
out.close();
}
So_So 2010-06-07
  • 打赏
  • 举报
回复
这个用stringbuffer 很好用的
tjb1216 2010-06-07
  • 打赏
  • 举报
回复
1楼跟5楼都能行,鉴定完毕
taotao119 2010-06-07
  • 打赏
  • 举报
回复
用BufferedWriter类的newLine()就可以了
robin_hood2012 2010-06-07
  • 打赏
  • 举报
回复
用\n换行试试
  • 打赏
  • 举报
回复
没学到哪,顶一下
gulang76 2010-06-06
  • 打赏
  • 举报
回复
FileOutputStream   fos   =   new   FileOutputStream( "d:\\test.txt "); 
String nextLine = System.getProperty( "line.separator ");
String testStr = "abcd "+nextLine+ "efgh "+nextLine+ "igkl ";
byte[] c = testStr.getBytes();
fos.write(c);
fos.close();

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧