java IO的总结

在java中 IO流 我工作中经常用到的只有两种,一种是字符流 和字节流

解释:读取和写出  都是相对而言的  相对内存而言的。

(1)写入操作:就是从硬盘中往内存中写入数据。

(2)写出操作:就是从内存中往硬盘中写出数据。

一。先来看看字节流:顾名思义 就是以字节为单位进行读取文件

1.字节流进行写出操作

public static void main(String[] args) {
   File file=new File("/Atomatic/java/homework/pachong/resluts/123.txt");
   OutputStream out=null;
   try {
      out=new BufferedOutputStream(new FileOutputStream(file));
      out.write("123edc".getBytes());
      out.flush();
   } catch (FileNotFoundException e) {
      e.printStackTrace();
   }catch (IOException e) {
      e.printStackTrace();
   }finally {
      if(out!=null) {
         try {
            out.close();
         } catch (IOException e) {
            e.printStackTrace();
         }
      }
   }
}

2.字节流进行写入操作

public static void main(String[] args) {
   InputStream in=null;
   File file=new File("/Atomatic/java/homework/pachong/resluts/123.txt");
   byte[] bs=new byte[1024];
   int len;
   try {
      in=new BufferedInputStream(new FileInputStream(file));
         while((len=in.read(bs))!=-1) {
            System.out.println(new String(bs,0,len));
         }
   } catch (FileNotFoundException e) {
      e.printStackTrace();
   } catch (IOException e) {
      e.printStackTrace();
   }finally {
      if(in!=null) {
         try {
            in.close();
         } catch (IOException e) {
            e.printStackTrace();
         }
      }
   }
}

3.字节流进行读写操作

public static void main(String[] args) {
   InputStream in=null;
   File file=new File("/Atomatic/java/homework/pachong/resluts/123.txt");
   File file2=new File("/Atomatic/java/homework/pachong/resluts/1232.txt");
   OutputStream out=null;
   byte[] bs=new byte[1024];
   int len;
   try {
      out=new BufferedOutputStream(new FileOutputStream(file2));
      in=new BufferedInputStream(new FileInputStream(file));
         while((len=in.read(bs))!=-1) {
            out.write(bs, 0, len);
            out.flush();
         }
   } catch (FileNotFoundException e) {
      e.printStackTrace();
   } catch (IOException e) {
      e.printStackTrace();
   }finally {
      if(in!=null) {
         try {
            in.close();
         } catch (IOException e) {
            e.printStackTrace();
         }
      }
      if(out!=null) {
         try {
            out.close();
         } catch (IOException e) {
            e.printStackTrace();
         }
      }
   }
}

再来看一下字符流:以字符为单位进行读取文件

1.字符流进行写出操作

File file=new File("/Atomatic/java/homework/pachong/resluts/0章 左勾拳2.txt");
   Writer writer=null;
   try {
      writer=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)));
      writer.write("成功了!");
      writer.flush();
      
   } catch (FileNotFoundException e) {
      e.printStackTrace();
   } catch (IOException e) {
      e.printStackTrace();
   }finally {
      if(writer!=null) {
         try {
            writer.close();
         } catch (IOException e) {
            e.printStackTrace();
         }
      }
   }
}

2.字符流进行写入操作

public static void main(String[] args){
   File file=new File("/Atomatic/java/homework/pachong/resluts/001章 左勾拳.txt");
   Reader read=null;
   try {
      read=new BufferedReader(new InputStreamReader(new FileInputStream(file)));
      char[] chars=new char[1024];
      int len;
         while((len=read.read(chars))!=-1) {
             System.out.println(new String(chars,0,len));
         }
   } catch (FileNotFoundException e) {
      e.printStackTrace();
   }catch (IOException e) {
      e.printStackTrace();
   }finally {
      if(read!=null) {
         try {
            read.close();
         } catch (IOException e) {
            e.printStackTrace();
         }
      }
   }
}

3.字符流进行读写操作

public static void main(String[] args) {
   File file=new File("/Atomatic/java/homework/pachong/resluts/001章 左勾拳.txt");
   File file2=new File("/Atomatic/java/homework/pachong/resluts/0章 左勾拳2.txt");
   Reader read=null;
   Writer writer=null;
   try {
      read=new BufferedReader(new InputStreamReader(new FileInputStream(file)));
      writer=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file2)));
      char[] chars=new char[1024];
      int len;
      while((len=read.read(chars))!=-1) {
         writer.write(new String(chars,0,len));
         writer.flush();
      }
   } catch (FileNotFoundException e) {
      e.printStackTrace();
   }catch (IOException e) {
      e.printStackTrace();
   }finally {
      if(read!=null) {
         try {
            read.close();
         } catch (IOException e) {
            e.printStackTrace();
         }
      }
      if(writer!=null) {
         try {
            writer.close();
         } catch (IOException e) {
            e.printStackTrace();
         }
      }
   }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值