使用字节流FileInputStream读取、FileOutputStream写入、拷贝(先读取后写入)

package com.io.test;

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

/**
 * 使用字节流FileInputStream读取、FileOutputStream写入、拷贝(先读取后写入)
 * @author St01
 *
 */
public class ByteStreamTest {
	public static void main(String[] args) {
		ByteStreamTest  st=new ByteStreamTest();
		st.read("E:/qwerty/p01.txt");
//		st.read2("E:/qwerty/p01.txt");
//		st.write("E:/qwerty/p03.txt");
//		st.write2("E:/qwerty/p02.txt");
//		st.copy("E:/test727.docx","F:/test727.docx");
//		st.copy2("E:/test727.docx","F:/test727.docx");
	}
	
	//read()------文件读取方法1
	/**
	 * for循环读取,每次只从输入流中读取一个数据字节,并将byte型数据转换为char型后输出
	 * @param url
	 */
	public void read(String  url) {
		FileInputStream  fis =null;
		try {  //创建FileInputStream对象
			fis=new FileInputStream(url);
			//available()返回下一次对此输入流调用的方法可以不受阻塞地从此输入流读取的估计剩余字节数
			int length =fis.available();//返回文件输入流的长度
			for (int i = 0; i < length; i++) {
				byte b =(byte)fis.read();//从此输入流中读取一个数据字节
				System.out.print((char)b);//将byte型数据转换为char型,并输出
			}
			
		} catch (FileNotFoundException e) {
			// 抛出异常--文件找不到
			e.printStackTrace();
		} catch (IOException e) {
			// 抛出异常--IOException
			e.printStackTrace();
		}finally {
			//不管是否抛出异常,最后都要关闭文件输入流
			try {
				if (fis != null) {
					fis.close();
				}//如果不为null,则关闭FileInputStream
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
	}

	//read2()-----文件读取方法2
	/**
	 * 将文件流读入byte数组,再将byte数组包装为String对象,最后输出String的内容
	 * @param url
	 */
	public void read2(String url) {
		FileInputStream  fis =null;
		try {//创建FileInputStream对象
			fis=new FileInputStream(url);
			int length =fis.available();//返回文件输入流的长度
			byte[]  buff =new byte[length];//根据文件流的长度创建一个byte数组
			//read(byte[] b)从此输入流中将最多 b.length 个字节的数据读入一个 byte 数组中
			fis.read(buff);//将文件流读入byte数组
			String content =new String(buff);//将byte数组包装为String对象
			System.out.println(content);//输出文件流的内容
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				if (fis !=null) {
					fis.close();
				}
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
	}
	
	//write()-----文件写入方法1
	/**
	 * for循环写入,每次只向FileOutputStream文件输出流写入单个字符
	 * 该方法会出现中文乱码问题
	 * @param url
	 */
	public void write(String url) {
		
		try {
			//创建FileOutputStream文件输出流对象
			FileOutputStream  fos =new FileOutputStream(url);
			String  str  ="Hello world!\r\n大家好!";
			for (int i = 0; i < str.length(); i++) {
				fos.write(str.charAt(i));
				//str.charAt(i)返回指定索引处的字符
				//fos.write()将字符写到文件输出流
			}
			//关闭文件输出流
			fos.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
	}

	//write2()------文件写入方法2
	/**
	 * 先将字符串String转换为byte数组,再写入到文件输出流中
	 * @param url
	 */
	public void write2(String url) {
		try {
			FileOutputStream fos = new FileOutputStream(url, true);
			String str = "Hello world!\r\n大家好!";
			fos.write(str.getBytes());
			//getBytes()使用平台的默认字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中
			//先将字符串String转换为byte数组,再写入到文件输出流中
			fos.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	//copy(oldURL,newURL)----复制文件方法1
	/**
	 * 将文件从oldURL复制到newURL
	 * for循环读取和写入,每次只读取和写入一个数据字节
	 * 缺点---该方法复制速度很慢
	 * @param url
	 */
	public void copy(String oldURL , String newURL) {
		try {
			//分别创建文件输入流和文件输出流
			FileInputStream fis =new FileInputStream(oldURL);
			FileOutputStream fos = new FileOutputStream(newURL);
			int length =fis.available();//返回文件输入流的最大长度
			for (int i = 0; i < length; i++) {
				//fis.read() 不带参数时,表示从此输入流中读取一个数据字节
				//fis.read() 返回下一个数据字节;如果已到达文件末尾,则返回 -1
				int b=fis.read();
				//将输入流中读到的数据字节写入到文件输出流
				fos.write(b);
			}
			//先关闭文件输出流,再关闭文件输入流
			fos.close();
			fis.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	//copy2(oldURL,newURL)-----复制文件方法2
	/**
	 * 将文件从oldURL复制到newURL
	 * while循环 --先将从输入流中的数据字节存储到byte数组中
	 * 再将byte数组中的数据字节写入到输出流
	 * 当fis.read()返回值为-1时停止,表示已到达文件末尾
	 * @param oldURL
	 * @param newURL
	 */
	public void copy2(String oldURL , String newURL) {
		
		try {
			//分别创建文件输入流和文件输出流
			FileInputStream fis =new FileInputStream(oldURL);
			FileOutputStream fos =new FileOutputStream(newURL);
			//创建一个大小为1024的byte数组
			//1KB=1024byte
			byte[]  buff =new byte[1024];
			//循环从输入流中读入数据字节,并存储到byte数组中
			//fis.read()返回值为-1时停止,表示已到达文件末尾
			//fis.read(byte[] b)返回读入缓冲区的字节总数,如果因为已经到达文件末尾而没有更多的数据,则返回 -1
			while ((fis.read(buff)) != -1) {
				fos.write(buff);//将byte数组中的数据字节写入到输出流
			}
			//先关闭文件输出流,再关闭文件输入流
			fos.close();
			fis.close();
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

猜你喜欢

转载自blog.csdn.net/ThinkPet/article/details/81235491