输入流

字节输入流:FileInputStream

  1. //方法一:
  2. public static void main(String args[]) throws IOException {
  3. FileInputString fi = new FileInputStream("a.txt");
  4. int by;
  5. while((by=fi.read())!=-1){
  6. System.out.println((char)by);
  7. }
  8. //关闭流输入
  9. fi.close();
  10. }
  11. //方法二:
  12. public static void main(String args[]) throws IOException {
  13. FileInputString fi = new FileInputStream("a.txt");
  14. byte[] bytes = new byte[1024];
  15. int len;
  16. while((len=fi.read(bytes))!=-1){
  17. System.out.println(new String(bytes,0,len));
  18. }
  19. }

字节缓冲输入流:BufferedInputStream

创建BufferedInputStream将创建一个内部缓冲区数组.当从流中读取或跳过字节时,内部缓冲区将根据需要从所包含的输入流中重新填充,一次很多字节

  1. public static void main(String[] args){
  2. BufferedInputStream bf = new BufferedInputStream(new FileInputStream("a.txt"));
  3. byte[] bytes = new byte[1024];
  4. int len;
  5. while((len = bf.read(bytes))!=-1){
  6. System.out.println(new String(bytes,0,len));
  7. }
  8. bis.close();
  9. }

输出流

字节输出流:FileOutPutStream

  1. public static void main(String[] args) throws Exception{
  2. FileOutputStream fo = new FileOutputStream("a.txt");
  3. fo.write("AGE".getBytes());
  4. fo.close();
  5. }

字节缓冲输入流:BufferedOutputStream

  1. public static void main(String[] args) throws IOException {
  2. //字节缓冲输出流:BufferedOutputStream(OutputStream out)
  3. BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("a.txt"));
  4. //写数据
  5. bos.write("hello\r\n".getBytes());
  6. bos.write("world\r\n".getBytes());
  7. //释放资源
  8. bos.close();
  9. }