【问题标题】:Java Read Each Line Into Separate ArrayJava 将每一行读入单独的数组
【发布时间】:2012-09-13 11:04:03
【问题描述】:

我在一个文本文件中有 1,000 行数据,我希望每一行都是自己的 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 的字节字符限制

【问题讨论】:

  • 每行的浮点数是否相同?
  • 是的,每一行的浮点数相同。

标签: java arrays io


【解决方案1】:

使用 .split("\n") 遍历文件的行分隔内容,然后将每个结果转换为浮点数组。以下是如何将字符串转换为浮点数 => http://www.devdaily.com/java/edu/qanda/pjqa00013.shtml

【讨论】:

  • -1,这根本不是答案。你需要一些细节。任何细节。
  • 这就是我刚才所说的。
【解决方案2】:
  • 使用LineIterator 读取每一行而不加载整个文件

  • 对于每一行,使用正则表达式提取像 (\d\.)+ 这样的数字,并使用像 find()这样的方法找到的匹配项进行迭代> 和 group()

【讨论】:

    【解决方案3】:

    尝试以下方法:

    // 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)。

    【讨论】:

      【解决方案4】:

      <body>
      <pre>
      
      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>

      【讨论】:

      • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-10
      • 2021-08-13
      • 2016-06-16
      • 2017-11-20
      • 1970-01-01
      • 2016-08-06
      相关资源
      最近更新 更多