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