IO流第六课,字节流、节点流、文件的拷贝

来源:互联网 发布:未来网络发展论文 编辑:程序博客网 时间:2024/06/05 17:53

文件的拷贝,程序为桥梁

建立联系     File对象    文件源头      拷贝至目的地

选择流        文件输出流 OutputStream   FileOutputStream

                   文件输入流 InputStream      FileInpuStream

操作            byte[] b = new byte[1024];

                   int len = 0;

                   while(len = (输入流.read(b)) != -1){

                            输出流.write ()+b 

                   }

释放资源    关闭输出、输入流

package com.pkushutong.ioFile;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;public class Test08 {public static void main(String[] args) {String InputPath = "F:/FileTest/12.jpg";String OutputPath = "F:/FileTest/11.jpg";try {copyFile(InputPath, OutputPath);} catch (FileNotFoundException e) {e.printStackTrace();System.out.println("文件不存在");} catch (IOException e) {e.printStackTrace();System.out.println("文件拷贝失败");}}public static void copyFile(String InputPath,String OutputPath) throws IOException{File src = new File(InputPath);File dest = new File(OutputPath);if(!src.isFile()){throw new IOException("只能拷贝文件");}InputStream is = new FileInputStream(InputPath);OutputStream os = new FileOutputStream(OutputPath);byte[] b = new byte[1024];int len = 0;while((len = (is.read(b))) != -1){os.write(b, 0, len);}os.flush();os.close();is.close();}}

0 0
原创粉丝点击