Java之字节输入流和输出流

package IODemo;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class OutputStreamAndInputStream {

 /**
  * @param args
  * @throws IOException
  */
 public static void main(String[] args) throws IOException {
  demo1();
  demo2();
  demo3();
 }

 private static void demo3() throws IOException {
  FileInputStream fis = new FileInputStream("Demo.txt");
  byte buf[] = new byte[10];
  int len = 0;
  while ((len = fis.read(buf)) != -1) {
   System.out.println(new String(buf, 0, len));
  }
 }

 private static void demo2() throws IOException {
  FileOutputStream fos = new FileOutputStream("Demo.txt");
  fos.write("abcd".getBytes());
 }

 private static void demo1() throws IOException {
  FileInputStream fis = new FileInputStream("Demo.txt");
  int ch = 0;
  while ((ch = fis.read()) != -1) {
   System.out.println((char) ch);
  }
 }

}
原文地址:https://www.cnblogs.com/ysw-go/p/5284844.html