资源下载阅读到单独的阵列中的每个线 [英] Java Read Each Line Into Separate Array
本文介绍了资源下载阅读到单独的阵列中的每个线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个文本文件1000行数据,我想每一行都将自己的浮动[]。
I have 1,000 lines of data in a text file and I would like each line to be its own float [].
1,1,1,1,1,1
2,2,2,2,2,2
3,3,3,3,3,3
将导致:
float[0] = {1,1,1,1,1,1}
float[1] = {2,2,2,2,2,2}
float[2] = {3,3,3,3,3,3}
这可能吗?我只能找到加载整个文件到一个数组的例子。我想所有的硬编码的数组,但超过〜65,000字节字符的限制
Is this possible? I could only find examples of loading an entire file into an array. I tried hardcoding all the arrays, but exceeded the byte character limit of ~65,000
推荐答案
请尝试以下操作:
// this list will store all the created arrays
List<float[]> arrays = new ArrayList<float[]>();
// use a BufferedReader to get the handy readLine() function
BufferedReader reader = new BufferedReader(new FileReader("myfile.txt"));
// this reads in all the lines. If you only want the first thousand, just
// replace these loop conditions with a regular counter variable
for (String line = reader.readLine(); line != null; line = reader.readLine()) {
String[] floatStrings = line.split(",");
float[] floats = new float[floatStrings.length];
for (int i = 0; i < floats.length; ++i) {
floats[i] = Float.parseFloat(floatStrings[i]);
}
arrays.add(floats);
}
请注意,我没有添加任何异常处理(的readLine(),例如,抛出IOException异常)。
Note that I haven't added any exception handling (readLine(), for example, throws IOException).
这篇关于资源下载阅读到单独的阵列中的每个线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文