简体   繁体   中英

Reading lines of data from text files using java

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();    } 

}}  

just put instead of linenum > 0 the next line (MAP = inputFile.readLine()) != null and delete the next lines, linenum=linenum-1; MAP = inputFile.readLine(); 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

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.

如果它的值为null,您不打印MAP怎么办?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM