常用的IO流
常用的IO流
•根据处理数据类型的不同分为:字节流和字符流
•根据数据流向不同分为:输入流和输出流
字节流:
字节流以字节(8bit)为单位,能处理所有类型的数据(如图片、avi等)。
字节输入流:
InputStream 是所有的输入字节流的父类,它是一个抽象类。
常用的字节输入流:
ByteArrayInputStream、StringBufferInputStream、FileInputStream、PipedInputStream,
它们分别从Byte 数组、StringBuffer、本地文件中、和从与其它线程共用的管道中读取数据。
ObjectInputStream 和所有FilterInputStream 的子类都是装饰流。
字节输出流:
OutputStream 是所有的输出字节流的父类,它是一个抽象类。
常用的字节输出流:
2.ByteArrayOutputStream、FileOutputStream、PipedOutputStream,它们分别向Byte 数组、本地文件、和向与其它线程共用的管道中写入数据。
ObjectOutputStream 和所有FilterOutputStream 的子类都是装饰流。
字符流:
只能处理字符类型的数据,因为数据编码的不同,而有了对字符进行高效操作的流对象。
本质其实就是基于字节流读取时,去查了指定的码表。读写以字符为单位,一次可能读多个字节。
字符输入流:
Reader 是所有的输入字符流的父类,它是一个抽象类。
常见的字符输入流:
CharReader、StringReader、PipedReader
InputStreamReader 是一个连接字节流和字符流的桥梁,可对读取到的字节数据经过指定编码转换成字符。
FileReader 可以说是一个达到此功能、常用的工具类
BufferedReader 是一个装饰器,它和其子类负责装饰其它Reader 对象。
FilterReader 是所有自定义具体装饰流的父类
字符输出流
Writer 是所有的输出字符流的父类,它是一个抽象类。
常见的字符输出流:
CharArrayWriter、StringWriter、PipedWriter、PrintWriter
OutputStreamWriter 是OutputStream 到Writer 转换的桥梁,可对读取到的字符数据经过指定编码转换成字节。
FileWriter 可以说是一个达到此功能、常用的工具类
BufferedWriter 是一个装饰器为Writer 提供缓冲功能。
字节流的例子:
1 /**
2 * @ 字节流
3 */
4 public static void streamDemo() {
5 try {
6 // 要读入的文件
7 File inputFile = new File("d:\\book.txt");
8 // 文件不存在的话,什么也不做
9 if (!inputFile.exists()) {
10 System.out.println("file is not exists!");
11 return;
12 }
13 // 读入流
14 InputStream in = new FileInputStream(inputFile);
15 // 输出流
16 OutputStream out = new FileOutputStream(new File("d:\\book" + System.currentTimeMillis() + ".txt"));
17 // 读到的字节数
18 int readBytes;
19 byte [] buffer = new byte [1024];
20 while ((readBytes = in.read(buffer)) != -1) {
21 out.write(buffer,0,readBytes);
22 }
23 out.flush();
24 out.close();
25 in.close();
26 } catch (IOException e) {
27 e.printStackTrace();
28 }
29 }
字符流的例子:
1 /**
2 * @ 字符流
3 */
4 public static void readerDemo() {
5 try {
6 // 要读入的文件
7 File inputFile = new File("d:\\book.txt");
8 // 文件不存在的话,什么也不做
9 if (!inputFile.exists()) {
10 System.out.println("file is not exists!");
11 return;
12 }
13 // 读入流
14 InputStreamReader in = new FileReader(inputFile);
15 BufferedReader bf = new BufferedReader(in);
16 // 输出流
17 PrintWriter out = new PrintWriter(new File("d:\\book" + System.currentTimeMillis() + ".txt"));
18 String str;
19 while ((str = bf.readLine()) != null) {
20 System.out.println(str);
21 out.println(str);
22 }
23 out.flush();
24 out.close();
25 bf.close();
26
27 } catch (IOException e) {
28 e.printStackTrace();
29 }
30 }
编码转换的例子:
1 /**
2 * @ 编码转换
3 */
4 public static void gbk2utf8Demo() {
5 try {
6 // 要读入的文件
7 File inputFile = new File("d:\\book.txt");
8 // 文件不存在的话,什么也不做
9 if (!inputFile.exists()) {
10 System.out.println("file is not exists!");
11 return;
12 }
13 // 读入时文件编码为gbk
14 InputStreamReader in = new InputStreamReader(new FileInputStream(inputFile),"gbk");
15 BufferedReader bf = new BufferedReader(in);
16 // 输出时文件编码变为utf-8
17 OutputStreamWriter outwriter = new OutputStreamWriter(new FileOutputStream(new File("d:\\book" + System.currentTimeMillis() + ".txt")),"utf-8");
18 PrintWriter out = new PrintWriter(outwriter,true);
19 String str;
20 // 一直读到文件的最后为止
21 while ((str = bf.readLine()) != null) {
22 System.out.println(str);
23 out.println(str);
24 }
25 // 关闭流
26 out.flush();
27 out.close();
28 bf.close();
29
30 } catch (IOException e) {
31 e.printStackTrace();
32 }
33 }
对象序列化与反序列化的例子:
class User implements Serializable{
String user_id;
String user_nm;
int age;
}
/**
* @ 对象的序列化与反序列化
*/
public static void serializeDemo() {
try {
// 输出流的定义
File file = new File("d:\\book" + System.currentTimeMillis() + ".dat");
ObjectOutputStream ob = new ObjectOutputStream(new FileOutputStream(file));
// 输出内容设定
User user1 = new User();
user1.user_id = "110";
user1.user_nm = "police";
user1.age = 25;
// 输出序列化对象
ob.writeObject(user1);
// 关闭输出流
ob.close();
// 打开输入流
ObjectInputStream in = new ObjectInputStream(new FileInputStream(file));
// 读入序列化对象
User user = (User)in.readObject();
System.out.println("userid: " + user.user_id);
System.out.println("username: " + user.user_nm);
System.out.println("age: " + user.age);
// 关闭输入流
in.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
读写特定类型数据的例子:
/**
* @ 读写特定数据类型的流
*/
public static void dataOutputStreamDemo () {
try {
// 输出流
File file = new File("d:\\data.dat");
FileOutputStream os = new FileOutputStream(file,false);
DataOutputStream out = new DataOutputStream(os);
// 布尔型
boolean val0 = true;
// 整型
int val1 = 110;
// 浮点型
double val2 = 110.011;
// 输出
out.writeBoolean(val0);
out.writeInt(val1);
out.writeDouble(val2);
// 关闭输出流
out.flush();
out.close();
// 输入流
FileInputStream is = new FileInputStream(file);
DataInputStream in = new DataInputStream(is);
// 打印结果
System.out.println(in.readBoolean());
System.out.println(in.readInt());
System.out.println(in.readDouble());
// 关闭输入流
in.close();
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
常用的IO流的更多相关文章
- java操作文件常用的 IO流对象
1.描述:流是字节数据或字符数据序列.Java采用输入流对象和输出流对象来支持程序对数据的输入和输出.输入流对象提供了数据从源点流向程序的管道,程序可以从输入流对象读取数据:输出流对象提供了数据从程序 ...
- 16个常用IO流
在包java.io.*:下 有以下16个常用的io流类: (Stream结尾的是字节流,是万能流,通常的视频,声音,图片等2进制文件, Reader/Writer结尾的是字符流,字符流适合读取纯文本文 ...
- JavaSE(十二)之IO流的字节流(一)
前面我们学习的了多线程,今天开始要学习IO流了,java中IO流的知识非常重要.但是其实并不难,因为他们都有固定的套路. 一.流的概念 流是个抽象的概念,是对输入输出设备的抽象,Java程序中 ...
- JAVA中IO流总结
本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/42119261 我想你对JAVA的IO流有所了解,平时使用的 ...
- 我爱Java系列之《JavaEE面试宝典》---【IO流面试总结】
1.什么是比特(Bit),什么是字节(Byte),什么是字符(Char),它们长度是多少,各有什么区别 答案 Bit最小的二进制单位 ,是计算机的操作部分 取值0或者1 Byte是计算机操作数据的最小 ...
- Java学习之路(十二):IO流
IO流的概述及其分类 IO流用来处理设备之间的数据传输,Java对数据的操作是通过流的方式 Java用于操作流的类都在IO包中 流按流向分为两种:输入流(读写数据) 输出流(写数据) 流按操作 ...
- IO流详解
目录 IO流 IO流概述及其分类 IO概念 流按流向分为两种: 流按操作类型分为两种: 常用的IO流类 字节流的抽象父类: 字符流的抽象父类: InputStream & FileInputS ...
- java IO流的API
常用的IO流API有:[InputStream.OutputStream] [FileInputStream.FileOutputStream] [BufferedInputStream.Buffer ...
- Java IO流体系中常用的流分类
Java输入/输出流体系中常用的流分类(表内容来自java疯狂讲义) 注:下表中带下划线的是抽象类,不能创建对象.粗体部分是节点流,其他就是常用的处理流. 流分类 使用分类 字节输入流 字节输出流 字 ...
随机推荐
- 微软的OneDrive研究~
Dropbox 很好,唯一觉得不爽的是只能同步指定的目录.不过被墙之后就不那么方便了,所以改用微软的 Live Mesh,缺点是支持的设备少(仅 PC 和 Mac). https://technet. ...
- QT自定义窗口(模拟MainWindow)
在这里自定义窗口是通过继承于QFrame,内部分为上下两部分,上半部分就是标题栏,下面是窗口的具体内容.上下两部分通过布局固定位置.最后窗口的各种鼠标拖动,缩放,等操作通过添加鼠标事件来完成一个窗口的 ...
- [置顶] SPL讲解(6)--Condition篇
SmartPersistenceLayer 2.0 之 Condition篇 原理 强大的Condition功能是SPL的一个特性,可以使用Condition完成绝大部分的条件定义,使用 ...
- jquery中使用offset()获得的div的left=0,top=0
写东西的时候要获取div的left和top,但怎么也取不到值都为0,但在chrome的console下是可以取到值的, 瞬间就纳闷了,于是乎就在网上找各种方法,大家一般的问题可能都是要获取的div被隐 ...
- android系统的图片资源
使用系统的图片资源的好处有,一个是美工不需要重复的做一份已有的图片了,可以节约不少工时:另一个是能保证我们的应用程序的风格与系统一致. 1.引用方式 在源代码*.Java中可以进入如下方式引用: my ...
- VC++6.0出现no compile tool is associated with the extension.解决方法
对于刚解除VC++6.0的小白,在编译时候经常出现下图的错误提示: 解释为:不能编译此BmpRot.h文件,没有合适的编译工具可以编译此扩展名的文件. 很明显,当然只有.cpp文件才能编译. .h头文 ...
- ZOJ 1136 Multiple (BFS)
Multiple Time Limit: 10 Seconds Memory Limit: 32768 KB a program that, given a natural number N ...
- openstack vm image
1,openstack 基于iso生成镜像
- C:\Program Files (x86)\Common Files\microsoft shared\TextTemplating\11.0
Generating Files with the TextTransform Utility \Program Files\Common Files\Microsoft Shared\TextTem ...
- poj 3728 The merchant(LCA)
Description There are N cities in a country, and there is one and only one simple path between each ...