博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java IO操作(6)
阅读量:4031 次
发布时间:2019-05-24

本文共 5310 字,大约阅读时间需要 17 分钟。

IO流主要用于硬板、内存、键盘等处理设备上得数据操作,根据处理数据的数据类型的不同可以分为:字节流(抽象基类为InPutStream和OutPutStream)和字符流(抽象基类为Reader和Writer)。根据流向不同,可以分为:输入流和输出流。  其中主要结构可以用下图来表示:                          

                        (转自:http://blog.csdn.net/zzp_403184692/article/details/8057693)

      

 字符流和字节流的主要区别:

       1.字节流读取的时候,读到一个字节就返回一个字节;  字符流使用了字节流读到一个或多个字节(中文对应的字节数是两个,在UTF-8码表中是3个字节)时。先去查指定的编码表,将查到的字符返回。

       2.字节流可以处理所有类型数据,如:图片,MP3,AVI视频文件,而字符流只能处理字符数据。只要是处理纯文本数据,就要优先考虑使用字符流,除此之外都用字节流。

(转自:http://blog.csdn.net/zzp_403184692/article/details/8057693)

其他总结:

1、http://blog.csdn.net/whuhan2013/article/details/51539591

2、http://blog.csdn.net/u013087513/article/details/51956801

()

3、http://blog.csdn.net/jinhongliang123/article/details/7931729 (通过“套接”图表示输入/输出原理)

 (箭头貌似标反了)

一些代码:(根据 马士兵 视频整理)

import java.io.*;//流 需求包import java.util.*;//Date类需求包/*范例名称: * 原文件名称: * 要点: * 1. 输入流、输出流 (相对程序来说) * 2. (字节流 --1字节 InputStream/OutputStream vs 字符流 --2字节 Reader/Writer) * 3. (节点流  vs 处理流--缓冲流、转换流、数据流、Print流、Object流)  */public class StreamTest {	public static void main(String[] args) {		// 创建文件		File dirCurrent = new File("");		String strCurrent = dirCurrent.getAbsolutePath();// 获取当前类的绝对路径		String pathStr = strCurrent + File.separator + "javaTest";// separator路径分隔符		String fileName = "test.txt";		File dirFile = new File(pathStr);		if (!dirFile.exists()) {			dirFile.mkdirs();// 创建目录		}		File file = new File(pathStr, fileName);		if (!file.exists()) {			try {				file.createNewFile();// 创建文件			} catch (IOException e) {				// TODO: handle exception				e.printStackTrace();			}		}		// FileOutputStream(节点流)		String fileName2 = "test2.txt";		File file2 = new File(pathStr, fileName2);// 创建文件		if (!file2.exists()) {			try {				file2.createNewFile();			} catch (IOException e) {				// TODO: handle exception				e.printStackTrace();			}		}		FileInputStream in = null;		FileOutputStream out = null;		int b = 0;		try {			in = new FileInputStream(strCurrent + File.separator + "StreamTest.java");			out = new FileOutputStream(file2);			while ((b = in.read()) != -1) {				out.write((char) b);// 字节流			}			in.close();			out.close();		} catch (FileNotFoundException e) {			// TODO: handle exception			System.out.println("找不到指定文件");			System.exit(-1);		} catch (IOException e) {			// TODO: handle exception			System.out.println("文件复制错误");			System.exit(-1);		}		// Buffer缓冲流(处理流)		try {			BufferedWriter bw = new BufferedWriter(new FileWriter(file));			BufferedReader br = new BufferedReader(new FileReader(file));			String strTest = null;			for (int i = 0; i <= 10; i++) {				strTest = String.valueOf(Math.random());				bw.write(strTest);				bw.newLine();			}			bw.flush();// 使内存中的数据立刻写出			while ((strTest = br.readLine()) != null) {				System.out.println(strTest);			}			bw.close();// 关闭写缓冲区			br.close();// 关闭读缓冲区		} catch (IOException e) {			e.printStackTrace();		}		// 转换流		try {			// OutputStreamWriter字符流转字节流			OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(file, true), "ISO8859_1");// true表示追加			osw.write("Microsoft win7");// 直接写字符串进去啦			System.out.println(osw.getEncoding());			osw.close();		} catch (IOException e) {			// TODO: handle exception			e.printStackTrace();		}		/*		// 使用了2根“管道”读取键盘输入,然后输出显示		// InputStreamReader字节流转字符流		InputStreamReader isr = new InputStreamReader(System.in);		// BufferedReader的readLine好使		BufferedReader br = new BufferedReader(isr);		String s = null;		try {			System.out.println("请键盘输入字符串:回车结束");			// System.in是阻塞式			s = br.readLine();			while (s != null) {				if (s.equalsIgnoreCase("exit"))					break;				System.out.println(s.toUpperCase());				System.out.println("请键盘输入字符串:回车结束");				s = br.readLine();			}			br.close();		} catch (IOException e) {			// TODO: handle exception			e.printStackTrace();		}		*/		// 数据流 (先进先出)		ByteArrayOutputStream baos = new ByteArrayOutputStream();		DataOutputStream dos = new DataOutputStream(baos);// 加 “管道”		try {			dos.writeDouble(Math.random());// 8字节			dos.writeBoolean(true);// 1字节			ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());			System.out.println(bais.available());// 剩余字节数			DataInputStream dis = new DataInputStream(bais);			System.out.println(dis.readDouble());			System.out.println(dis.readBoolean());			dos.close();			dis.close();		} catch (IOException e) {			// TODO: handle exception			e.printStackTrace();		}		// Print 流 (跟上边 “InputStreamReader字节流转字符流” 注释的部分 一次运行一个)		String strTemp = null;		BufferedReader br2 = new BufferedReader(new InputStreamReader(System.in));		try {			FileWriter fw = new FileWriter(file,true);// 追加			PrintWriter log = new PrintWriter(fw);//Print管道添加到file文件上			while ((strTemp = br2.readLine()) != null) {				if (strTemp.equalsIgnoreCase("exit"))					break;				System.out.println(strTemp.toUpperCase());				log.println("-----");				log.println(strTemp.toUpperCase());				log.flush();			}			log.println("===" + new Date() + "===");			log.flush();			log.close();			br2.close();		} catch (IOException e) {			// TODO: handle exception			e.printStackTrace();		}				//Object流(必须序列化相应的object)		T t=new T();		try{			FileOutputStream fos=new FileOutputStream(file,true);			ObjectOutputStream oos=new ObjectOutputStream(fos);			oos.writeObject(t);//写Object流			oos.flush();			oos.close();					}catch(IOException e){			e.printStackTrace();		}	}}//Serializable--标记化接口class T implements Serializable{	int i=1;	int j=2;	double d=1.1;	transient int k=33;//序列化时不予考虑}

你可能感兴趣的文章
socket,accept函数解析
查看>>
今日互联网关注(写在清明节后):每天都有值得关注的大变化
查看>>
”舍得“大法:把自己的优点当缺点倒出去
查看>>
[今日关注]鼓吹“互联网泡沫,到底为了什么”
查看>>
[互联网学习]如何提高网站的GooglePR值
查看>>
[关注大学生]求职不可不知——怎样的大学生不受欢迎
查看>>
[关注大学生]读“贫困大学生的自白”
查看>>
[互联网关注]李开复教大学生回答如何学好编程
查看>>
[关注大学生]李开复给中国计算机系大学生的7点建议
查看>>
[关注大学生]大学毕业生择业:是当"鸡头"还是"凤尾"?
查看>>
[茶余饭后]10大毕业生必听得歌曲
查看>>
gdb调试命令的三种调试方式和简单命令介绍
查看>>
C++程序员的几种境界
查看>>
VC++ MFC SQL ADO数据库访问技术使用的基本步骤及方法
查看>>
VUE-Vue.js之$refs,父组件访问、修改子组件中 的数据
查看>>
Vue-子组件改变父级组件的信息
查看>>
Python自动化之pytest常用插件
查看>>
Python自动化之pytest框架使用详解
查看>>
【正则表达式】以个人的理解帮助大家认识正则表达式
查看>>
性能调优之iostat命令详解
查看>>