1.
package homework; import java.io.*; /** * @author 紫英 * @version 1.0 * @discription */ @SuppressWarnings("all") public class Homework01 { public static void main(String[] args) throws IOException { String dirPath = "E:\\mytemp"; File file = new File(dirPath); if (file.exists()) { System.out.println("目录已存在"); } else { if (file.mkdir()) { System.out.println("目录创建成功!"); } else { System.out.println("目录创建失败!"); } System.out.println("目录不存在可以创建"); } File file2 = new File(dirPath, "hello.txt"); if (!file2.exists()) { file2.createNewFile(); System.out.println("hello.txt创建成功"); } else { System.out.println("文件已存在"); } BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file2)); bufferedWriter.write("hello,world!"); bufferedWriter.close();//一定记得关!!!!! System.out.println("写入成功"); } }
2.
package homework; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; /** * @author 紫英 * @version 1.0 * @discription */ public class Homework02 { public static void main(String[] args) throws IOException { BufferedReader bufferedReader = new BufferedReader(new FileReader("e:\\a.txt")); String s = null; int i =0; while ((s=bufferedReader.readLine())!=null){ System.out.println(++i +" "+ s); } bufferedReader.close(); } }
3.