Hi... I want to read only the first line of a file and print the line. I don't want to read the whole file. How can I do that ?

Here is how we normally read the file

import java.io.*;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Readfile {
    public void Readfile(){
        
    }
    public static void main(String argv[]) throws Exception {
        try {
            BufferedReader br = new BufferedReader(new FileReader("inputfile.txt"));
            String s=null;

            while((s=br.readLine())!=null && (s = s.trim()).length() > 0){
                 String f[] = s.split("\t");

               System.out.println(s);

            }  br.close();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Readfile.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
}

So what should I do to make it stop reading the whole file and just print the first line of a text file ?

Thanks

Recommended Answers

All 5 Replies

Use Scanner, not BufferedReader. Create the Scanner then use scanner.nextLine(). Print out the line that nextLine() returns, and you're done. You could use BufferedReader also and just read in bytes while its not = '\n' also, I suppose.

Use Scanner, not BufferedReader. Create the Scanner then use scanner.nextLine(). Print out the line that nextLine() returns, and you're done. You could use BufferedReader also and just read in bytes while its not = '\n' also, I suppose.

I did like below but its reading and printing the whole file:--

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;


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

        File file = new File("inputfile.txt");

        try {

            Scanner scan = new Scanner(file);
            while (scan.hasNextLine()) {
                String line = scan.nextLine();
                System.out.println(line);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}

Thanks

Use an if statement in your program instead of a while loop and your program will work as expected. You do know how to use while loops vs. how to use if statements, don't you? It seems like you don't.

Use "RandomAccessFile".
http://java.sun.com/j2se/1.4.2/docs/api/java/io/RandomAccessFile.html

private String readFirstLineOfFile(String fn){
        String lineData = "";

        try{
            RandomAccessFile inFile = new RandomAccessFile(fn,"rw");
            lineData = inFile.readLine();
            inFile.close();
        }//try
        catch(IOException ex){
            System.err.println(ex.getMessage());
        }//catch

        return lineData;

    }

the most simple way is:

add this to your code:

File file = new File(filename);
        if (file.exists()){

         FileReader fr = new FileReader(file);
         LineNumberReader ln = new LineNumberReader(fr);


            while (ln.getLineNumber() == 0){


              String s = ln.readLine();
              System.out.println("The line is: "+ s);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.