【问题标题】:Arrays and Files in JavaJava中的数组和文件
【发布时间】:2017-11-08 02:47:41
【问题描述】:

所以我最近从 Python 转向学习 Java,但我无法让二维数组工作。我设法从文件中读取数据并将文件中的文本附加到数组中。但我只设法将文件的内容作为一项写入数组。精通 Python 意味着我会认为自己非常了解构造的工作原理,只是我正在努力适应在执行此操作时使用的库/方法。

这是我要逐行拆分成数组的文本文件的内容。

    Mildred Bush 45 65 45 67 65 into [[Mildred Bush],[45],[65],[67],[65]]
    Fred Snooks 23 43 54 23 76               etc.
    Morvern Callar 65 45 34 87 76            etc.
    Colin Powell 34 54 99 67 87
    Tony Blair 67 76 54 22 12
    Peter Gregor 99 99 99 99 99

没有数组工作的代码。

    public void Process() {
    String filename;
    filename = "Details.txt";


    FileReader fileReader = null;
    BufferedReader bufferedReader = null;

    List<String> alist = new ArrayList<String>();


    try{

        fileReader = new FileReader(filename);
        bufferedReader = new BufferedReader(fileReader); 

        String nextLine = bufferedReader.readLine();

        while ((nextLine = bufferedReader.readLine()) != null) {
            System.out.println(nextLine);
            nextLine = bufferedReader.readLine();
            nextLine.split(",");
            System.out.print(nextLine);
            alist.add(nextLine);
        }
    }
    catch (IOException e){
        System.out.println("Error reading from file: " + e);
    }
    System.out.print(alist);

}

所以我打算这样做的方法是使用 .split 两次,分别用“\n”和“”分割。创建一个二维数组。但我无法让它工作,我想知道是否有不同的库/方法可用于将文件的每一行读入二维数组。我研究了使用 ArrayList 模块(?),但没有成功分割每一行。

【问题讨论】:

  • 嗯,真的,你应该有一个包装属性的对象,然后你把它添加到你的List。解析你的String 并不难,除了分隔符也会拆分名称,所以你必须注意这一点;)。我会先探索nextLine.split(" "); 的结果
  • @MadProgrammer 是的,所以我已经尝试将 .split 与 nextLine 一起使用,但我被空指针异常捕获。因此,为什么我被卡住了
  • while (nextLine != null){ 应该是 while ((nextLine = bufferedReader.readLine()) != null){ 否则您不会更新该值。 String#split 将通过分隔符将字符串拆分为数组。也许您应该提供一个尝试的示例
  • 所以我又试了一次,但我一直受到同样的异常的打击。更新的尝试在上面
  • 由于您的数据似乎用空格分隔,但您似乎试图用“,”分隔,那么您提供的信息是错误的,或者您使用了错误的分隔符

标签: java arrays file


【解决方案1】:

所以,我拿了你的数据:

Mildred Bush 45 65 45 67 65
Fred Snooks 23 43 54 23 76
Morvern Callar 65 45 34 87 76
Colin Powell 34 54 99 67 87
Tony Blair 67 76 54 22 12
Peter Gregor 99 99 99 99 99

修改你的代码如下:

String filename;
filename = "Details.txt";

List<String[]> alist = new ArrayList<String[]>();

try (BufferedReader bufferedReader = new BufferedReader(new FileReader(filename))) {

    String nextLine = null;
    while ((nextLine = bufferedReader.readLine()) != null) {
        String[] parts = nextLine.split(" ");
        String[] elements = new String[parts.length - 1];
        elements[0] = parts[0] + " " + parts[1];
        System.arraycopy(parts, 2, elements, 1, elements.length - 1);
        alist.add(elements);
    }
} catch (IOException e) {
    System.out.println("Error reading from file: " + e);
}
for (String[] values : alist) {
    System.out.println(Arrays.asList(values));
}

然后打印出来:

[Mildred Bush, 45, 65, 45, 67, 65]
[Fred Snooks, 23, 43, 54, 23, 76]
[Morvern Callar, 65, 45, 34, 87, 76]
[Colin Powell, 34, 54, 99, 67, 87]
[Tony Blair, 67, 76, 54, 22, 12]
[Peter Gregor, 99, 99, 99, 99, 99]

正如我之前所说,从数据的Object 表示开始并将数据读入其中会更容易,但这就是我。

这个例子对数据的状态做了很多假设,所以要小心

查看try with resources 了解有关我使用的try (...) {...} catch {...} 语句的更多详细信息

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-11
    • 2014-11-30
    • 1970-01-01
    • 2014-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多