Java 之 Java 字节流

字节流

一个流可以理解为一个数据的序列。输入流表示从一个源读取数据,输出流表示向一个目标写数据。
字节流
参照物为自己的程序
什么叫输出?
程序 --> 文件 输出
什么叫输入?
文件 --> 程序 输入
字节输出流(OutPut) -- 写文件
OutPutStream 是抽象类,是所有输出流的父类
一次写入一个字节,一个字节是8个二进制位

字节输出流

public class Demo {
	public static void main(String[] args){
		/*
		 * 异常处理
		 * io 发生了异常,都需要停止程序修改代码
		 */
		File file = new File("/Users/Desktop/Test/Test.txt");
		FileOutputStream fileOutputStream = null;
		try {
			// 创建一个流
			// 只要是手动创建的流,需要手动关闭
			fileOutputStream = new FileOutputStream(file);
			// 写入
			fileOutputStream.write("Hallo".getBytes());
		} catch (FileNotFoundException e) {
			// 抛出一个运行时异常(直接停止掉程序)
			throw new RuntimeException("无法查询到此文件!");
		} catch (IOException e) {
			// 文件编写失败,直接停止程序
			throw new RuntimeException("文件编写失败!");
		} finally {
			try {
				// 非空判断
				// 非空,关闭流
				// 空,创建流失败,不需要关闭
				if (fileOutputStream != null) {
					fileOutputStream.close();
				}
			} catch (IOException e) {
				// 关闭资源失败,停止程序
				throw new RuntimeException("关闭失败!");
			}
		}
	}
public static void Test() throws FileNotFoundException, IOException {
	/*
	 * 构造方法:
	 * 参数	1.文件(被写入的文件)
	 * 		2.文件路径(被写入的路径)
	 * 
	 * 写入文件的流程:
	 * 1.绑定要写入的文件或文件路径
	 * 2.使用 write 方法写入
	 * 3.关闭资源
	 */
	// 绑定数据的目的地
	// 该路径下,没有此文件时,会帮你创建这个文件
	// 如果已经有了此文件,文件会被覆盖
	File file = new File("/Users/Desktop/Test/www.baidu.com.txt");
	FileOutputStream fileOutputStream = new FileOutputStream(file);
	// 写入数据
	// 按 ASCII 表的值输入的
	fileOutputStream.write(49);
	fileOutputStream.write(48);
	fileOutputStream.write(48);
	// 用字节数组写入
	byte[] b = {65,66,67,68};
	fileOutputStream.write(b);
	// 按字节数组的索引和长度写入
	fileOutputStream.write(b, 1, 2);
	
	// 简单写法
	fileOutputStream.write("Hello".getBytes());
	fileOutputStream.write("World".getBytes());
	// 关闭资源
	fileOutputStream.close();
}
public static void Test() throws FileNotFoundException, IOException {
	/*
	 * 文件的续写和换行
	 */
	File file = new File("/Users/Desktop/Test/Test.txt");
	FileOutputStream fileOutputStream = new FileOutputStream(file, true);
	fileOutputStream.write("Hello\n".getBytes());
	// mac 下 \n 换行
	// Windows 下 \r\n 换行
	fileOutputStream.close();
}

字节输入流

/*
 * 字节输入流(读文件)
 * InputStream(所有输入流的父类)
 * 注意:字节流写入的时候是一个字节一个字节的写
 * 		读取也是一个字节一个字节的读
 * 
 * 读取文件流程
 * 1.绑定数据源文件(要读那个文件)
 * 2.使用 read 方法读
 * 3.关闭资源
 */
public static void main(String[] args) throws IOException {
	File file = new File("/Users/Desktop/Test/Test.txt");
	FileInputStream fileInputStream = new FileInputStream(file);
	byte[] b = new byte[1024];
	int read = fileInputStream.read(b, 0, 1024);
	for (int i = 0; i < read; i++) {
		System.out.println(b[i]);
	}
	fileInputStream.close();
}
public static void test() throws FileNotFoundException, IOException {
	// 使用字节数组读取文件
	File file = new File("/Users/Desktop/Test/Test.txt");
	FileInputStream fileInputStream = new FileInputStream(file);
	// 声明了一个长度为2的空的字节数组
	// 读取时,会把读出来的字节放进字节数组中存储
	byte[] b = new byte[2];
	int i = fileInputStream.read(b);
//		for (byte c : b) {
//			System.out.println(c);
//		}
	// 利用字符串的构造方法直接打印字符串
	System.out.println(i);				// 2
	System.out.println(new String(b));	// ab
	i = fileInputStream.read(b);			
	System.out.println(i);				// 2
	System.out.println(new String(b));	// cd
	i = fileInputStream.read(b);
	System.out.println(i);				// 1
	System.out.println(new String(b));	// ed
	i = fileInputStream.read(b);
	System.out.println(i);				// -1
	System.out.println(new String(b));	// ed
	
	fileInputStream.close();
}
public static void test() throws FileNotFoundException, IOException {
	// 循环读
	File file = new File("/Users/Desktop/Test/Test.txt");
	FileInputStream fileInputStream = new FileInputStream(file);
	// read 方法没调用一次,就读取一次
	// 循环的时候,read 方法只能出现一次
	// 声明一个变量保存,读出来的结果
	int read = 0;
	while((read = fileInputStream.read()) != -1) {
		System.out.println(read);
	}
	fileInputStream.close();
}
public static void test() throws FileNotFoundException, IOException {
	File file = new File("/Users/Desktop/Test/Test.txt");
	FileInputStream fileInputStream = new FileInputStream(file);
	// 读取
	int read = fileInputStream.read();
	System.out.println((char)read);
	read = fileInputStream.read();
	System.out.println((char)read);
	read = fileInputStream.read();
	System.out.println((char)read);
	read = fileInputStream.read();
	System.out.println((char)read);
	read = fileInputStream.read();
	System.out.println((char)read);
	// 当读取到文件末尾的时候会返回-1
	// 相当于读取到-1,文件就读取完了
	read = fileInputStream.read();
	System.out.println(read);
	//关闭资源
	fileInputStream.close();
}
/*
 * 使用字节的输入输出流进行文件的复制
 * (字节流不但可以写文本,还可以写图片、音频、视频等等...)
 */
public class Demo {
	public static void main(String[] args) {
		// 获取系统时间
		long Start = System.currentTimeMillis();
		// 异常处理的复制文件
		// 读
		FileInputStream fis = null;
		// 写
		FileOutputStream fos = null;
		try {
			fis = new FileInputStream("/Users/Desktop/01/wl.zip");
			fos = new FileOutputStream("/Users/Desktop/02/wl2.zip");
			// 开始读文件,在读的同时写入文件
			/*
			 * 一个字节一个字节读
			 * int len = 0;
			 * while ((len = fis.read()) != -1) {
			 * // 直接将读出的字节写入文件中
			 * fos.write(len);
			 * }
			 */
			/*
			 * 用字节数组的方式读写    利用缓冲数组读写,相对于字节读写,效率较高
			 */
			byte[] b = new byte[1024];
			int len = 0;
			while((len = fis.read(b)) != -1) {
				fos.write(b, 0, len);
			}
		} catch (FileNotFoundException e) {
			throw new RuntimeException("文件路径有误,无法找到该文件!");
		} catch (IOException e) {
			throw new RuntimeException("文件读取失败!");
		} finally {
			try {
				if (fis != null) {
					fis.close();
				}
			} catch (IOException e) {
				throw new RuntimeException("资源关闭失败!");
			} finally {
				// 不能与上一个写一起,如果第一个报了异常,这个也会无法关闭
				try {
					if (fos != null) {
						fos.close();
					}
				} catch (IOException e) {
					throw new RuntimeException("资源关闭失败!");
				}
			}
		}
		long end = System.currentTimeMillis();
		long speed = end - Start;
		System.out.println(speed);
	}
}

/*
 * 需求:
 * 将一个文件夹复制到另一个文件夹下
 * 
 * 分析思路
 * 源文件(文件夹 Test) ---> 目标文件(文件夹 XTest)
 * 1.目标文件夹下,创建一个文件夹出来
 *   Test 文件夹,创建在 XTest 目录下
 * 2.遍历源文件夹,找出文件
 *   把文件读写到目标路径下
 */
public class Demo {
	public static void main(String[] args) throws IOException {
		// 测试
		File src = new File("/Users/Desktop/Test");
		File dest = new File("/Users/Desktop/XTest");
		copyDir(src, dest);
	}
	/*
	 * 复制文件夹方法
	 * 参数
	 * src源文件(要被复制的)
	 * dest目标文件(要把源文件夹复制进去)
	 */
	public static void copyDir(File src, File dest) throws IOException{
		// 在目标文件夹下创建新的文件夹,名字和源文件夹一样
		File newDir = new File(dest, src.getName());// 拼接路径
		newDir.mkdir();// 创建文件夹
		/*
		 * 复制文件
		 * 找到文件
		 *   一边读文件一边写文件
		 * 找到文件夹
		 *   继续调用本方法
		 */
		// 遍历源文件夹
		File[] listFiles = src.listFiles();
		for (File subFile : listFiles) {
			// 判断是否是文件
			if(subFile.isFile()) {
				// 是文件,进行读写
				FileInputStream fis = new FileInputStream(subFile);
				// 要在新建的文件夹下写入 newDir + subFile.getName()
				File tempFile = new File(newDir, subFile.getName());
				FileOutputStream fos = new FileOutputStream(tempFile);
				// 循环读写(使用数组)
				byte[] b = new byte[1024];
				int len = 0;
				while ((len = fis.read(b)) != -1) {
					fos.write(b, 0, len);
				}
				// 关闭资源
				fis.close();
				fos.close();
			} else {
				// 是文件夹,需要创建文件夹,然后继续遍历读写
				copyDir(subFile, newDir);
			}
		}
	}
}

/*
 * 需求:
 * 将一个文件夹下的所有 txt 文件复制到另一个文件夹下
 * 
 * 分析思路:
 * 源文件(文件夹 Test) ---> 目标文件夹(XTest)
 * 1.遍历目标文件夹
 *   找到文件
 *     是 txt 文件,保存到目标文件夹下
 *     不是 txt 文件,不执行操作
 *   找到文件夹
 *     重新调用此方法,在下一级文件夹下遍历
 */
public class Demo03 {
	public static void main(String[] args) throws IOException {
		// 测试
		File src = new File("/Users/Desktop/Test");
		File dest = new File("/Users/Desktop/XTest");
		copyDirTXTFIle(src, dest);
	}
	public static void copyDirTXTFIle(File src, File dest) throws IOException {
		File[] listFiles = src.listFiles(new MyFilterByTxt());
		// 遍历源文件
		for (File subFile : listFiles) {
			// 判断是否是文件
			if (subFile.isFile()) {
				// 是文件,进行读写
				FileInputStream fis = new FileInputStream(subFile);
				// 拼接
				File tempFile = new File(dest, subFile.getName());
				FileOutputStream fos = new FileOutputStream(tempFile);
				// 循环读写(使用数组)
				byte[] b = new byte[1024];
				int len = 0;
				while((len = fis.read(b)) != -1) {
					fos.write(b, 0, len);
				}
				// 关闭资源
				fis.close();
				fos.close();
			} else {
				copyDirTXTFIle(subFile, dest);
			}
		}
	}
}
// 使用文件过滤器过滤掉不是 txt 的文件
// 文件夹不需要过滤
// 过滤器
class MyFilterByTxt implements FileFilter{
	@Override
	public boolean accept(File pathname) {
		// 判断是文件夹,直接返回 true,不被过滤
		if (pathname.isDirectory()) {
			return true;
		}
		// 返回 txt 文件
		return pathname.getName().endsWith("txt");
	}
}

http://blog.csdn.net/huzongnan/article/list

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值