Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read data from a text file using Java

I need to read a text file line by line using Java. I use available() method of FileInputStream to check and loop over the file. But while reading, the loop terminates after the line before the last one. i.e., if the file has 10 lines, the loop reads only the first 9 lines. Snippet used :

while(fis.available() > 0)
{
    char c = (char)fis.read();
    .....
    .....
}
like image 516
Saran Avatar asked May 19 '10 09:05

Saran


People also ask

How do you read the contents of a file into a string in Java?

Java read file to String using BufferedReader BufferedReader reader = new BufferedReader(new FileReader(fileName)); StringBuilder stringBuilder = new StringBuilder(); String line = null; String ls = System. getProperty("line. separator"); while ((line = reader. readLine()) !=

Which class do you use to read data from a text file?

Java FileReader class is used to read data from the file.

How do you read and print the contents of a file in Java?

To read the contents of a file into a Java application, we can create a FileInputStream to the file and call its read() method to read its bytes one at a time. An OutputStream outputs a stream of bytes from a Java application. System. out is a PrintStream, which is a type of OutputStream.


3 Answers

You should not use available(). It gives no guarantees what so ever. From the API docs of available():

Returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking by the next invocation of a method for this input stream.

You would probably want to use something like

try {
    BufferedReader in = new BufferedReader(new FileReader("infilename"));
    String str;
    while ((str = in.readLine()) != null)
        process(str);
    in.close();
} catch (IOException e) {
}

(taken from http://www.exampledepot.com/egs/java.io/ReadLinesFromFile.html)

like image 124
aioobe Avatar answered Sep 28 '22 11:09

aioobe


How about using Scanner? I think using Scanner is easier

     private static void readFile(String fileName) {
       try {
         File file = new File(fileName);
         Scanner scanner = new Scanner(file);
         while (scanner.hasNextLine()) {
           System.out.println(scanner.nextLine());
         }
         scanner.close();
       } catch (FileNotFoundException e) {
         e.printStackTrace();
       }
     }

Read more about Java IO here

like image 34
vodkhang Avatar answered Sep 28 '22 11:09

vodkhang


//The way that I read integer numbers from a file is...

import java.util.*;
import java.io.*;

public class Practice
{
    public static void main(String [] args) throws IOException
    {
        Scanner input = new Scanner(new File("cards.txt"));

        int times = input.nextInt();

        for(int i = 0; i < times; i++)
        {
            int numbersFromFile = input.nextInt();
            System.out.println(numbersFromFile);
        }




    }
}
like image 34
Abdullah Ahmad Avatar answered Sep 28 '22 12:09

Abdullah Ahmad