java - 打开数据文件、追加行和分割数据

标签 java

我正在启动一个项目。我需要打开一个文件并将所有数据存储到一个数组中。

我尝试过使用数组列表,但它目前用“,”分割每一行并将多个点存储到单个索引中,而我每个索引需要一个点。
我尝试将 ArrayList 字符串更改为数字,但它不起作用。

这是数据文件的一部分

150 4  
5.1 3.5 1.4 0.2
4.9 3 1.4 0.2
4.7 3.2 1.3 0.2
4.6 3.1 1.5 0.2

这是我当前的代码

    public static void main(String a[]){
        StringBuilder sb = new StringBuilder();
        String filename = "";
        String strLine = "";
        double arr[]; 

        Scanner myObj = new Scanner(System.in);  // Create a Scanner object
        System.out.println(filename);

        String fn = myObj.nextLine();

        List<String> list = new ArrayList<String>();
        try {
             BufferedReader br = new BufferedReader(new FileReader("./phase1/" + fn));
              while (strLine != null)
               {
                strLine = br.readLine();
                sb.append(strLine);
                sb.append(System.lineSeparator());
                if (strLine==null)
                   break;
                list.add(strLine);
            }
         System.out.println(Arrays.toString(list.toArray()));

             br.close();
        } catch (FileNotFoundException e) {
            System.err.println("File not found");
        } catch (IOException e) {
            System.err.println("Unable to read the file.");
        }



        // Removing the first line with the number of elements and dimensions 
      //  list.remove(0); 

        // Checking the the element was removed 
     //   System.out.println(list.get(0));

        // Printing elements one by one 
       for (int i=0; i<list.size(); i++) 
            System.out.print(list.get(i)+" "); 

      //    System.out.println("\n");
      //  System.out.print(list.get(0));



     }

我当前的输出 [150 4 , 5.1 3.5 1.4 0.2, 4.9 3 1.4 0.2, 4.7 3.2 1.3 0.2, 4.6 3.1 1.5 0.2, 5 3.6 1.4 0.2,]

索引(0) = 150 4

预期输出 索引(0) = 150 索引(1) = 4

最佳答案

我假设您想根据文件内容创建一个数字数组。您提供的样本数字是 float 。下面是 Java 代码,它逐行读取您提供的示例文件,将每一行拆分为单独的数字,并将字符串转换为 Float。每个Float都被添加到ArrayList中,最后ArrayList被转换为Float数组。

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

public class NumArray {

    public static void main(String[] args) {
        if (args.length < 1) {
            System.out.println("Usage: java NumArray <file>");
        }
        else {
            try (BufferedReader br = new BufferedReader(new FileReader(args[0]))) {
                ArrayList<Float> numbers = new ArrayList<>();
                String line = br.readLine();
                while (line != null) {
                    String[] parts = line.split(" ");
                    for (String part : parts) {
                        numbers.add(Float.valueOf(part)); // throws java.lang.NumberFormatException
                    }
                    line = br.readLine();
                }
                Float[] numArray = new Float[numbers.size()];
                numbers.toArray(numArray);
                for (Float number : numbers) {
                    System.out.print(number);
                    System.out.print(" ");
                }
                System.out.println();
            }
            catch (IOException xIo) {
                xIo.printStackTrace();
            }
        }
    }
}

输出...

150.0 4.0 5.1 3.5 1.4 0.2 4.9 3.0 1.4 0.2 4.7 3.2 1.3 0.2 4.6 3.1 1.5 0.2

请注意我的代码中的注释:抛出 java.lang.NumberFormatException
如果文件包含无法转换为Float的字符,程序将崩溃。

关于java - 打开数据文件、追加行和分割数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57946953/

相关文章:

java - 我可以使用 JavaFX 向我的程序添加更多开始菜单项吗?

java - 收到异常 ClassNotFoundException 并且不知道为什么

java - Hibernate 不改变现有的列是一个缺点吗?

java - iBatis java.lang.OutOfMemoryError : Java heap space 错误

java - 如何在 jframe 标题栏上显示当前日期?

exception - 与其他库一起创建库 jar

java - Intellij 如何为项目的所有运行配置禁用类路径文件模式?

java - Discord 机器人在 if/else 语句中遍历两条路径?

java - 从表中删除选择监听器

java - 如何使服务器端域模型可用于客户端 Web 浏览器?