Java实验报告五

发布时间 2023-11-24 16:05:28作者: 李图南99

实验五

实验名称: 文件与 I/O 流
实验目的: 掌握文件与输入输出流的使用。
实验时间: (2 学时)
实验类型: 验证型
实验内容:
1.创建类: FindFile.java, 遍历当前目录,打印目录中文件名称,目录打印”is Directory”,文件打印“is file”。修改程序打印当前目录及子目录信息。 提示:当前目录名用”.”表示
2.用对象序列化把若干 Student 对象写到文件中,再读取出来
3.写一个程序,列出某目录下所有的.java 文件(包括子目录)
4. 编写一个 Java 应用程序,该程序使用 FileInputStream 类,实现从磁盘读取本应用程序源代码文件,并将文件内容显示在屏幕上。

(1) FindFile.java

 1 import java.io.File;
 2 
 3 public class FindFile {
 4     public static void main(String[] args) {
 5         File currentDir = new File(".");
 6         printDirectoryContents(currentDir);
 7     }
 8 
 9     public static void printDirectoryContents(File dir) {
10         File[] files = dir.listFiles();
11         if (files != null) {
12             for (File file : files) {
13                 if (file.isDirectory()) {
14                     System.out.println(file.getName() + " is Directory");
15                     printDirectoryContents(file);
16                 } else {
17                     System.out.println(file.getName() + " is file");
18                 }
19             }
20         }
21     }
22 }

 

 

(2)Students.java

 1 import java.io.*;
 2 
 3 public class Student implements Serializable {
 4     private String name;
 5     private int age;
 6 
 7     public Student(String name, int age) {
 8         this.name = name;
 9         this.age = age;
10     }
11 
12     public static void main(String[] args) {
13         // 创建学生对象
14         Student student1 = new Student("Alice", 20);
15         Student student2 = new Student("Bob", 21);
16 
17         // 将学生对象写入文件
18         try {
19             FileOutputStream fos = new FileOutputStream("students.dat");
20             ObjectOutputStream oos = new ObjectOutputStream(fos);
21             oos.writeObject(student1);
22             oos.writeObject(student2);
23             oos.close();
24         } catch (IOException e) {
25             e.printStackTrace();
26         }
27 
28         // 从文件中读取学生对象
29         try {
30             FileInputStream fis = new FileInputStream("students.dat");
31             ObjectInputStream ois = new ObjectInputStream(fis);
32             Student readStudent1 = (Student) ois.readObject();
33             Student readStudent2 = (Student) ois.readObject();
34             ois.close();
35 
36             System.out.println("Student 1: " + readStudent1.getName() + ", " + readStudent1.getAge());
37             System.out.println("Student 2: " + readStudent2.getName() + ", " + readStudent2.getAge());
38         } catch (IOException | ClassNotFoundException e) {
39             e.printStackTrace();
40         }
41     }
42 
43     public String getName() {
44         return name;
45     }
46 
47     public int getAge() {
48         return age;
49     }
50 }

 

(3)ListJavaFiles.java

 1 import java.io.File;
 2 
 3 public class ListJavaFiles {
 4     public static void main(String[] args) {
 5         File directory = new File("D:\\devtools\\IdeaProjects\\untitled5");
 6         listJavaFiles(directory);
 7     }
 8 
 9     public static void listJavaFiles(File directory) {
10         File[] files = directory.listFiles();
11         if (files != null) {
12             for (File file : files) {
13                 if (file.isDirectory()) {
14                     listJavaFiles(file);
15                 } else if (file.getName().endsWith(".java")) {
16                     System.out.println(file.getAbsolutePath());
17                 }
18             }
19         }
20     }
21 }

 

(4)ReadSourceCode.java

 1 import java.io.File;
 2 import java.io.FileInputStream;
 3 import java.io.IOException;
 4 
 5 public class ReadSourceCode {
 6     public static void main(String[] args) {
 7         File sourceFile = new File("D:\\devtools\\IdeaProjects\\untitled5\\src\\CopyArray.java");
 8         readSourceCode(sourceFile);
 9     }
10 
11     public static void readSourceCode(File file) {
12         try (FileInputStream fis = new FileInputStream(file)) {
13             byte[] buffer = new byte[1024];
14             int bytesRead;
15             while ((bytesRead = fis.read(buffer)) != -1) {
16                 System.out.write(buffer, 0, bytesRead);
17             }
18         } catch (IOException e) {
19             e.printStackTrace();
20         }
21     }
22 }