1.字节流 InputStream(抽象类)
1 package ioStudy; 2 3 import java.io.File; 4 import java.io.FileInputStream; 5 import java.io.FileNotFoundException; 6 import java.io.IOException; 7 import java.io.InputStream; 8 9 /* 10 1.创建源 11 2.选择流 12 3.操作 13 4.释放资源 14 */ 15 public class IOstudy2 { 16 public static void main(String[] args) { 17 File file = new File("test.txt"); //创建源 18 19 InputStream is = null; 20 21 try { 22 is = new FileInputStream(file); // 选择流 23 int temp; 24 while ((temp = is.read()) != -1) { //一次读一个字节 如果包含中文可能会出现乱码 考虑使用字符流 25 System.out.print((char) temp); //操作 26 } 27 } catch (FileNotFoundException e) { 28 // TODO Auto-generated catch block 29 e.printStackTrace(); 30 } catch (IOException e) { 31 // TODO Auto-generated catch block 32 e.printStackTrace(); 33 }finally { 34 if(is!=null) { 35 try { 36 is.close(); //释放资源 37 } catch (IOException e) { 38 // TODO Auto-generated catch block 39 e.printStackTrace(); 40 } 41 } 42 } 43 } 44 }
1 package ioStudy; 2 3 import java.io.File; 4 import java.io.FileInputStream; 5 import java.io.FileNotFoundException; 6 import java.io.IOException; 7 import java.io.InputStream; 8 9 /* 10 1.创建源 11 2.选择流 12 3.操作 13 4.释放资源 14 */ 15 public class IOstudy3 { 16 public static void main(String[] args) { 17 File file = new File("test.txt"); 18 InputStream is = null; 19 try { 20 is = new FileInputStream(file); 21 byte[] buffer = new byte[5]; 22 int len; 23 while ((len = is.read(buffer)) != -1) { //一次读取多个 24 String s = new String(buffer,0,len); 25 System.out.print(s); 26 } 27 } catch (FileNotFoundException e) { 28 // TODO Auto-generated catch block 29 e.printStackTrace(); 30 } catch (IOException e) { 31 // TODO Auto-generated catch block 32 e.printStackTrace(); 33 }finally { 34 if(is!=null) { 35 try { 36 is.close(); 37 } catch (IOException e) { 38 // TODO Auto-generated catch block 39 e.printStackTrace(); 40 } 41 } 42 } 43 } 44 }
2.OutputStream
1 package ioStudy; 2 3 import java.io.File; 4 import java.io.FileNotFoundException; 5 import java.io.FileOutputStream; 6 import java.io.IOException; 7 import java.io.OutputStream; 8 9 /* 10 1.创建源 11 2.选择流 12 3.操作 13 4.释放资源 14 */ 15 public class IOstudy4 { 16 public static void main(String[] args) { 17 File file = new File("output.txt"); // 不存在会自动创建 18 OutputStream os = null; 19 20 try { 21 os = new FileOutputStream(file,true); //开启向后追加 不然每次都删掉原来的 再写 默认的是false 22 String temp = "hello world! "; 23 byte[] b = temp.getBytes(); 24 os.write(b, 0, b.length); 25 os.flush(); //刷新内存 26 } catch (FileNotFoundException e) { 27 // TODO Auto-generated catch block 28 e.printStackTrace(); 29 } catch (IOException e) { 30 // TODO Auto-generated catch block 31 e.printStackTrace(); 32 } finally { 33 if (os != null) { 34 try { 35 os.close(); 36 } catch (IOException e) { 37 // TODO Auto-generated catch block 38 e.printStackTrace(); 39 } 40 } 41 } 42 } 43 }
3.实现文件的copy
1 package ioStudy; 2 3 import java.io.File; 4 import java.io.FileInputStream; 5 import java.io.FileNotFoundException; 6 import java.io.FileOutputStream; 7 import java.io.IOException; 8 import java.io.InputStream; 9 import java.io.OutputStream; 10 11 /* 12 1.创建源 13 2.选择流 14 3.操作 15 4.释放资源 16 */ 17 public class Copy { 18 public static void main(String[] args) { 19 copy("test.txt","testcopy.txt"); 20 } 21 22 public static void copy(String source, String destination) { 23 File src = new File(source); 24 File dest = new File(destination); 25 InputStream is = null; 26 OutputStream os = null; 27 try { 28 is = new FileInputStream(src); 29 os = new FileOutputStream(dest); 30 31 byte[] buffer = new byte[1034]; 32 int len; 33 while ((len = is.read(buffer)) != -1) { 34 os.write(buffer, 0, len); 35 os.flush(); 36 } 37 } catch (FileNotFoundException e) { 38 // TODO Auto-generated catch block 39 e.printStackTrace(); 40 } catch (IOException e) { 41 // TODO Auto-generated catch block 42 e.printStackTrace(); 43 } finally { 44 // 流关闭的原则 先打开的后关闭 45 if (os != null) { 46 try { 47 os.close(); 48 } catch (IOException e) { 49 // TODO Auto-generated catch block 50 e.printStackTrace(); 51 } 52 } 53 54 if (is != null) { 55 try { 56 is.close(); 57 } catch (IOException e) { 58 // TODO Auto-generated catch block 59 e.printStackTrace(); 60 } 61 } 62 } 63 } 64 }