如何为多个文本文件创建一个 BufferedReader

发布于 2025-01-16 05:34:51 字数 928 浏览 4 评论 0原文

我正在尝试从三个单独的文件中读取文本。我想不出一种更干净的方法来仅使用一个 BufferedReader。有没有一种方法可以将文本保存到数组的空部分而不创建byte i = 0。 我是 Java 新手,所以感谢您提前回答。

        String unitsPath = "SI_system_units.txt";
    String namesPath = "SI_system_units_names.txt";
    String definitionsPath = "SI_system_definitions.txt";
    String line;
    byte i = 0;
    String[] siUnits = new String[100];
    String[] siNames = new String[100];
    String[] siDefinitions = new String[100];

    BufferedReader unitsBr = new BufferedReader(new FileReader(unitsPath));
    BufferedReader namesBr = new BufferedReader(new FileReader(namesPath));
    BufferedReader definitionsBr = new BufferedReader(new FileReader(definitionsPath));

    while ((line = unitsBr.readLine()) != null) {
        siUnits[i] = line.trim();
        siNames[i] = namesBr.readLine().trim();
        siDefinitions[i] = definitionsBr.readLine().trim();
        i++;
    }

I am trying to read text from three separate files. I couldn't think of a cleaner way to use only one BufferedReader. And is there a way to save text in to a empty part of an array without creating byte i = 0.
I am new to Java, So thanks for answering in advance.

        String unitsPath = "SI_system_units.txt";
    String namesPath = "SI_system_units_names.txt";
    String definitionsPath = "SI_system_definitions.txt";
    String line;
    byte i = 0;
    String[] siUnits = new String[100];
    String[] siNames = new String[100];
    String[] siDefinitions = new String[100];

    BufferedReader unitsBr = new BufferedReader(new FileReader(unitsPath));
    BufferedReader namesBr = new BufferedReader(new FileReader(namesPath));
    BufferedReader definitionsBr = new BufferedReader(new FileReader(definitionsPath));

    while ((line = unitsBr.readLine()) != null) {
        siUnits[i] = line.trim();
        siNames[i] = namesBr.readLine().trim();
        siDefinitions[i] = definitionsBr.readLine().trim();
        i++;
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

昵称有卵用 2025-01-23 05:34:51

我可以对多个文件使用单个 BufferedReader 吗?

不可以。单个 BufferedReader 只能操作单个文件。

有没有更简洁的方法来做到这一点?

我可以想出一些方法来使代码更简洁,但老实说,它们对于您的用途来说有点过分了正在做。

有没有办法将文本保存到数组的空部分而不创建索引指针(byte i = 0)?

没有。数组是固定长度的,您需要准确指定要放置值的位置。如果您想要一个动态大小的数组(根据需要增长的数组),您可以使用 ArrayList。

注意:数组可能是最基本的数据结构,您需要熟悉它们的使用。许多东西都是使用数组构建的,包括 ArrayList。 ArrayList 并没有消除对数组的需要。

import java.util.ArrayList;  // import statement is required

// ...snip...

String unitsPath = "SI_system_units.txt";
String namesPath = "SI_system_units_names.txt";
String definitionsPath = "SI_system_definitions.txt";
String line;
ArrayList<String> siUnits = new ArrayList<>();
ArrayList<String> siNames = new ArrayList<>();
ArrayList<String> siDefinitions = new ArrayList<>();

BufferedReader unitsBr = new BufferedReader(new FileReader(unitsPath));
BufferedReader namesBr = new BufferedReader(new FileReader(namesPath));
BufferedReader definitionsBr = new BufferedReader(new FileReader(definitionsPath));

while ((line = unitsBr.readLine()) != null) {
    siUnits.add(line.trim());
    siNames.add(namesBr.readLine().trim());
    siDefinitions.add(definitionsBr.readLine().trim());
}

// Don't forget to close your files :)
unitsBr.close();
namesBr.close();
definitionsBr.close();

如果需要的话,可以从 ArrayList 转到数组并返回,但这是另一个故事了。

Can I use a single BufferedReader for multiple files?

Not really. A single BufferedReader can only operate with a single file.

Is there a cleaner way to do this?

There are some ways I can think of some ways to make the code a bit cleaner, but they are honestly overkill for what you are doing.

Is there a way to save text in to an empty part of an array without creating an index pointer (byte i = 0)?

No. Arrays are fixed-length and you are required to specify exactly where you want the value to be placed. If you want a dynamically-sized array, one which grows as needed, you can use an ArrayList.

Note: Arrays are probably the most fundamental data structure and you need to be comfortable working with them. Many things are built using arrays including ArrayLists. ArrayLists do not remove the need for arrays.

import java.util.ArrayList;  // import statement is required

// ...snip...

String unitsPath = "SI_system_units.txt";
String namesPath = "SI_system_units_names.txt";
String definitionsPath = "SI_system_definitions.txt";
String line;
ArrayList<String> siUnits = new ArrayList<>();
ArrayList<String> siNames = new ArrayList<>();
ArrayList<String> siDefinitions = new ArrayList<>();

BufferedReader unitsBr = new BufferedReader(new FileReader(unitsPath));
BufferedReader namesBr = new BufferedReader(new FileReader(namesPath));
BufferedReader definitionsBr = new BufferedReader(new FileReader(definitionsPath));

while ((line = unitsBr.readLine()) != null) {
    siUnits.add(line.trim());
    siNames.add(namesBr.readLine().trim());
    siDefinitions.add(definitionsBr.readLine().trim());
}

// Don't forget to close your files :)
unitsBr.close();
namesBr.close();
definitionsBr.close();

It is possible to go from ArrayList to array and back if needed, but that is a story for another time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文