How to read a file in java - code example

Java code for read a text file in java
 
File file = new File("C:\\filenamehere.txt");
    FileInputStream
fileInputStream = null;
    BufferedInputStream
bufferedInputStream = null;
    DataInputStream
dataInputStream = null;

    try {
     
fileInputStream = new FileInputStream(file);

     
     
bufferedInputStream = new BufferedInputStream(fileInputStream);
     
dataInputStream = new DataInputStream(bufferedInputStream);

     
      while (
dataInputStream.available() != 0) {

    
        System.out.println(
dataInputStream.readLine());
      }

     
     
fileInputStream.close();
     
bufferedInputStream.close();
      
dataInputStream.close();

    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }

Comments

Recent Post

Recent Posts Widget

Popular posts from this blog

Capture image from webcam java code, examples

How to capture finger prints in java

autoboxing and unboxing in java