简体   繁体   中英

Reading a particular line from a text file in Java

What is the most efficient way to extract specific line numbers of data from a text file? For example, if I use the Scanner to parse a file, do I first have to create an array with a length matching the total number of lines in the text file?

If a text file has 30 lines and I only want to work with lines 3, 8, and 12, is there a way to specifically only read those lines?

here is how you do it:

import java.io.*;

public class Test {
public static void main(String [] args) {

    // The name of the file to open.
    String fileName = "temp.txt";
    int counter = 0;

    // This will reference one line at a time
    String line = null;
    FileReader fileReader = null;

    try {
        // FileReader reads text files in the default encoding.
        fileReader = 
            new FileReader(fileName);

        // Always wrap FileReader in BufferedReader.
        BufferedReader bufferedReader = 
            new BufferedReader(fileReader);

        while((line = bufferedReader.readLine()) != null) {
            counter++;
            if(counter == 3 || counter == 8 || counter == 12)
            {
               // do your code
            }
        }   

    }
    catch(FileNotFoundException ex) {
        System.out.println(
            "Unable to open file '" + 
            fileName + "'");                
    }
    catch(IOException ex) {
        System.out.println(
            "Error reading file '" 
            + fileName + "'");                  
        // Or we could just do this: 
        // ex.printStackTrace();
    }
    finally
    {
        if(fileReader != null){
           // Always close files.
           bufferedReader.close();            
        }
    }
}
}

here is what you can do. (it is only part of your program, not exactly your program)

int counter 0 =;
BufferedReader br = new BufferedReader(new FileReader(file));  
String line;  
while ((line = br.readLine()) != null) {  
   // process the line.  
   counter++;

   switch(counter){  
    case 3:  
       \\ do your code for line no 3  
       break;  
    case 8:  
       \\ do your code for line no 8  
       break;  
    case 12:  
       \\ do your code for line no 12  
       break;  
   }  
}    
br.close();  

Try this

try
    {
        String sCurrentLine;
        br = new BufferedReader(new FileReader("File_Path"));
        int counter = 0;
        while ((sCurrentLine = br.readLine()) != null)
        {
            counter++;
            if (counter == 3 || counter == 8 || counter == 12)
            {
                System.out.println("" + br.read());
                if (counter == 12)
                    break;
            }
        }
    }
    catch (IOException e)
    {
        System.out.println("Exception " + e);

    }
    finally
    {
      try
        {
           if (br != null)
           {
               br.close();
           }
        }
        catch (IOException ex)
        {
           ex.printStackTrace();
        }
     }

Regarding the last question: According to this answer , Java 8 enables you to extract specific lines from a file. Examples are provided in that answer.

here you can find the the solution for reading a file according to the line no. This site is the best It contain this code in all the programming languages. And I have given you the java code.

https://www.rosettacode.org/wiki/Read_a_specific_line_from_a_file#Java

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