Blog Archive

Java Read File using BufferedInputStream example

package com.as400samplecode;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class ReadBufferedInputStream {

 public static void main(String[] args) {

  File file = new File("data/newFile.txt");
  FileInputStream fis = null;
  BufferedInputStream bis = null;
  
  try {
   fis = new FileInputStream(file);
   bis = new BufferedInputStream(fis);

   //process each character at a time
   while( bis.available() > 0 ){
    System.out.print((char)bis.read());
   }

  } catch (IOException e) {
   e.printStackTrace();
  } finally {
   try {
    fis.close();
    bis.close();
   } catch (IOException ex) {
    ex.printStackTrace();
   }
  }

 }

}


Another more efficient way using a byte array
package com.as400samplecode;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class ReadBufferedInputStream {

 public static void main(String[] args) {

  File file = new File("data/newFile.txt");
  FileInputStream fis = null;
  BufferedInputStream bis = null;

  try {
   fis = new FileInputStream(file);
   bis = new BufferedInputStream(fis);

   byte[] buffer = new byte[1024];
   int bytesRead = 0;

   // Keep reading from the file while there is any content
   // when the end of the stream has been reached, -1 is returned
   while ((bytesRead = bis.read(buffer)) != -1) {
    // Process the chunk of bytes read
    // in this case we just construct a String and print it out
    String chunk = new String(buffer, 0, bytesRead);
    System.out.print(chunk);
   }

  } catch (IOException e) {
   e.printStackTrace();
  } finally {
   try {
    fis.close();
    bis.close();
   } catch (IOException ex) {
    ex.printStackTrace();
   }
  }

 }

}


1 comment :

  1. Hello and thanks for the posts!
    I use this code to save and read data.Is this right?

    save function:

    File sdCard = Environment.getExternalStorageDirectory();
    File directory = new File (sdCard, "Folder");
    directory.mkdirs();
    File file = new File(directory, filename);


    BufferedWriter bw = null;

    try {
    FileWriter writer=new FileWriter(file);
    bw= new BufferedWriter(writer);

    bw.write(date+"\t"+data+"\n");

    bw.newLine();
    } catch (FileNotFoundException e) {
    // handle exception
    } catch (IOException e) {
    // handle exception
    } finally {
    try {
    if (bw != null){
    bw.flush();
    bw.close();
    }
    } catch (IOException ex) {
    ex.printStackTrace();
    }
    }

    reading function:

    File sdCard = Environment.getExternalStorageDirectory();
    File directory = new File (sdCard, "Folder");
    File file = new File(directory, filename);


    FileInputStream fis = null;
    BufferedInputStream bis = null;

    try{

    fis = new FileInputStream(file);
    bis = new BufferedInputStream(fis);
    byte[] buffer = new byte[1024];
    int bytesRead = 0;

    // Keep reading from the file while there is any content
    // when the end of the stream has been reached, -1 is returned
    while ((bytesRead = bis.read(buffer)) != -1) {
    // Process the chunk of bytes read
    // in this case we just construct a String and print it out
    String chunk = new String(buffer, 0, bytesRead);
    System.out.print(chunk);
    }


    } catch (FileNotFoundException e) {
    // handle exception
    } catch (IOException e) {
    // handle exception
    } finally {
    try {
    fis.close();
    bis.close();
    } catch (IOException ex) {
    ex.printStackTrace();
    }
    }

    is this right way to do that?And retrieve the "date" and "data".

    Also, the file I want to save is xls file.
    In line "bw.write(date+"\t"+data+"\n");" I can't put in one column the date and in the other the data..
    If you can help me , I'll appreciate.

    Thanks!

    ReplyDelete