首页 > 编程语言 >Java实验报告五

Java实验报告五

时间:2023-11-24 16:14:25浏览次数:45  
标签:java File new file Student Java 实验报告 public

实验五

实验名称: 文件与 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 }

 

标签:java,File,new,file,Student,Java,实验报告,public
From: https://www.cnblogs.com/litunan/p/17853983.html

相关文章

  • JavaWeb中的文件的上传和下载
    文件上传1.要有一个form标签,method=post请求2.form标签的encType属性的值必须为multipart/form-data值3.在from标签中使用inputtype=file添加上传的文件4.编写服务器代码接收上传的数据Content-Type:表示提交的数据类型enctype="multipart/form-data":表示提交的数据,以多段(每......
  • Java线程生命周期
    操作系统线程生命周期操作系统线程生命状态有5种。初始状态(New)进程正在被创建时的状态。仅为线程对象开辟了一块堆内存,实际上线程在操作系统层面还未创建。就绪状态(Ready)可运行,由于其他进程处于运行状态而暂时停止运行运行状态(Running)该进程此时正占用CPU......
  • Java实验报告
    实验一实验名称:JAVA中循环结构实验目的:熟悉循环结构,熟悉JAVA类的定义以及参数的传递。实验时间:(2学时)实验类型:验证型实验内容:(1)金字塔:Pyramid.java在屏幕上显示一个由星型符号“*”组成的金字塔图案,要求用户设置金字塔的高度,程序能根据用户设置的高度打印金字塔,......
  • Java系列之 String indexOf() 方法
    我|在这里 ......
  • 03_Exception in thread “main“ java.lang.AssertionError
    问题maven构建报错***[INFO]---maven-compiler-plugin:3.8.1:compile(default-compile)@engine_auth---[INFO]Changesdetected-recompilingthemodule![INFO]Compiling38sourcefilestoC:\Users\...\target\classes[INFO]-----------------------------......
  • [Java] 解析Xml配置文件
    1、解析方法importjavax.xml.parsers.DocumentBuilder;importjavax.xml.parsers.DocumentBuilderFactory;importorg.w3c.dom.Document;importorg.w3c.dom.NamedNodeMap;importorg.w3c.dom.Node;importorg.w3c.dom.NodeList;publicstaticMap<String,String>pars......
  • Java中使用try-with-resources
    Java7中引入的对资源 try-with-resources ,声明在 try 块中使用的资源,并保证资源将在该块执行后关闭。声明的资源需要实现自动关闭接口。 1.使用资源try典型的try-catch-finally块:Scannerscanner=null;try{scanner=newScanner(newFile("test.txt"));......
  • Java设计模式之代理模式
    在某些情况下,我们希望通过一个中间代理来控制对某个对象的访问,这可能是因为原始对象的创建或访问涉及复杂的逻辑,或者我们想要在访问原始对象之前或之后执行一些操作代理模式提供了一个代理对象,它充当了原始的对象的替代品,以控制对原始对象的访问。代理对象与原始对象实现相同的接口......
  • 值得收藏的一些HTML、JavaScript、ASP代码
    1.CDONTS.NewMail组件使用说明 SetMailObject=Server.CreateObject("CDONTS.NewMail") MailObject.From="发信邮箱" MailObject.To="收信邮箱" MailObject.Cc="抄送邮箱" MailObject.Bcc="密送邮箱" MailObject.Subject=&qu......
  • HTML+CSS+Javascript+Vue
    TableofContentsI.HTMLII.CSSGetStartedI.HTML按tab自动生成LabelMeaningPropertiesdiv块状元素span行间元素h1-h6标题iicon图标strong字体加粗a超链接img插入图片video插入视频controlsinput表单(输入账号、......