public class FileTest01 {
public static void writeFileString() {
File file = new File("E:\\zkn");
if(!file.isDirectory())
file.mkdir();//创建目录
File fileDir = new File(file,"zkn.txt");
if(!fileDir.isFile()){
try {
fileDir.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
FileWriter fw = new FileWriter(fileDir);//FileWriter写入文件时不能指定编码格式,编码格式是系统默认的编码格式
fw.write("11111111\n111111"); //向文件中写入字符串
fw.flush(); //刷新
fw.close(); //关闭流
} catch (IOException e) {
e.printStackTrace();
}
}
public static void readFileString() {
try {
FileReader fr = new FileReader("E:\\zkn\\zkn.txt");//字符读入流
BufferedReader br = new BufferedReader(fr);
while(br.read() > 0){
System.out.println(br.readLine());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
FileTest01.writeFileString();
FileTest01.readFileString();
}
}