Java——IO流(字节输入输出流)_2

1、单个文件字节输入流

package IOTest_2;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
/**
 * 文件单个字节输入流
 * @author Root
 * read():读取下一字节,返回int型数值,没有下一字节,返回-1
 * close():通知系统关闭数据流
 */
public class TestFileInputStream_1 {
	public static void main(String[] args) {
		File src = new File("abc.txt");
		InputStream is = null;
		try {
			is = new FileInputStream(src);
			int temp;
			while((temp=is.read())!=-1) {
				System.out.print((char)temp);
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				if(is!=null) {					
					is.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}

2、分段文件字节输入流

package IOTest_2;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
/**
 * 文件分段字节输入流
 * @author Root
 * read(byte[]):返回读取到的字节的长度,没有则返回-1
 */
public class TestFileInputStream_2 {
	public static void main(String[] args) {
		File src = new File("abc.txt");	//创建源
		InputStream is = null;
		try {
			is = new FileInputStream(src);
			byte[] b = new byte[10];		//缓冲容器
			int len = -1;				//记录字节长度
			while((len = is.read(b))!=-1) {
				System.out.println(new String(b,0,len));
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				if(is!=null) {					
					is.close();		//关闭流
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}

3、字节输出流

package IOTest_2;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
/**
 * 文件字节输出流
 * @author Root
 * FileOutputStream(File src,boolean append):创建文件输出流以写入由指定的 File对象表示的文件.
 * 			append true 追加方式反之覆盖
 * write(byte[] b):输入字节流
 * flush():强制刷新字节流
 */
public class TestFileOutputStream_3 {
	public static void main(String[] args) {
		File src = new File("test.txt");	//一:创建源
		OutputStream os = null;
		try {
			os = new FileOutputStream(src,true);	//二、选择流
			String s = "hello world\r\n";	//三、中间操作
			byte[] b = s.getBytes();
			os.write(b);						//四、输出字节流
			os.flush();					//五、强制刷新字节流
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {		//六、关闭字节流
			try {
				if(os!=null) {					
					os.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}

4、文件拷贝

package IOTest_2;

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 Copy {
	public static boolean copy(File src, File path) {
		InputStream is = null;
		OutputStream os = null;
		try {
			is = new FileInputStream(src); // 输入流
			os = new FileOutputStream(path);// 输出流
			byte[] b = new byte[1024]; // 缓冲器
			int len = -1;
			while ((len = is.read(b)) != -1) {
				os.write(b); // 输出
			}
			return len == -1 ? true : false;
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try { // 关闭输出流
				if (os != null) {
					os.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			try { // 关闭输入流
				if (is != null) {
					is.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return false;
	}
}

package IOTest_2;

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 TestCopy_4 {
	public static void main(String[] args) {
		File src = new File("img.png"); // 输入源
		File path = new File("E://abc.png"); // 输出源
		System.out.println(Copy.copy(src, path));
	}
}

5、文件夹整体拷贝

这里用的Copy类,在上面有记录

package IOTest_2;

import java.io.File;
/**
 * 文件夹之整体移动
 * @author Root
 *
 */
public class TestCopy_5 {
	public static void main(String[] args) {
		File src = new File("F:\\网站");
		File path = new File("E:/");
		myCopy(src,path);
	}
	
	static void myCopy(File src,File path){
		File file = null;
		if(src.isDirectory()) {
			file = new File(path+"/"+src.getName());
			file.mkdirs();
			for(File f: src.listFiles()) {
				myCopy(f, file);
			}
		}else {
			Copy.copy(src, new File(path.getPath()+"/"+src.getName()));
		}
	}
}

发布了49 篇原创文章 · 获赞 5 · 访问量 8785

猜你喜欢

转载自blog.csdn.net/Asdzxc968/article/details/88368466