题1:
1.判断D盘work文件夹中是否有mytemp文件夹,如果没有就创建mytemp文件夹
2.在d:\work\mytemp目录下,创建文件hello.txt,若hello.txt已经存在。提示该文件已经存在,就不要在重复创建
3.并且在hello.txt文件中,写入hello-world~~
/*题目: *1.判断D盘work文件夹中是否有mytemp文件安家,如果没有就创建mytemp文件夹 *2.在d:\\work\\mytemp目录下,创建文件hello.txt,若hello.txt已经存在。提示该文件已经存在,就不要在重复创建 *3.并且在hello.txt文件中,写入hello-world~~~ * */ //1.判断D盘work文件夹中是否有mytemp文件夹,如果没有就创建mytemp文件夹 //1.1)设置文件夹的位置 String fileName = "d:\\work\\mytemp"; //1.2)创建File对象 File file = new File(fileName); //1.3)判断该文件夹是否存在 if(file.exists()){ System.out.println("mytemp该文件夹存在..."); }else{ System.out.println("mytemp文件夹不存在..."); System.out.println("创建该文件夹..."); //1.4)若文件夹不存在,则创建该文件夹 if(file.mkdirs()){ System.out.println("mytemp文件夹创建成功..."); }else { System.out.println("mytemp文件夹创建失败..."); } } //2.在d:\\work\\mytemp目录下,创建文件hello.txt //2.1)设置文件创建的地址 String fileName02 ="d:\\work\\mytemp\\hello.txt"; //2.2)创建File对象 File file1 = new File(fileName02); try { //2.3)判断文件是否存在 if (file1.exists()){ System.out.println("hello.txt文件已经存在,请不再重建..."); //3.并且在hello.txt文件中,写入hello-world~~~ BufferedWriter bw = new BufferedWriter(new FileWriter(fileName02)); bw.write("hello-world~~~"); bw.close(); }else { //2.4)若未创建该文件,则创建 file1.createNewFile(); System.out.println("hello.txt文件创建成功..."); } } catch (IOException e) { e.printStackTrace(); }
题2:
使用BufferedReader读取一个文本文件,为每一行加上行号,
再连同内容一并输出到屏幕上;
/*题目: * 使用BufferedReader读取一个文本文件,为每一行加上行号, * 再连同内容一并输出到屏幕上 * */ //1.设置读取文件的地址 String fileName = "d:\\work\\feature.txt"; //2.创建对象 BufferedReader br = new BufferedReader(new FileReader(fileName)); //3.接收每行的数据 String line = " "; //4.记录所处的行数 int num = 0; //5.循环接收 while ((line = br.readLine()) != null){ num++; System.out.println(num + ":" + line); } //6关闭流 br.close();
题3:
编写一个dog.properties
内容:{name=tom;age=5;color=red}
2.编写一个Dog类(name,age,color)创建一个dog对象
读取dog.properties用相应的内容完成属性初始化并输出
3.将创建的Dog对象,序列化到文件dog.txt文件
/*题目: * 1.编写一个dog.properties * name=tom * age=5 * color=red * 2.编写一个Dog类(name,age,color)创建一个dog对象 * 读取dog.properties用相应的内容完成属性初始化 并输出 * 3.将创建的Dog对象,序列化到文件dog.txt文件 * */ //1.设置需要的文件路径 String filePath = "src\\dog.properties"; //2.创建Properties对象 Properties properties = new Properties(); //3.加载盘配置文件的键值对 到properties类 properties.load(new FileReader(filePath)); //4.根据键找出所对应的值 并进行赋给相应的变量 String name = properties.get("name") + ""; int age = Integer.parseInt(properties.get("age") + ""); String color = properties.get("color") +""; //5.将4中所获得的变量添加到Dog类中 Dog dog = new Dog(name,age,color); System.out.println(dog); //序列化: //6.设置序列化到某文件的路径 String fileName = "d:\\work\\dog.dat"; //7.创建ObjectOutputStream对象 ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(fileName)); //8.写入dog类信息 oos.writeObject(dog); //9.关闭流 oos.close(); System.out.println("序列化完成...");
@Test //反序列化 public void text01() throws IOException, ClassNotFoundException { //1.设置需要反序列化的地址 String fileName = "d:\\work\\dog.dat"; //2.创建ObjectInputStream对象 ObjectInputStream ois = new ObjectInputStream(new FileInputStream(fileName)); //3.读取Dog类的信息,并进行强壮 Dog dog = (Dog)ois.readObject(); //4.关闭流 ois.close(); System.out.println("反序列化成功..."); System.out.println(dog);
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 name + "\t" + age + "\t" + color ; } }