淘先锋技术网

首页 1 2 3 4 5 6 7
package pack.java.io;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class GetFileDemo {

	/**
	 * 测试IO流的主方法;
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		File readFile =new File("C:\\readFile.txt");
		File writeFile = new File("C:\\writeFile.txt");
		FileInputStream fileInputStream = null;
		FileOutputStream fileOutputStream = null;
		try {
			//从程序的角度出发,从C盘的myFile.txt文件读进来,是输入流;
			fileInputStream=new FileInputStream(readFile);
			//从程序的角度出发,输出到别出去,是输出流.
			fileOutputStream=new FileOutputStream(writeFile);
			try {
				int c=0;
				while ((c=fileInputStream.read())!=-1) {
					fileOutputStream.write(c);
				}
				System.out.println("成功输出文件.");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			//关闭输入流;
			if(fileInputStream!=null){
				try {
					fileInputStream.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			//关闭输出流;
			if(fileOutputStream!=null){
				try {
					fileOutputStream.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
}

 

 

这个例子非常的简单..就是从应该读取一个文件,然后写入到另外一个地方..