前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >字节流---输入输出

字节流---输入输出

作者头像
shimeath
发布2020-07-30 18:41:31
3770
发布2020-07-30 18:41:31
举报
文章被收录于专栏:shimeath的Java学习

流的输入及输出均是对程序而言

字节流输入

  1. 创建File对象,关联文件
代码语言:javascript
复制
File src = new File("D:/aa", "a.txt");
  1. 1)、创建以src为输入流的对象,
代码语言:javascript
复制
Inputstream in = new FileInputStream(src);
  1. 2)、建立字节数组(byte[]),创建长度整形变量len
代码语言:javascript
复制
byte[] car = new byte[10];
int len = 0;
  1. 3)、读取输入流,将byte[]转换为String并输出
代码语言:javascript
复制
while (-1 != (len = in.read(car))) {
	String info = new String(car, 0, len);
	System.out.println(info);
}

read()返回读入长度,为-1时结束

2. 4)、读取结束后关闭输入流

代码语言:javascript
复制
in.close();
  1. 进行异常处理
代码语言:javascript
复制
try {
	in = new FileInputStream(src);
	byte[] car = new byte[10];
	int len = 0;
	while (-1 != (len = in.read(car))) {
		String info = new String(car, 0, len);
		System.out.println(info);
	}
} catch (FileNotFoundException e) {
	System.out.println("文件读取失败");
} catch (IOException e) {
	System.out.println("文件流操作失败");
} finally {
	if (in != null) {
		try {
			in.close();
		} catch (IOException e) {
			System.out.println("关闭输入流失败");
		}
	}
}

字节流输出

  1. 创建File对象,关联文件
代码语言:javascript
复制
File src = new File("D:/aa", "a.txt");
  1. 1)、创建以src为输出流的对象,
代码语言:javascript
复制
Outputstream in = new FileOutputStream(src);
  1. 2)、建立字符串,转换为字节数组并写入
代码语言:javascript
复制
String str = "simple\n";
out.write(str.getBytes());
  1. 3)、刷新输出流
代码语言:javascript
复制
out.flush();

flush()方法使流中的数据写入文件

2. 4)、读取结束后关闭输出流

代码语言:javascript
复制
out.close();
  1. 进行异常处理
代码语言:javascript
复制
try {
	out = new FileOutputStream(src, true);
	String str = "simple\n";
	out.write(str.getBytes());
	out.flush();
} catch (FileNotFoundException e) {
	System.out.println("文件不存在");
} catch (IOException e) {
	System.out.println("文件流操作失败");
} finally {
	if (out != null) {
		try {
			out.close();
		} catch (IOException e) {
			System.out.println("关闭输出流失败");
		}
	}
}

完整代码(输入):

代码语言:javascript
复制
package cn.hxh.io.filebyte;

import java.io.*;

public class myInputStream {

	public static void main(String[] args) {
		File src = new File("D:/aa", "a.txt");
		InputStream in = null;
		try {
			in = new FileInputStream(src);
			byte[] car = new byte[10];
			int len = 0;
			while (-1 != (len = in.read(car))) {
				String info = new String(car, 0, len);
				System.out.println(info);
			}
		} catch (FileNotFoundException e) {
			System.out.println("文件读取失败");
		} catch (IOException e) {
			System.out.println("文件流操作失败");
		} finally {
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					System.out.println("关闭输入流失败");
				}
			}
		}
	}

}

完整代码(输出):

代码语言:javascript
复制
package cn.hxh.io.filebyte;

import java.io.*;

public class myOutputStream {

	public static void main(String[] args) {
		File src = new File("D:/aa", "a.txt");
		OutputStream out = null;
		try {
			out = new FileOutputStream(src, true);
			String str = "simple\n";
			out.write(str.getBytes());
			out.flush();
		} catch (FileNotFoundException e) {
			System.out.println("文件不存在");
		} catch (IOException e) {
			System.out.println("文件流操作失败");
		} finally {
			if (out != null) {
				try {
					out.close();
				} catch (IOException e) {
					System.out.println("关闭输出流失败");
				}
			}
		}
	}

}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-01-26 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 字节流输入
  • 字节流输出
    • 完整代码(输入):
      • 完整代码(输出):
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档