package file.homework;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class HomeWork01 {
public static void main(String[] args) throws IOException {
String directoryPath = "e:\\mytemp";
File file = new File(directoryPath);
if(!(file.exists())){//不存在就创建
if(file.mkdirs()){
System.out.println("创建成功");
}
}else{
System.out.println("创建失败");
}
String filePath = directoryPath+"\\hello.txt";
file = new File(filePath);
if (!file.exists()){
if(file.createNewFile()){//创建失败
System.out.println("创建成功");
//如果文件存在,使用BufferedWriter字符输入流写入内容
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file));
bufferedWriter.write("hello world");
bufferedWriter.close();
}else{
System.out.println("已经存在");
}
}
}
}
package file.homework;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class HomeWork02 {
public static void main(String[] args) {
String filePath = "d:\\x.txt";
BufferedReader br=null;
int lineNum =0;
String line = null;
try {
br = new BufferedReader(new FileReader(filePath));
while((line =br.readLine())!=null){
System.out.println(++lineNum+line);//行号,内容
}
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
try {
if (br != null){
br.close();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}