繁体   English   中英

利用扫描仪填充对象数组列表

[英]Utilize scanners to fill object arraylist

我对编程还不陌生,最近我写了一些东西来利用扫描程序类从文本文件填充对象数组。 本质上,我可以重写此文本文件或添加新信息,而不必更改代码。 我想我的问题是:这样做有更简单/更优选的方法吗? 我正在尝试学习编码的细微差别。

import java.io.*;
import java.util.*;
public class ImportTest {
public static void main(String[] args) throws IOException 
{
    Scanner s = null;
    Scanner k = null;
    ArrayList myList = new ArrayList<String>();
    ArrayList myList2 = new ArrayList<String>();
    ArrayList myList3 = new ArrayList<Student>();
    try 
    {
        s = new Scanner(new BufferedReader(new FileReader("testMe.txt")));
        while (s.hasNext()) 
        {
            myList.add(s.nextLine()); 
        }
    } 
    finally 
    {
        if (s != null) 
        {
            s.close();
        }
    }
    System.out.println("My List 1:");
    for(int i=0; i<myList.size(); i++)
    {
        System.out.println(i+". "+myList.get(i));
    }
    for(int x=0; x<myList.size(); x++)
    {
        try 
        {
            k = new Scanner(myList.get(x).toString());
            while (k.hasNext()) 
            {
                myList2.add(k.next()); 
            }
        } 
        finally 
        {
            if (k != null) 
            {
                k.close();
            }
        }
        String name;
        int age;
        double money;
        name=myList2.get(0).toString();
        age=Integer.parseInt(myList2.get(1).toString());
        money=Double.parseDouble(myList2.get(2).toString());
        Student myStudent=new Student(name, age, money);
        myList3.add(myStudent);
        myList2.clear();
    }
    System.out.println("Test of list object: ");
    for(int i=0; i<myList3.size(); i++)
    {
        System.out.println(i+". "+myList3.get(i).toString());
    }
}
}

我会逐行读取文件并直接解析每一行。 这样,您就不需要3个列表,2个扫描程序和多个迭代:

String line = "";
BufferedReader br = new BufferedReader(new FileReader("test.txt"));
ArrayList<Student> students = new ArrayList<Student>();
while( (line = br.readLine()) != null)
{
   String[] tmp = line.split("\\s+");  //split line by spaces

   //this needs bounds & error checking etc.
   students.add(new Student(tmp[0], Integer.parseInt(tmp[1]), Double.parseDouble(tmp[2])));
}

在Java 7中,您可以使用新文件功能一次读取所有行:

List<String> allLines = Files.readAllLines("test.txt", Charset.defaultCharset());

不要忘记关闭阅读器或使用try-with-resources (自Java 1.7起)

如果我错了,请纠正我, testMe.txt文件包含Student信息,它们是nameagemoney和您想要读取这些值的信息。

最好的方法是,应该在ObjectOutputStream的帮助下将Student对象序列化到testMe.txt中。 同样,您也可以使用ObjectInputStream读取这些值,这样,您就可以获取Student对象本身(无需绑定String )。

如果确实要将数据序列化到文件中,则应该以某种预定义的格式存储数据,例如以逗号(,)或分号(;)分隔。

例如 -

emp1, 24, 20000
emp emp2, 25, 24000
emp3, 26, 26000

在这种情况下,在读取字符串时,您可以使用分隔符将其拆分并获取实际信息。

程式码片段

List<Student> students = new ArrayList<Student>();
...
try(scanner = new Scanner(new BufferedReader(new FileReader("testMe.txt")))){        
    while (scanner.hasNext()){
        String data[] = scanner.nextLine().split(",");
        Student student = new Student(data[0],data[1],data[2]);
        students.add(student);
    }
} 

Try-with-resource将自动处理资源,您不需要显式关闭它。 从1.7开始,此功能在Java中可用。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM