简体   繁体   English

从文件中读取元素,并将其作为对象存储在ArrayList中

[英]Read elements from a file and store them in ArrayList as Objects

I have a file contains (string, int, floating, char) 我有一个文件包含(字符串,整数,浮点数,字符)

12 12

1.8 1.8

a 一种

John 约翰

23.5 23.5

I want to read them an store them in an ArrayList. 我想读取它们并将它们存储在ArrayList中。 My ArrayList type is object. 我的ArrayList类型是object。 But, still I get exceptions when I run it. 但是,当我运行它时,仍然会出现异常。

    public static void main(String [] args) throws IOException{


    try{
            FileReader a=new FileReader("src/SET_A.txt");
            Scanner b=new Scanner(a);
            ArrayList<Object> c=new ArrayList<Object>();
            while(b.hasNextLine())
                {
                    Object d=b.nextInt();
                    c.add(d);
                 }
            System.out.println("The content of arraylist is: " + a);
    }
    catch(Exception exception)
    {
        exception.printStackTrace();
    }
    }

Are you displaying the File or the Contents of ArrayList? 您正在显示文件还是ArrayList的内容? You should be displaying C and not A. If not, please let us know what is the Exception. 您应该显示C而不是A。如果不是,请告诉我们什么是例外。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从文件中读取并将其存储在对象的 ArrayList 中? - How to read from files and store it in ArrayList of objects? 读取大文件并将其存储在pojo中并在arraylist中反过来 - To read large file and store them in pojo and inturn in arraylist 如何从 String 中过滤掉 Unique 元素并将它们存储到 ArrayList - How filter out Unique elements from a String and store them into an ArrayList 从文件读取和写入对象的ArrayList的问题 - Problems with read and write an ArrayList of Objects from a file Java从文件中读取内容到ArrayList(对象) - Java read contents from file into ArrayList (Objects) 我正在尝试读取文本文件并将其存储在对象数组列表中 - I'm trying to read a text file and store it in an arraylist of objects Java,从文件中读取对象并将其添加到数组列表 - Java, Reading objects from a file and adding them to an arraylist 如何使用构造函数初始化每个数组元素时从文件读取和存储对象数组 - How to read from file and store array of objects while initializing each array elements using a constructor 如何创建文件以存储ArrayList Java中的对象 - How to create a file to store an Objects from an ArrayList Java Java将文件读入对象的数组列表并返回该数组列表 - Java read a file into an arraylist of objects and return that arraylist
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM