Write的基本方法
public class WriterDemo {
public static void main(String[] args) {
File file = new File("a4.txt");
Writer writer = null;
try {
writer = new FileWriter(file,false);
writer.write(99);
writer.write("abc你好,中国!");
writer.write(new char[]{'5','t','p'});
writer.append('a').append('b').append('c').append('d');
writer.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Reader的基本方法
public class BufferedReaderDemo {
public static void main(String[] args) {
FileReader fileReader = null;
BufferedReader reader = null;
try {
fileReader = new FileReader("a.txt");
reader = new BufferedReader(fileReader);
String str = "";
while((str = reader.readLine())!=null){
System.out.println(str);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}