java中读取文件和存储文件

1、统一编码,java工程的编码统一用utf-8;
2、 利用IO文件流的 InputStreamReader()和OutputStreamWriter()这两个函数对文件进行读取和存储操作。

BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileUrl), "UTF-8")); 
bw.write("内容");//将内容写到fileUrl中 
bw.close();
// 或者
OutputStreamWriter out= new OutputStreamWriter(new FileOutputStream(fileUrl), "UTF-8");   
out.write("内容"); //将内容写到fileUrl中  
out.flush(); //将内容刷新到文件中存储
out.close();
 
//读取操作:
BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream(fileUrl),"UTF-8"));
String s=null;
while((s=br.readLine())!=null)
{
    System.out.println(s);  
}
br.close();

你可能感兴趣的:(java中读取文件和存储文件)