FileReader例子
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class FileReaderTest {
public static void main(String[] args) {
FileReader reader = null;
try {
reader = new FileReader("D:\\abc.txt");
char[] chars = new char[1];
int readCount = 0;
while((readCount = reader.read(chars)) != -1){
System.out.println(new String(chars, 0, readCount));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
FileWriter例子
import java.io.FileWriter;
import java.io.IOException;
public class FileWriterTest {
public static void main(String[] args) {
FileWriter fw = null;
try {
fw = new FileWriter("D:\\sbabc.txt");
char[] chars = {'我','是','你','的'};
fw.write(chars);
fw.write(chars,2,2);
fw.write("lmx" +
"lxn" +
"sb" +
"okt" +
"sg" +
"");
fw.flush();
} catch (IOException e) {
e.printStackTrace();
}finally {
if (fw != null) {
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}