Java/File Input Output/BufferedInputStream — различия между версиями

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

Текущая версия на 06:02, 1 июня 2010

Copy byte between BufferedInputStream and BufferedOutputStream

 
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
class BufferedCopy {
  public static void main(String[] args) throws Exception {
    BufferedInputStream bis = null;
    BufferedOutputStream bos = null;
    FileInputStream fis = new FileInputStream(args[0]);
    bis = new BufferedInputStream(fis);
    FileOutputStream fos = new FileOutputStream(args[1]);
    bos = new BufferedOutputStream(fos);
    int byte_;
    while ((byte_ = bis.read()) != -1)
      bos.write(byte_);
  }
}





Create BufferedInputStream from FileInputStream

 

import java.io.BufferedInputStream;
import java.io.FileInputStream;
class BufferedInputStreamDemo {
  public static void main(String args[]) throws Exception {
    FileInputStream fis = new FileInputStream(args[0]);
    BufferedInputStream bis = new BufferedInputStream(fis);
    int i;
    while ((i = bis.read()) != -1) {
      System.out.println(i);
    }
    fis.close();
  }
}





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





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