java写文件如何加换行符

2025-01-06 10:31:59
推荐回答(1个)
回答1:

package io;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
public class WriteFile {
public static void Write(String path,String context,boolean bool){
OutputStreamWriter osr = null;
OutputStream os = null;
try {
os=new FileOutputStream(path,bool);
osr=new OutputStreamWriter(os,"utf-8");
osr.write(context);
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
if(osr!=null)
osr.close();
if(os!=null)
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}

}

public static void main(String[] args) {
Write("D:\\文档\\url.txt", "qwe \r\n 123", true);;
}

}