java的I / O多行文件并存储为ArrayList的阅读 [英] java i/o read in multi line file and store as arraylist
问题描述
我想在一个简单的文本文件中读取并保存为一个ArrayList。该文件看起来像这样
0.2253
0.808
0.132
0.3414.18546
8.65418
1.45535
0.341等等...
他们将永远是四行,他们将始终由空格分隔。不知怎的,我想使用它作为破发点,开始新的阵列和再爬起来的第一个新的数字作为指标为零。
数字必须存储为字符串。
我想这个数据分配到一个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
我怎么可以这样使用Java的结构I / O?
Java的IO的ArrayList
到目前为止,我有这个..
//数组列表数据STRUC
ArrayList的<串GT; array_list =新的ArrayList<串GT;(); 字符串component_doc =/home/joao/document.txt 扫描仪INFILE = NULL;
尝试
{
INFILE =新的扫描仪(新File(component_doc));
}
赶上(FileNotFoundException异常E)
{
e.printStackTrace();
} 而(inFile.hasNextLine())
{
array_list.add(inFile.nextLine());
}
这是在那些线条阅读并把它们放在一个数组列表的方式。
进口java.nio.charset.Charset中;
进口java.nio.file.Files;
进口java.nio.file.Paths;保护静态的ArrayList<串GT; yourArrayList =新的ArrayList<串GT;();
字符串文件名=C:\\\\ \\\\用户\\\\我的电脑桌面\\\\ test_file.txt
尝试
{
清单<串GT;线= Files.readAllLines(Paths.get(文件名),Charset.defaultCharset());
的for(int i = 0; I< lines.size();我++)
{
yourArrayList.add(lines.get(ⅰ)的ToString());
}
}赶上(IOException异常IO)
{
io.printStackTrace();
}
I want to read in a simple text file and save it as an 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
[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 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();
}
这篇关于java的I / O多行文件并存储为ArrayList的阅读的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!