import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; /* 判断D盘下是否有文件夹temp,如果没有则创建temp 在D盘temp文件夹下创建文件 hello.txt, 并写入hello,java 如果hello.txt已存在,提示该文件已存在 */ public class Test01 { public static void main(String[] args) throws IOException { String directoryPath = "d:\\temp"; File file = new File(directoryPath); if (!file.exists()){//如果没有则创建temp if (file.mkdirs()){ System.out.println(directoryPath + "创建成功"); }else{ System.out.println(directoryPath + "创建失败"); } } String filePath = directoryPath + "\\hello.txt";// "d:\\temp\\hello.txt" file = new File(filePath); if (!file.exists()){ if (file.createNewFile()){ System.out.println(filePath + "创建成功"); //写入内容 BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file)); bufferedWriter.write("hello,java"); //关闭流 bufferedWriter.close(); }else{ System.out.println(filePath + "创建失败"); } }else{ System.out.println(file + "文件已存在"); } } }
import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; // 使用BufferedReader读取文本文件test1.txt,并标上行号 public class Test02 { public static void main(String[] args) throws IOException { String filePath = "d:\\test1.txt"; BufferedReader br = null; String line = ""; int count = 0;//行号 try { br = new BufferedReader(new FileReader(filePath)); while ((line = br.readLine()) != null){ System.out.println(++count + "\t" + line); } } catch (FileNotFoundException e) { e.printStackTrace(); } finally { if (br != null){ br.close(); } } } }
import org.junit.jupiter.api.Test; import java.io.*; import java.util.Properties; /* 编写一个dog.properties 属性为 name=jack; age=6; color=black; 编写Dog类(name,age,color),创建一个dog对象,读取dog.properties,完成属性初始化并输出 将创建的dog对象序列化到文件dog.dat文件中 编写一个方法反序列化dog */ public class Test03 { public static void main(String[] args) throws IOException { String filePath = "src\\dog.properties"; Properties properties = new Properties(); properties.load(new FileReader(filePath)); String name = properties.get("name") + "";// Object-->String( + "") int age = Integer.parseInt(properties.get("age") + "");// Object-->int String color = properties.get("color") + "";// Object-->String Dog dog = new Dog(name, age, color); System.out.println(dog); //创建的dog对象序列化到文件dog.dat文件中(注意实现接口) String serFilePath = "d:\\dog.dat"; ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(serFilePath)); oos.writeObject(dog); //关闭流 oos.close(); System.out.println("序列化完成"); } //反序列化 @Test public void m() throws IOException, ClassNotFoundException { String serFilePath = "d:\\dog.dat"; ObjectInputStream ois = new ObjectInputStream(new FileInputStream(serFilePath)); Dog dog = (Dog)ois.readObject(); System.out.println(dog); ois.close();//关流 } } class Dog implements Serializable {//序列化实现接口 private String name; private int age; private String color; public Dog(String name, int age, String color) { this.name = name; this.age = age; this.color = color; } @Override public String toString() { return "Dog{" + "name='" + name + '\'' + ", age=" + age + ", color='" + color + '\'' + '}'; } }