Java--IO流

IO流--输入输出流,流就是数据传输,字节集合

分类:根据数据类型不同可分为,字节流与字符流;按照数据流向的方向不同可分为输入流与输出流;按功能不同可分为,节点流(直接操作数据源,比如文件流)和处理流(处理其他流)

四个抽象类 -- InputStream,OutputStream,Reader,Writer

常用输入输出流有如下五种:

1.文件流

FileInputStream,FileOutputStream;FileReader,FileWriter

下面分别是字节输入输出文件流,字符输入输出文件流 -- 节点流

		try (FileInputStream fis = new FileInputStream("./src/IOStream/InputStream_.java")) {
			// 读取一个字节,并返回读取的数据
//			System.out.println((char)fis.read());
//			int temp=0;
//			while((temp=fis.read())!=-1) {
//				System.out.print((char)temp);
//			}
			byte[] bytes = new byte[100];

			// 读取一个字节数组,并返回读取个数
//			System.out.println(fis.read(bytes));
			int temp = 0;
			while ((temp = fis.read(bytes)) != -1) {
				System.out.print(new String(bytes));
			}
//			System.out.println(Arrays.toString(bytes));
		} catch (IOException e) {
			e.printStackTrace();
		}
		try (FileReader fr = new FileReader("./src/IOStream/Reader_.java");) {
//			int temp=0;
//			while((temp=fr.read())!=-1) {
//				System.out.print((char)temp);
//			}
			char[] chars = new char[200];
			int temp = 0;
//			fr.read(chars);
//			System.out.print(new String(chars));
			while ((temp = fr.read(chars)) != -1) {
				System.out.print(new String(chars));
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

2.缓存流

BufferedWriter,BufferedReader,BufferedInputStream,BufferedOutputStream

  • 缓存流,主要是为了提高效率
  • 关闭缓冲流时也会自动关闭节点流
  •   字符读取缓存流--新增readLine方法,一行一行读取,不读取换行符,返回类型null,到末尾返回null
try (BufferedReader br = new BufferedReader(new FileReader("D:/train/test.txt"));
				BufferedWriter bw = new BufferedWriter(new FileWriter("D:/train/day1/copy1.txt"));) {
			int t;
			char[] chars = new char[30];
			while ((t = br.read(chars)) != -1) {
//			System.out.println(br.readLine());
				bw.write(chars);
				// 换行方法,缓冲流新增方法
				bw.newLine();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

		try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:/train/test.txt"));
				BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D:/train/test.txt"));) {
			// 方法与文件输入输出流一样
		} catch (IOException e) {
			e.printStackTrace();
		}

3.转换流

字节转换流,当文件编码发生乱码时使用,一个汉字占两个字节,所以读取时可能会读取一般以至于出现乱码

(字符串编码异常时,new String("乱码的字符串".getBytes("ISO-8859-1"),"UTF-8"),第二个参数码格式)

InputStreamReader,OutputStreamWriter

		try (Reader r = new InputStreamReader(new FileInputStream("D:/train/day1/copy.txt"));
				Writer w = new OutputStreamWriter(new FileOutputStream("D:/train/day1/copyConvertCharset.txt"),
						"ISO-8859-1")) {
			char[] chars = new char[1024];
			int len;
			while ((len = r.read(chars)) != -1) {
				System.out.println(chars);
				w.write(chars, 0, len);
			}

		} catch (IOException e) {
			e.printStackTrace();
		}

4.数据流

DataInputStream,为了个平台之间数据读取的一致性

Linux与Windows操作系统的存储数据的方式是不一样的

try (DataInputStream dis = new DataInputStream(new FileInputStream("D:/train/day1/copy2.txt"));
				DataOutputStream dos = new DataOutputStream(new FileOutputStream("D:/train/day1/copy2.txt"));) {
			// 以特定类型写出
			dos.writeLong(34);
			dos.writeInt(45666);
			dos.writeUTF("真的等");// 字符串
			// 读取要按照写出的顺序进行读取,否则结果不正确
//			System.out.println(dis.readInt());
			System.out.println(dis.readLong());
			System.out.println(dis.readInt());
			System.out.println(dis.readUTF());
		} catch (IOException e) {
			e.printStackTrace();
		}

5.打印流

打印流,System类中有setIn(InputStream in),setOut(PrintStream out) -- 重定向输入输出流

		try (
				// 字节打印流
				PrintStream ps = new PrintStream(new FileOutputStream("D:/train/day1/copy2.txt"));
				// 字符打印流
				PrintWriter pw = new PrintWriter(new FileWriter("D:/train/day1/copy2.txt"));) {
			ps.print(123);
			ps.print(false);
			ps.print("随便");

			pw.print("suibian");
			ps.print(12321);
			ps.print(true);
			// 刷缓存
			pw.flush();

			// 改变输出的位置,使其输出到ps的文件中
			System.setOut(ps);
			System.out.println("======");
		} catch (IOException e) {
			e.printStackTrace();
		}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值