基于字节的IO操作
2016-08-05 17:27
127 查看
包括:
FileInputStream
ByteArrayInputStream
ObjectInputStream
BufferedInputStream
DataInputStream
上面 5 个为常用的基于字节操作的 IO 操作类,其中前三个 相当于 装饰器模式的 ConcreteComponent,后两个是具体的装饰者类。传送门:装饰器模式
一. FileInputStream
用于读取文件内容。常用的有两种构造函数,例如:
[java] view
plain copy
![](https://code.csdn.net/assets/CODE_ico.png)
![](https://code.csdn.net/assets/ico_fork.svg)
public FileInputStream(String name) throws FileNotFoundException {
this(name != null ? new File(name) : null);
}
public FileInputStream(File file) throws FileNotFoundException {
...
}
其中 name 和 file 都是指向 需要读取的文件。Demo:
[java] view
plain copy
![](https://code.csdn.net/assets/CODE_ico.png)
![](https://code.csdn.net/assets/ico_fork.svg)
public class Test{
public static void main(String[] args) {
InputStream inputStream = null;
try {
inputStream = new FileInputStream("/Users/yunxin/Desktop/a");
byte data[] = new byte[inputStream.available()];
inputStream.read(data);
System.out.println(new String(data));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(inputStream != null){
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
需要注意的是 try-catch-finally
二. ByteArrayInputStream
ByteArrayInputStream,主要是应对流的来源和目的地不一定是文件这种情况,比如说可能是内存,可能是数组。例如:
[java] view
plain copy
![](https://code.csdn.net/assets/CODE_ico.png)
![](https://code.csdn.net/assets/ico_fork.svg)
public class Test{
public static void main(String[] args) {
byte data[] = "abc".getBytes();
InputStream inputStream = new ByteArrayInputStream(data);
byte data0[] = new byte[data.length];
try {
inputStream.read(data0);
System.out.println(new String(data0));
} catch (IOException e) {
e.printStackTrace();
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
三. ObjectInputStream
ObjectInputStream 可以用于读取对象,但是读取的对象必须实现 Serializable 接口。
[java] view
plain copy
![](https://code.csdn.net/assets/CODE_ico.png)
![](https://code.csdn.net/assets/ico_fork.svg)
public class Test{
public static void main(String[] args) {
ObjectOutputStream objectOutputStream = null;
try {
objectOutputStream = new ObjectOutputStream(new FileOutputStream("/Users/yunxin/Desktop/a"));
User user = new User();
user.setNum(2);
objectOutputStream.writeObject(user);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(objectOutputStream != null){
try {
objectOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
class User implements Serializable{
private static final long serialVersionUID = -8987587467273881932L;
private int num;
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
@Override
public String toString() {
return "User [num=" + num + "]";
}
}
四. BufferedInputStream
BufferedInputStream 提供了一个缓冲的功能,可以避免大量的磁盘IO。因为像FileInputStream 这种,每一次的读取,都是一次磁盘IO。
[java] view
plain copy
![](https://code.csdn.net/assets/CODE_ico.png)
![](https://code.csdn.net/assets/ico_fork.svg)
public class Test{
public static void main(String[] args) {
BufferedInputStream bufferedInputStream = null;
try {
bufferedInputStream = new BufferedInputStream(new FileInputStream("/Users/yunxin/Desktop/a"));
byte data[] = new byte[bufferedInputStream.available()];
bufferedInputStream.read(data);
System.out.println(new String(data));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(bufferedInputStream != null){
try {
bufferedInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
五. DataInputStream
该类的主要作用是可以返回一些基本类型或者是String类型,否则的话,只能返回byte类型的数据,利用该类,我们可以更好的操作数据。
[java] view
plain copy
![](https://code.csdn.net/assets/CODE_ico.png)
![](https://code.csdn.net/assets/ico_fork.svg)
public class Test{
public static void main(String[] args) {
DataOutputStream dataOutputStream = null;
try {
dataOutputStream = new DataOutputStream(new FileOutputStream("/Users/yunxin/Desktop/a"));
dataOutputStream.writeInt(999);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(dataOutputStream != null){
try {
dataOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
FileInputStream
ByteArrayInputStream
ObjectInputStream
BufferedInputStream
DataInputStream
上面 5 个为常用的基于字节操作的 IO 操作类,其中前三个 相当于 装饰器模式的 ConcreteComponent,后两个是具体的装饰者类。传送门:装饰器模式
一. FileInputStream
用于读取文件内容。常用的有两种构造函数,例如:
[java] view
plain copy
![](https://code.csdn.net/assets/CODE_ico.png)
public FileInputStream(String name) throws FileNotFoundException {
this(name != null ? new File(name) : null);
}
public FileInputStream(File file) throws FileNotFoundException {
...
}
其中 name 和 file 都是指向 需要读取的文件。Demo:
[java] view
plain copy
![](https://code.csdn.net/assets/CODE_ico.png)
public class Test{
public static void main(String[] args) {
InputStream inputStream = null;
try {
inputStream = new FileInputStream("/Users/yunxin/Desktop/a");
byte data[] = new byte[inputStream.available()];
inputStream.read(data);
System.out.println(new String(data));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(inputStream != null){
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
需要注意的是 try-catch-finally
二. ByteArrayInputStream
ByteArrayInputStream,主要是应对流的来源和目的地不一定是文件这种情况,比如说可能是内存,可能是数组。例如:
[java] view
plain copy
![](https://code.csdn.net/assets/CODE_ico.png)
public class Test{
public static void main(String[] args) {
byte data[] = "abc".getBytes();
InputStream inputStream = new ByteArrayInputStream(data);
byte data0[] = new byte[data.length];
try {
inputStream.read(data0);
System.out.println(new String(data0));
} catch (IOException e) {
e.printStackTrace();
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
三. ObjectInputStream
ObjectInputStream 可以用于读取对象,但是读取的对象必须实现 Serializable 接口。
[java] view
plain copy
![](https://code.csdn.net/assets/CODE_ico.png)
public class Test{
public static void main(String[] args) {
ObjectOutputStream objectOutputStream = null;
try {
objectOutputStream = new ObjectOutputStream(new FileOutputStream("/Users/yunxin/Desktop/a"));
User user = new User();
user.setNum(2);
objectOutputStream.writeObject(user);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(objectOutputStream != null){
try {
objectOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
class User implements Serializable{
private static final long serialVersionUID = -8987587467273881932L;
private int num;
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
@Override
public String toString() {
return "User [num=" + num + "]";
}
}
四. BufferedInputStream
BufferedInputStream 提供了一个缓冲的功能,可以避免大量的磁盘IO。因为像FileInputStream 这种,每一次的读取,都是一次磁盘IO。
[java] view
plain copy
![](https://code.csdn.net/assets/CODE_ico.png)
public class Test{
public static void main(String[] args) {
BufferedInputStream bufferedInputStream = null;
try {
bufferedInputStream = new BufferedInputStream(new FileInputStream("/Users/yunxin/Desktop/a"));
byte data[] = new byte[bufferedInputStream.available()];
bufferedInputStream.read(data);
System.out.println(new String(data));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(bufferedInputStream != null){
try {
bufferedInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
五. DataInputStream
该类的主要作用是可以返回一些基本类型或者是String类型,否则的话,只能返回byte类型的数据,利用该类,我们可以更好的操作数据。
[java] view
plain copy
![](https://code.csdn.net/assets/CODE_ico.png)
public class Test{
public static void main(String[] args) {
DataOutputStream dataOutputStream = null;
try {
dataOutputStream = new DataOutputStream(new FileOutputStream("/Users/yunxin/Desktop/a"));
dataOutputStream.writeInt(999);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(dataOutputStream != null){
try {
dataOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
相关文章推荐
- Java IO:基于字节的IO操作
- 高级字符驱动程序操作之异步通知IO(实践篇)基于内核2.6.35-30
- Java io文件操作(按字符、字节、行、随机读取,追加,文件操作)
- JAVA-IO操作,字节-字符转换流
- 虚拟机字节码操作引擎-----基于栈的字节码解释引擎
- STM8S---选项字节(Option Byte)写操作之IO复用
- Java IO操作与字节流
- STM8S---选项字节(Option Byte)写操作之IO复用
- java IO 字节流、字符流操作总结二之字节流
- stm32之IO操作(基于库函数)
- 基于缓冲字节输入输出流进行复制文件的操作
- 黑马程序员--读写字节数组,随机读写流,集合IO的思维导图,多线程部分,单例设计模式,线程和进程的概念,Java中的线程的创建方式,线程的随机性,线程的状态图,多线程操作共享数据的安全性,死锁
- java IO总结之字节流操作文件读写(高效)
- Java IO操作——字节-字符转换流(OutputStreamWriter、InputStreamReader)
- Java io文件操作(按字符、字节、行、随机读取,追加,文件操作)
- Java io文件操作(按字符、字节、行、随机读取,追加,文件操作)
- java IO总结之字节流操作文件读写
- Java的IO操作中有面向字节(Byte)和面向字符(Character)两种方式
- C++ (IO操作小案列) 基于本地文本文档的读写
- (1)基于字节的IO类(InputStream、OutputStream)