Java by API/java.io/BufferedInputStream

Материал из Java эксперт
Перейти к: навигация, поиск

BufferedInputStream: available()

 
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
public class Main {
  public static void main(String[] args) throws Exception {
    File file = new File("C:/ReadFile.txt");
    FileInputStream fin = new FileInputStream(file);
    BufferedInputStream bin = new BufferedInputStream(fin);
    while (bin.available() > 0) {
      System.out.println((char) bin.read());
    }
    bin.close();
  }
}





BufferedInputStream: close()

 
import java.io.BufferedInputStream;
import java.io.FileInputStream;
public class Main {
  public static void main(String[] args) throws Exception {
    byte[] buffer = new byte[1024];
    BufferedInputStream bufferedInput = new BufferedInputStream(new FileInputStream("filename.txt"));
    int bytesRead = 0;
    while ((bytesRead = bufferedInput.read(buffer)) != -1) {
      String chunk = new String(buffer, 0, bytesRead);
      System.out.print(chunk);
    }
    bufferedInput.close();
  }
}





BufferedInputStream: read()

 
/*
 * Output:
 *  
 */
import java.io.BufferedInputStream;
import java.io.FileInputStream;
public class MainClass {
  public static void main(String args[]) {
    try {
      FileInputStream fis = new FileInputStream(args[0]);
      BufferedInputStream bis = new BufferedInputStream(fis);
      int i;
      while ((i = bis.read()) != -1) {
        System.out.println(i);
      }
      fis.close();
    } catch (Exception e) {
      System.out.println("Exception: " + e);
    }
  }
}





BufferedInputStream: read(byte[] b)

 
import java.io.BufferedInputStream;
import java.io.FileInputStream;
public class Main {
  public static void main(String[] args) throws Exception {
    byte[] buffer = new byte[1024];
    BufferedInputStream bufferedInput = new BufferedInputStream(new FileInputStream("filename.txt"));
    int bytesRead = 0;
    while ((bytesRead = bufferedInput.read(buffer)) != -1) {
      String chunk = new String(buffer, 0, bytesRead);
      System.out.print(chunk);
    }
    bufferedInput.close();
  }
}





new BufferedInputStream(FileInputStream fileInputStream)

 
/*
 * Output:
 */
import java.io.BufferedInputStream;
import java.io.FileInputStream;
public class MainClass {
  public static void main(String args[]) {
    try {
      FileInputStream fis = new FileInputStream(args[0]);
      BufferedInputStream bis = new BufferedInputStream(fis);
      int i;
      while ((i = bis.read()) != -1) {
        System.out.println(i);
      }
      fis.close();
    } catch (Exception e) {
      System.out.println("Exception: " + e);
    }
  }
}