简体   繁体   English

如何将数据从txt文件复制到数组

[英]How to copy data from txt file to array

I am doing a project that requires me to copy all the information from txt file into array. 我正在做一个项目,要求我将txt文件中的所有信息复制到数组中。 The content of the txt is listed below. txt的内容在下面列出。 What i want here is that i want to grab all the names of goods and description into its arrays respectively. 我在这里想要的是我想将所有商品名称和描述分别捕获到其数组中。

GoodTitle        Description            
    Gold       The shiny stuff
    Wheat   What wheaties are made of
    Wood    To make more ships
    Spices  To disguise the taste of rotten food
    Tobacco Smoko time
    Coal    To make them steam ships go
    Coffee  Wakes you up
    Tea Calms you down

What I have done so far: 到目前为止,我所做的是:

public void openFile()
    {
        ArrayList <String> ShippingTokens = new ArrayList<String>();
        try{
            FileInputStream fstream = new FileInputStream("D://Shipping.txt");
            // Use DataInputStream to read binary NOT text.
                    BufferedReader br = new BufferedReader (new InputStreamReader (fstream));
            String strline;
            while ((strline = br.readLine()) != null){ 
                strline = strline.trim();

            if ((strline.length()!=0)) {
                String[] Shippings = strline.split("//s+");
                ShippingTokens.add(Shippings[TOKEN_COLUMN]);
            }

        }

        for (String s : ShippingTokens) {
            System.out.println(s);
        }

        in.close();
        } catch (Exception e){
            System.err.println("Error");
        }

    }

First of all, Java SE 7 really simplified working with files, so I suggest you using it. 首先,Java SE 7确实简化了文件的处理,因此建议您使用它。

try(BufferedReader reader = Files.newBufferedReader(Paths.get("D://Shipping.txt"), StandardCharsets.UTF_8)) { 
    List<String> lines = Files.readAllLines(reader, StandardCharsets.UTF_8);
    for(String line : lines) {
         String[] shipping = line.split("//s+");
         //now do whatever you want with the values
    }
}

Please ensure you defined TOKEN_COLUMN in your class. 请确保您在课堂上定义了TOKEN_COLUMN。

 ShippingTokens.add(Shippings[TOKEN_COLUMN]);

if your TOKEN_COLUMN value is 0 then your output will be as below. 如果您的TOKEN_COLUMN value is 0那么您的输出将如下所示。

GoodTitle   
Gold
Wheat
Wood
Spices
Tobacco
Coal
Coffee
Tea

One possible solution is to first read the first word of the string, and store that in the array. 一种可能的解决方案是先读取字符串的第一个单词,然后将其存储在数组中。 Then you would probably want to store the rest of the line, which could be done in various way. 然后,您可能希望存储该行的其余部分,这可以通过各种方式来完成。 One possible solution is to read the whole line, store the first word as the GoodTitle, remove the first word and store the rest of the string as the description, I am not quite sure how BufferedReaders work in Java. 一种可能的解决方案是读取整行,将第一个单词存储为GoodTitle,删除第一个单词,并将其余字符串存储为描述,但我不确定BufferedReaders在Java中的工作方式。

You might want to consider using a Scanner instead. 您可能需要考虑使用扫描仪。 http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html

Which comes with methods such as next() and nextLine(), which probably would ease your solution. 带有诸如next()和nextLine()之类的方法,这可能会简化您的解决方案。

Are you sure that you want to store this in an array? 您确定要将此存储在数组中吗? It would probably make more sense to store the information in a HashMap with the GoodTitle as the key. 将信息以GoodTitle作为键存储在HashMap中可能更有意义。

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

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