java读取文件和写入文件
//读取
public static String readFile(String fileUrl) throws IOException {
FileInputStream fis=new FileInputStream(str);
InputStreamReader isr=new InputStreamReader(fis, "GBK");//utf-8 或者gbk 根据你个人需求
BufferedReader br = new BufferedReader(isr);
//简写如下
//BufferedReader br = new BufferedReader(new InputStreamReader(
// new FileInputStream("E:/phsftp/evdokey/evdokey_201103221556.txt"), "UTF-8"));
String line="";
String arrs="";
while ((line=br.readLine())!=null) {
arrs += line+",";
}
br.close();
isr.close();
fis.close();
return arrs;//返回的字符串 可以 split(“,”) 来分割为数组
}
//以字符串数组形式写入
public static void writeFile(String []arrs) throws IOException { FileOutputStream fos=new FileOutputStream(new File("C:/Users......")); OutputStreamWriter osw=new OutputStreamWriter(fos, "GBK"); BufferedWriter bw=new BufferedWriter(osw); //简写如下: //BufferedWriter writer = new BufferedWriter(new OutputStreamWriter( // new FileOutputStream(new File("E:/phsftp/evdokey/evdokey_201103221556.txt")), "UTF-8")); for(String arr:arrs){ bw.write(arr+"\t\n"); } //注意关闭的先后顺序,先打开的后关闭,后打开的先关闭 bw.close(); osw.close(); fos.close(); }