java - 尝试读取txt文件并将它们转换为具有不同原语的对象

标签 java inputstream bufferedreader

我正在尝试编写一种方法,使用阅读器从纯 txt 文件(学生 ID-学生姓名-学生姓氏)中读取学生详细信息,并创建并返回相应的新学生对象。 。 txt 文件包含逐行详细信息,例如。学生 ID 位于一行,姓名位于下一行。

我尝试在 readStudent() 方法中执行此操作

class StudentInputStream{

BufferedReader in;
public StudentInputStream(InputStream input) {
        in = new BufferedReader(new InputStreamReader(input));
    }

@Override
    public void close() throws IOException {
        in.close();
    }

    public Student readStudent() throws IOException {
        /*Student student1 = new student();*/
        return null; 
    }
}

最佳答案

代码是不言自明的,如果您有任何疑问,请告诉我。

File file = new File("Your file's path");
     Scanner sc=null;
     try {
        sc = new Scanner(file);
     } catch (FileNotFoundException e) {
        e.printStackTrace();
     }
     ArrayList<Student> list = new ArrayList<>();
     while(sc.hasNextLine()){
         if(sc.nextLine().equalsIgnoreCase("student")){
             //Assuming each property is in the seperate line of file
         String id,name,surname=null;
         if(sc.hasNextLine()){
         id = sc.nextLine();
         /*if id is int use
          * int id = Integer.parseInt(sc.nextLine());
          */
         }
         if(sc.hasNextLine()){
             name = sc.nextLine();
         }
         if(sc.hasNextLine()){
             surname = sc.nextLine();
         }
         list.add(new Student(id,name,surname));
         
         }
     }

使用 bufferedReader:

InputStream in = new FileInputStream("Your file's path");
     BufferedReader br = new BufferedReader(new InputStreamReader(in));
     String str;
     ArrayList<Student> list = new ArrayList<>();
     while((str=br.readLine())!=null){
         if(str.equalsIgnoreCase("student")){
             String id=null,name=null,surname=null;
             if((str=br.readLine())!=null){
             id = str;
             /*if id is int use
              * int id = Integer.parseInt(sc.nextLine());
              */
             }
             if((str=br.readLine())!=null){
                 name = str;
             }
             if((str=br.readLine())!=null){
                 surname = str;
             }
             list.add(new Student(id,name,surname));
         }
     }

使用对象输入流

OutputStream out = new FileOutputStream("yourfilepath.bin");
     ObjectOutputStream outputStream = new ObjectOutputStream(out);
     
     Student s1 = new Student("100383", "JOHN", "MITCHELL");
     Student s2 = new Student("100346", "AMY", "CHING");
     
     
     outputStream.writeObject(s1);
     outputStream.writeObject(s2);
     outputStream.writeObject(null);//to realize that you reach the end of file
     
     outputStream.close();
     out.close();
     
     InputStream in = new FileInputStream("yourfilepath.bin");
     ObjectInputStream inputStream = new ObjectInputStream(in);
     
     Student temp = null;
     
     while((temp =(Student)inputStream.readObject())!=null){
         System.out.println(temp.id+","+temp.name+","+temp.surname);
     }

输出

100383,JOHN,MITCHELL

100346,AMY,CHING

关于java - 尝试读取txt文件并将它们转换为具有不同原语的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55142773/

相关文章:

java - 使用客户端阻塞的套接字进行客户端与服务器通信

java - 如果我在 Java 中使用 BufferedReader 进行流式传输,是否存在资源泄漏

javascript - 在 android studio 中将 JavaScript (UnityScript) 翻译为 Java 随机字符串生成

Java 字符串缓冲区

java - 语义分析

java - 从 1 个 zip 文件到另一个 zip 文件时出现错误 java.util.zip.ZipException : invalid entry size while copying image (. png)

Java从套接字读取

java - 读取大文件(例如非常大的文本文档)的最佳方式

java - 即使有 try catch 也会获取 RuntimeException

java - java中父类中返回子类的方法