分享下在java中,使用io查看文件,编写文件,复制文件。
1.复制图片,未抛出异常
package com.newdream.class4; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class test_FileCopy { //未抛出异常 public static void main(String[] args) throws IOException { //找到目标文件 File inFile = new File("F:/3-150402141930.jpg"); File destFile = new File("E:/1.jpg"); //建立数据的输入输出通道 FileInputStream fileInputStream = new FileInputStream(inFile); FileOutputStream fileOutputStream = new FileOutputStream(destFile); //每新创建一个FileOutputStream的时候,默认情况下FileOutputStream 的指针是指向了文件的开始的位置。 每写出一次,指向都会出现相应移动。 //建立缓冲数据,边读边写 byte[] buf = new byte[1024]; int length = 0 ; while((length = fileInputStream.read(buf))!=-1){ fileOutputStream.write(buf,0,length); //写出很多次数据,所以就必须要追加。 } //关闭资源 原则: 先开后关,后开先关。 fileOutputStream.close(); fileInputStream.close(); } }
2.查看文件
1 package com.newdream.class4; 2 3 import java.io.BufferedReader; 4 import java.io.FileReader; 5 6 public class CharIOReader { 7 //读取文件当中的内容 8 public static void main(String[] args) { 9 //读取字符文件 10 //1.打通通道 11 FileReader fr=null; //2.创建字符流输入缓冲区 12 BufferedReader br=null; 13 try{ 14 //把文件内容放到缓存区 15 fr=new FileReader("E:\newdream.txt"); 16 //3,从缓冲区读取字符 17 //从缓存区读取数据 18 br=new BufferedReader(fr); 19 //使用while 读出数据 20 while(br.read()!=-1){ 21 String date =br.readLine(); 22 System.out.println(date); 23 } 24 }catch (Exception e){ 25 System.out.println(e.toString()); 26 }finally{ 27 try{ 28 br.close(); //5.关闭缓冲区,关闭通道 29 }catch(Exception e){ 30 System.out.println(e.toString()); 31 } 32 } 33 } 34 35 }
3.写入文件
1 package com.newdream.class4; 2 3 import java.io.BufferedReader; 4 import java.io.BufferedWriter; 5 import java.io.File; 6 import java.io.FileWriter; 7 import java.io.StringReader; 8 9 public class CharIOWriter { 10 11 //将文字写入指定文件 12 13 14 public static String data="我胡汉三又回来了QAQ!"; 15 public static void main(String[] args) { 16 String line;//用来控制写文件结束 17 BufferedWriter bw=null; 18 BufferedReader br=null; 19 try{ 20 FileWriter fr=new FileWriter("E:\newdream.txt"); 21 bw=new BufferedWriter(fr); 22 //先读取到字符流,再把字符流存到读取流中 23 br=new BufferedReader(new StringReader(data)); 24 //readLine() 为null 表示没有内容了,结束 25 while((line=br.readLine())!=null){ 26 bw.write(line); 27 //写入一行,刷新一下,写下一行 28 bw.flush(); 29 } 30 System.out.println("写入成功!"); 31 }catch(Exception e){ 32 System.out.println(e.toString()); 33 }finally{ 34 try{ 35 //关闭缓存区:先打开后关闭,后打开先关闭 36 br.close(); 37 bw.close(); 38 }catch(Exception e){ 39 System.out.println(e.toString()); 40 } 41 } 42 } 43 }
4.复制电影
1 package com.newdream.class4; 2 3 import java.io.File; 4 import java.io.FileInputStream; 5 import java.io.FileOutputStream; 6 import java.io.IOException; 7 8 public class test_FileCopyMovie { 9 public static void main(String[] args) throws IOException { 10 //找到目标文件 11 File inFile = new File("F:\\迅雷下载\\与君相恋100次.mp4"); 12 File destFile = new File("D:\\100婚.mp4"); 13 //建立数据的输入输出通道 14 FileInputStream fileInputStream = new FileInputStream(inFile); 15 FileOutputStream fileOutputStream = new FileOutputStream(destFile); 16 17 //每新创建一个FileOutputStream的时候,默认情况下FileOutputStream 的指针是指向了文件的开始的位置。 每写出一次,指向都会出现相应移动。 18 //建立缓冲数据,边读边写 19 byte[] buf = new byte[1024]; 20 int length = 0 ; 21 while((length = fileInputStream.read(buf))!=-1){ 22 fileOutputStream.write(buf,0,length); 23 } 24 //关闭资源 原则: 先开后关,后开先关。 25 fileOutputStream.close(); 26 fileInputStream.close(); 27 } 28 29 }
--与上面复制文件相同
本文仅代表作者观点,系作者@请多指教 发表。
欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
http://www.cnblogs.com/yushengaqingzhijiao/p/8343319.html