Java Tutorial/File/BufferedInputStream

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

BufferedInputStream

For faster reading you should use BufferedInputStream to wrap any InputStream. BufferedInputStream has two constructors that accept an InputStream:



public BufferedInputStream(InputStream in)
public BufferedInput Stream (Input Stream in, int bufferSize)



Then, instead of calling the methods on fis, work with the BufferedInputStream bis.


Buffered Stream Copier

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class MainClass {
  public static void main(String[] args) {
    try {
      copy(System.in, System.out);
    } catch (IOException ex) {
      System.err.println(ex);
    }
  }
  public static void copy(InputStream in, OutputStream out) throws IOException {
    BufferedInputStream bin = new BufferedInputStream(in);
    BufferedOutputStream bout = new BufferedOutputStream(out);
    while (true) {
      int datum = bin.read();
      if (datum == -1)
        break;
      bout.write(datum);
    }
    bout.flush();
  }
}





Import a file of exported preference data.

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.prefs.Preferences;
public class Main {
  public static void main(String[] argv) throws Exception {
    // Create an input stream on a file
    InputStream is = new BufferedInputStream(new FileInputStream("output.xml"));
    // Import preference data
    Preferences.importPreferences(is);
  }
}





Read File in String Using Java BufferedInputStream Example

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);
    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);
    }
    bin.close();
  }
}





Read File Using Java BufferedInputStream Example

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();
  }
}





Read from file with BufferedInputStream

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();
  }
}





Save keyboard input with BufferedInputStream

import java.io.BufferedInputStream;
import java.io.IOException;
public class ConsoleInput {
  public static String readLine() {
    StringBuffer response = new StringBuffer();
    try {
      BufferedInputStream buff = new BufferedInputStream(System.in);
      int in = 0;
      char inChar;
      do {
        in = buff.read();
        inChar = (char) in;
        if ((in != -1) & (in != "\n") & (in != "\r")) {
          response.append(inChar);
        }
      } while ((in != -1) & (inChar != "\n") & (in != "\r"));
      buff.close();
      return response.toString();
    } catch (IOException e) {
      System.out.println("Exception: " + e.getMessage());
      return null;
    }
  }
  public static void main(String[] arguments) {
    System.out.print("\nWhat is your name? ");
    String input = ConsoleInput.readLine();
    System.out.println("\nHello, " + input);
  }
}





Use buffered streams to copy a file

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class Main {
  public static void main(String[] argv) throws Exception {
    BufferedInputStream fin = new BufferedInputStream(new FileInputStream("in.dat"));
    BufferedOutputStream fout = new BufferedOutputStream(new FileOutputStream("out.dat"));
    int i;
    do {
      i = fin.read();
      if (i != -1)
        fout.write(i);
    } while (i != -1);
    fin.close();
    fout.close();
  }
}