Java(Android)通过FileInputStream读取文件内容到字符串

liuli 分享于 10小时前 4943阅 0人收藏此代码, 我要收藏

这段代码通过FileInputStream的read方法将文件内容逐个读取到StringBuffer中,最后输出StringBuffer的内容

/*
  Read file using FileInputStream
  This example shows how to read a file using Java FileInputStream class.
  FileInputStream is used to read binary content of the file and
  return bytes of data.
*/
import java.io.*;
 
public class ReadStringFromFile {
 
  public static void main(String[] args) {
   
    //create file object
    File file = new File("C://FileIO//ReadString.txt");
   
    int ch;
    StringBuffer strContent = new StringBuffer("");
    FileInputStream fin = null;
   
    try
    {
      /*
       * Create new FileInputStream object. Constructor of FileInputStream throws
       * FileNotFoundException if the agrument File does not exist.
       */
     
      fin = new FileInputStream(file);
     
      /*
       * To read bytes from stream use,
       * int read() method of FileInputStream class.
       *
       * This method reads a byte from stream. This method returns next byte of data
       * from file or -1 if the end of the file is reached.
       *
       * Read method throws IOException in case of any IO errors.
       */
     
      while( (ch = fin.read()) != -1)
        strContent.append((char)ch);
 
      /*
       * To close the FileInputStream, use
       * void close() method of FileInputStream class.
       *
       * close method also throws IOException.
       */
      fin.close();
     
    }
    catch(FileNotFoundException e)
    {
      System.out.println("File " + file.getAbsolutePath() +
                             " could not be found on filesystem");
    }
    catch(IOException ioe)
    {
      System.out.println("Exception while reading the file" + ioe);
    }
   
    System.out.println("File contents :");
    System.out.println(strContent);
   
   
    /*
     * Please note that, FileInputStream SHOULD NOT BE USED to read
     * character data file.
     * It is meant for reading binary data such as an image file.
     *
     * To read character data, FileReader should be used.
     */
  }
}
 
/*
Output would be
File contents :
This file is a demonstration of how to read a file using Java FileInputStream.
*/
//该代码片段来自于: http://www.sharejs.com/codes/java/9041

标签:
  • FileInputStream
  • 读取文件
  • Android