简体   繁体   English

Java I / O读取多行文件并存储为arraylist

[英]java i/o read in multi line file and store as arraylist

I want to read in a simple text file and save it as an ArrayList. 我想读取一个简单的文本文件并将其另存为ArrayList。 The file looks like so 该文件看起来像这样

0.2253 
0.808 
0.132 
0.341 

4.18546
8.65418
1.45535
0.341

and so on...

They will always be four lines and they will always be delimited by a blank space. 它们将始终为四行,并且始终以空格分隔。 Somehow I'd like to use that as the break point to begin the new array and pick back up with the first new number as index zero. 我想以某种方式将其用作断点,以开始新的数组并以第一个新数字作为索引零进行选择。

The numbers must be stored as strings. 数字必须存储为字符串。

I want to assign this data to an ArrayList such that 我想将此数据分配给ArrayList这样

[0][0] = 0.2253
[0][1] = 0.808
[0][2] = 0.132
[0][3] = 0.341

[1][0] = 4.18546
[1][1] = 8.65418
[1][2] = 1.45535
[1][3] = 0.341

how can I do this using the structures of java i/o? 我如何使用Java I / O的结构来做到这一点?

java io arraylist java io arraylist

So far I have this.. 到目前为止,我有这个。

    //array list data struc
    ArrayList<String> array_list = new ArrayList<String>();

    String component_doc = "/home/joao/document.txt"

    Scanner inFile = null;
    try 
    {
        inFile = new Scanner(new File(component_doc));
    } 
    catch (FileNotFoundException e) 
    {
        e.printStackTrace();
    }

    while(inFile.hasNextLine())
    {
        array_list.add(inFile.nextLine());
    }

This is a way to read in those lines and put them in an array list. 这是一种读取这些行并将其放入数组列表的方法。

import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;

protected static ArrayList<String> yourArrayList = new ArrayList<String>();
String fileName = "C:\\Users\\myComputer\\Desktop\\test_file.txt";
    try
    {
        List<String> lines = Files.readAllLines(Paths.get(fileName), Charset.defaultCharset());
        for (int i = 0; i < lines.size(); i++)
        {
            yourArrayList.add(lines.get(i).toString());
        }
    }catch(IOException io)
    {
        io.printStackTrace();
    }

Just a hint: 只是一个提示:

ArrayList<ArrayList<String>> array_list = new ArrayList<ArrayList<String>>(); //Create 2D arrayList

// open file and do all previous stuff 

ArrayList<String> item = new ArrayList<String>(); // Create 1D ArrayList 

while(inFile.hasNextLine())
     String str = nextLine(); 
     if (str.trim().length() == 0) {  // is blank line?
         if (item.size() > 0) array_list.add(item); // Store 1D arrayList into 2D 
         item = new ArrayList<String>();  // Create new 1D arrayList
     }
     else { 
          item.add(strLine); // Add each element to 1D arrayList
     } 
} 

Remark : This is an instance of an ArrayList , so you can't access them using array_list[X][Y] . 备注 :这是ArrayList的实例,因此您不能使用array_list[X][Y]来访问它们。 You could try the method .toArray() to convert it to a true 2-D array. 您可以尝试.toArray()方法将其转换为真正的二维数组。

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

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