【问题标题】:trying to read txt file and converting them into object with different primitive试图读取 txt 文件并将它们转换为具有不同原语的对象
【发布时间】:2019-03-13 13:14:54
【问题描述】:

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

我试图在 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; 
    }
}

【问题讨论】:

  • 您能否提供您的文件样本,它们会以逗号分隔吗?
  • STUDENT 100383 JOHN MITCHELL STUDENT 100346 AMY CHING STUDENT 100373 MICHEAL SMITH STUDENT 100389 PAUL BAKER 没有逗号来分隔它,它使用一个新行来分隔它。学生告诉我,它是一个新学生,下面是 id,然后是名字的新行,然后是surename的新行

标签: java inputstream bufferedreader


【解决方案1】:

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

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));
         }
     }

使用 ObjectInputStream

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,约翰,米切尔

100346,艾米,清

【讨论】:

  • 谢谢你,如果我不能让输入流工作,我会用这个,但是输入流可以做到这一点
  • 当然你可以用inputStream来做。在java中操作文件I/O操作的方法不止一种,比如scanner、bufferedReader、inputStream。所有人都用不同的方式和方法做同样的事情。如果您想知道如何使用流对其进行编码但无法实现,请告诉我,我会尽力提供帮助。
  • 嘿,我试过编码,但还是不行。我尝试理解 objectinputstream 但是也没有用。
  • 如果你通过 ObjectOutputStream 用对象填充文件,使用 objectinputstream 是有意义的。但是,如果您是手动填充文件,为了能够使用 readLine(),bufferedReader 是更明智的方式
  • 我添加了 ObjectInputStream 的示例代码,只是为了看看它是如何工作的。
猜你喜欢
  • 2021-01-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-03
  • 2022-12-10
  • 1970-01-01
相关资源
最近更新 更多