读取的时候根据读取什么内容选取合适的流,
FileReader
public static void main(String[] args){
FileReader f = null;
try {
f = new FileReader("D:\\ppp\\p.txt");
char []c = new char[512];
int temp = 0;
while((temp=f.read(c)) != -1) {
System.out.println(new String(c,0,temp));
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if(f!= null) {
try {
f.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
FileWriter
public static void main(String[] args) throws IOException {
//FileWriter f = new FileWriter("D:\\ppp\\p");//覆盖
FileWriter f = new FileWriter("D:\\ppp\\p.txt",true);//追加
f.write("李");
char c[] = {'w','s','e'};
f.write(c, 0, 3);
f.flush();
f.close();
}