集册 Java实例教程 使用BufferedInputStream读取字符串中的文件

使用BufferedInputStream读取字符串中的文件

欢马劈雪     最近更新时间:2020-01-02 10:19:05

563
使用BufferedInputStream读取字符串中的文件


import java.io.BufferedInputStream;
/**来自 
 时代Java公众号**/

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;


public class Main {


  public static void main(String[] args) {


    // create file object

    File file = new File("C://Folder//ReadFile.txt");

    BufferedInputStream bin = null;
/*时代Java公众号 - N o w J a  v a . c o m*/

    try {

      // create FileInputStream object

      FileInputStream fin = new FileInputStream(file);


      // create object of BufferedInputStream

      bin = new BufferedInputStream(fin);


      // create a byte array

      byte[] contents = new byte[1024];


      int bytesRead = 0;

      String strFileContents;


      while ((bytesRead = bin.read(contents)) != -1) {


        strFileContents = new String(contents, 0, bytesRead);

        System.out.print(strFileContents);

      }


    } catch (FileNotFoundException e) {

      System.out.println("File not found" + e);

    } catch (IOException ioe) {

      System.out.println("Exception while reading the file " + ioe);

    } finally {

      
展开阅读全文