使用java从文本文件中读取数据行

发布于 2024-10-09 10:38:50 字数 841 浏览 3 评论 0原文

我有一个包含 x 行数的文本文件。每行保存一个整数。当用户单击按钮时,将通过 actionlistener 执行操作,其中应列出文本文件中显示的所有值。然而,现在我将 linenum 设置为 10,这意味着我已经告诉代码只处理文本文件的 10 行。因此,如果我的文本文件只有 3 行/行数据...它将列出这些行,而对于其余 7 行,它将输出“null”。

我记得有一种方法可以使用省略号让程序知道您不知道确切的值,但最后它会根据给定的信息计算它。我给定的信息将是带有数字(数据)的行数。

下面是部分代码。

private class thehandler implements ActionListener{     
public void actionPerformed(ActionEvent event){
BufferedReader inputFile=null;          
try {
    FileReader freader =new FileReader("Data.txt");
    inputFile = new BufferedReader(freader); 

    String MAP = "";
    int linenum=10;
    while(linenum > 0)
        { 
    linenum=linenum-1; 
    MAP = inputFile.readLine();//read the next line until the specfic line is found 

    System.out.println(MAP);
    }

    } catch( Exception y ) {    y.printStackTrace();    } 

}}  

I have a text file with x amount of lines. each line holds a integer number. When the user clicks a button, an action is performed via actionlistener where it should list all the values as displayed on the text file. However, right now I have linenum set to 10 implying I already told the code that just work with 10 lines of the text file. So, if my text file has only 3 rows/lines of data...it will list those lines and for rest of the other 7 lines, it will spit out "null".

I recall there is a way to use ellipsis to let the program know that you don't know the exact value but at the end it calculates it based on the given information. Where my given information will the number of lines with numbers(data).

Below is part of the code.

private class thehandler implements ActionListener{     
public void actionPerformed(ActionEvent event){
BufferedReader inputFile=null;          
try {
    FileReader freader =new FileReader("Data.txt");
    inputFile = new BufferedReader(freader); 

    String MAP = "";
    int linenum=10;
    while(linenum > 0)
        { 
    linenum=linenum-1; 
    MAP = inputFile.readLine();//read the next line until the specfic line is found 

    System.out.println(MAP);
    }

    } catch( Exception y ) {    y.printStackTrace();    } 

}}  

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

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

发布评论

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

评论(3

知足的幸福 2024-10-16 10:38:50

只需将 linenum > 代替即可0 下一行(MAP = inputFile.readLine()) != null
并删除接下来的行,linenum=linenum-1;
MAP = inputFile.readLine();
下次谷歌搜索可能会有所帮助+)
最后一行的空值不会被打印,因为它将该行设置为当前行并将其与空值进行比较,因此如果最后一行为空,则不会打印它,那么 10 行限制又如何呢?你可以很容易地做到这一点,你只需将一个索引添加到 for 循环并索引并使用 && 进行检查即可。如果 i 小于 10

just put instead of linenum > 0 the next line (MAP = inputFile.readLine()) != null
and delete the next lines, linenum=linenum-1;
MAP = inputFile.readLine();
and next time a bit of googling might help +)
The null value of the last line would not be printed because it sets the line to be the current line and compares it with the null value so if the last line is null it will not print it and what about the 10 lines limit? you can do it easily you can just add an an index to the for loop and to index and to check with && if the i is lower then 10

依 靠 2024-10-16 10:38:50

测试从 BufferedReader.readLine() 返回的值,如果为空则停止循环,如下所示:

BufferedReader reader = new BufferedReader(new FileReader("Data.txt"));
try {
    for (String line; (line = reader.readLine()) != null;) {
        System.out.println(line);
    }
} finally {
    reader.close();
}

编辑:忘记了获取前 10 行的要求,您可以更改上面的代码以将输出放入列表中并返回列表,那么你可以通过这样的函数过滤它:

public List<String> takeFirst(int howMany, List<String> lines) {
 return lines.size() <= howMany ? lines : lines.subList(0, howMany);
}

如果文件很大,那么这当然会效率低下,如果这很重要,你最终会做类似的事情:

BufferedReader reader = new BufferedReader(new FileReader("Data.txt"));
try {
    int linesRead = 0;
    for (String line; (line = reader.readLine()) != null && linesRead < 10;) {
        System.out.println(line);
        linesRead += 1;
    }
} finally {
    reader.close();
}

这更丑陋,但只读取你需要的行。

Test the value that comes back from BufferedReader.readLine(), if it is null stop looping, like so:

BufferedReader reader = new BufferedReader(new FileReader("Data.txt"));
try {
    for (String line; (line = reader.readLine()) != null;) {
        System.out.println(line);
    }
} finally {
    reader.close();
}

EDIT: forgot the requirement to take first 10 lines, you can change above code to put output in a list and return the list, then you can filter it through a function like this:

public List<String> takeFirst(int howMany, List<String> lines) {
 return lines.size() <= howMany ? lines : lines.subList(0, howMany);
}

If the file is huge then this will be inefficient, of course, and if that matters you will end up doing something like:

BufferedReader reader = new BufferedReader(new FileReader("Data.txt"));
try {
    int linesRead = 0;
    for (String line; (line = reader.readLine()) != null && linesRead < 10;) {
        System.out.println(line);
        linesRead += 1;
    }
} finally {
    reader.close();
}

which is uglier but reads only the lines you need.

浸婚纱 2024-10-16 10:38:50

如果 MAP 的值为空,那么不打印 MAP 怎么样?

How about you don't print MAP if its value is null?

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