简体   繁体   English

Java将每一行读入单独的数组

[英]Java Read Each Line Into Separate Array

I have 1,000 lines of data in a text file and I would like each line to be its own float [].我在一个文本文件中有 1,000 行数据,我希望每一行都是它自己的浮点数 []。

1,1,1,1,1,1
2,2,2,2,2,2
3,3,3,3,3,3

Would result in:会导致:

 float[0] = {1,1,1,1,1,1}
 float[1] = {2,2,2,2,2,2}
 float[2] = {3,3,3,3,3,3}

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我尝试对所有数组进行硬编码,但超出了 ~65,000 的字节字符限制

Try the following:请尝试以下操作:

// 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);
}

Note that I haven't added any exception handling (readLine(), for example, throws IOException).请注意,我没有添加任何异常处理(例如,readLine() 抛出 IOException)。

  • use a LineIterator to read each line without loading the whole file使用LineIterator读取每一行而不加载整个文件

  • for each line, use a regular expression to extract figures like (\\d\\.)+ and iterator over the matches found with methods like find() and group()对于每一行,使用正则表达式提取像(\\d\\.)+ 这样的数字,并在使用find()group()等方法找到的匹配项上进行迭代

 <body> <pre> package FirstPrac; import java.io.FileReader; public class Check { public static void main(String[] args) { readingfile(); } public static void readingfile() { try { FileReader read = new FileReader("D:\\\\JavaWkspace\\\\numbers.txt"); int index; String nums1 = ""; while ((index = read.read()) != -1) { if (((char) index) != '\\n') { nums1 += String.valueOf((char) index); } } System.out.println("Problem statement: Print out the greatest number on each line:\\n" + nums1); String f = nums1.substring(0, 14); String s = nums1.substring(15, 29); String t = nums1.substring(30); String[] fs = f.split(","); int size = fs.length; int[] arr = new int[size]; for (int i = 0; i < size; i++) { arr[i] = Integer.parseInt(fs[i]); } int max = arr[0]; for (int i = 0; i < arr.length; i++) { if (max < arr[i]) { max = arr[i]; } } System.out.println("\\nGreatest number in the first line is:" + (max)); String[] sstr = s.split(","); int size2 = sstr.length; int[] arr2 = new int[size2]; for (int i = 0; i < size2; i++) { arr2[i] = Integer.parseInt(sstr[i]); } int max2 = arr2[0]; for (int i = 0; i < arr2.length; i++) { if (max2 < arr2[i]) { max2 = arr2[i]; } } System.out.println("\\nGreatest number in the second line is:" + (max2)); String[] str3 = t.split(","); int size3 = str3.length; int[] arr3 = new int[size3]; for (int i = 0; i < size3; i++) { arr3[i] = Integer.parseInt(str3[i]); } int max3 = arr3[0]; for (int i = 0; i < arr3.length; i++) { if (max3 < arr3[i]) { max3 = arr3[i]; } } System.out.println("\\nGreatest number in the third line is:" + (max3)); read.close(); } catch (Exception e) { System.out.println(e); } } } </pre> </body>

Loop over the line-delimited contents of the file with .split("\\n") and then cast each result as float array.使用 .split("\\n") 循环遍历文件的行分隔内容,然后将每个结果转换为浮点数组。 Here's how to convert the string into a float for you => http://www.devdaily.com/java/edu/qanda/pjqa00013.shtml这是将字符串转换为浮点数的方法 => http://www.devdaily.com/java/edu/qanda/pjqa00013.shtml

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

相关问题 Java将每一行读入单独的ArrayList(字符串) - Java Read Each Line Into Separate ArrayList (String) 如何在Java中一行一行地读取文本文件,并分别分隔每一行的内容? - How do you read a text file, line by line, and separate contents of each line in Java? 在Java中,我需要读取一个文本文件并将每一行放在单独的数组中。 但是每次阅读文本文件时,我都无法分开 - In Java I need to read a text file and put each line in a separate array. But every time I read the text file I cannot split the lines Java:如何读取文本文件的每一行并将每一行设置为数组元素? - Java: How do you read each line of a text file and setting each line as an array element? Java:读取文件并将一行拆分为单独的字符串 - Java: Read a File & Split a line into separate string 尝试读取与文件分开的每一行代码 - Trying to read each line of code separate from a file 使用 java 中的 MappedByteBuffer 读取文件的每一行 - Read each line of file using MappedByteBuffer in java 逐行读取,在java中输入数组 - read Line by Line, in java feed in to an array 如何在每行Java上读取带有数字的文件 - How to read a file with a number on each line java 为什么每次读取文件行时我的对象都没有存储到数组中? 爪哇 - Why isn't my object storing into an array each time I read a line of a file? Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM