文件读写IO操作

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.Closeable;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;


public class test3 {
public static void main(String[] args) throws FileNotFoundException {


// 读取文件,写入文件


FileInputStream fis = null;
FileOutputStream fos = null;
Reader isr = null;
BufferedReader buffReader = null;
BufferedWriter buffWriter = null;
Writer osr = null;
try {
fis = new FileInputStream("d://1.txt");
fos = new FileOutputStream("d:\\2.txt");
// InputStreamReader isr=new InputStreamReader(fis,"utf-8");
isr = new InputStreamReader(fis, "utf-8");
buffReader = new BufferedReader(isr);
osr = new OutputStreamWriter(fos, "utf-8");
buffWriter = new BufferedWriter(osr);
String line = null;
while ((line = buffReader.readLine()) != null) {
buffWriter.write(line);
buffWriter.flush();
// buffWriter.newLine();
buffWriter.write("\n\r");
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
closeQuitely(fis);
closeQuitely(isr);
closeQuitely(buffReader);
closeQuitely(buffWriter);
closeQuitely(osr);
closeQuitely(fos);
}


// trim函数的用法.把字母的数值去掉
/*
* String s = "  d   "; System.out.println(s.length());
* System.out.println(s.trim().length());
*/


}


public static void closeQuitely(Closeable close) {
if (close != null) {
try {
close.close();
} catch (IOException e) {
}
}
}
}

你可能感兴趣的:(java)