Java 输入流与输出流的详细介绍
2016-06-14 17:55
567 查看
转载请备注出自于:http://blog.csdn.net/qq_22118507/article/details/51673502
Java中的流分为两种:
1) 字节流:数据流中最小的数据单元是字节
2) 字符流:数据流中最小的数据单元是字符,
Java中的字符是Unicode编码,一个字符占用两个字节。
在java.io包中有四个基本类:InputStream、OutputStream及Reader、Writer类,它们分别处理字节流和字符流:java中其他多种多样变化的流均是由
它派生得来:
:
字节流InputStream/OutputStream
InputStream 为字节输入流,它本身为一个抽象类,必须依靠其子类实现各种功能,此抽象类是表示字节输入流的所有类的超类。 继承自InputStream
的流都是向程序中输入数据的,且数据单位为字节(8bit);
InputStream是输入字节数据用的类,所以InputStream类提供了3种重载的read方法.Inputstream类中的常用方法:
(1) public abstract int read( ):读取一个byte的数据,返回值是高位补0的int类型值。若返回值=-1说明没有读取到任何字节读取工作结束。
(2) public int read(byte b[ ]):读取b.length个字节的数据放到b数组中。返回值是读取的字节数。该方法实际上是调用下一个方法实现的
(3) public int read(byte b[ ], int off, int len):从输入流中最多读取len个字节的数据,存放到偏移量为off的b数组中。
(4) public int available( ):返回输入流中可以读取的字节数。注意:若输入阻塞,当前线程将被挂起,如果InputStream对象调用这个方法的话,它只会返回0,这个方法必须由继承InputStream类的子类对象调用才有用,
(5) public long skip(long n):忽略输入流中的n个字节,返回值是实际忽略的字节数, 跳过一些字节来读取
(6) public int close( ) :我们在使用完后,必须对我们打开的流进行关闭.
OutputStream提供了3个write方法来做数据的输出,这个是和InputStream是相对应的。
1. public void write(byte b[ ]):将参数b中的字节写到输出流。
2. public void write(byte b[ ], int off, int len) :将参数b的从偏移量off开始的len个字节写到输出流。
3. public abstract void write(int b) :先将int转换为byte类型,把低字节写入到输出流中。
4. public void flush( ) : 将数据缓冲区中数据全部输出,并清空缓冲区。
5. public void close( ) : 关闭输出流并释放与流相关的系统资源。
字符流Writer/Reader
[b] Java中字符是采用Unicode标准,一个字符是16位,即一个字符使用两个字节来表示。为此,JAVA中引入了处理字符的流。[/b]
主要方法:
(1) public int read() throws IOException; //读取一个字符,返回值为读取的字符
(2) public int read(char cbuf[]) throws IOException; /*读取一系列字符到数组cbuf[]中,返回值为实际读取的字符的数量*/
(3) public abstract int read(char cbuf[],int off,int len) throws IOException; /*读取len个字符,从数组cbuf[]的下标off处开始存放,返回值为实际读取的字符数量,该方法必须由子类实现*/
主要方法:
(1) public void write(int c) throws IOException; //将整型值c的低16位写入输出流
(2) public void write(char cbuf[]) throws IOException; //将字符数组cbuf[]写入输出流
(3)
public abstract void write(char cbuf[],int off,int len) throws IOException; //将字符数组cbuf[]中的从索引为off的位置处开始的len个字符写入输出流
(4)
public void write(String str) throws IOException; //将字符串str中的字符写入输出流
(5)
public void write(String str,int off,int len) throws IOException; //将字符串str 中从索引off开始处的len个字符写入输出流
(6)
flush( ) //刷空输出流,并输出所有被缓存的字节。
(7)close() 关闭流 public abstract void close() throws IOException
使用缓冲流的好处是,能够更高效的读写信息,原理是将数据先缓冲起来,然后一起写入或者读取出来。经常使用的是readLine()方法,表示一次读取一行数据。所以极力推荐大家使用,效率高。下面为实战演示:
[java] view
plain copy
package org.example.io;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class TestBufferedWriter {
public static void main(String[] args) throws Exception {
write();
read();
}
/**
* DOC 读取信息.
*
* @throws FileNotFoundException
* @throws IOException
*/
private static void read() throws FileNotFoundException, IOException {
File file = new File("E:\\a.txt");// 指定要读取的文件
// 获得该文件的缓冲输入流
BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
String line = "";// 用来保存每次读取一行的内容
while ((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}
bufferedReader.close();// 关闭输入流
}
/**
* DOC 写入信息.
*
* @throws IOException
*/
private static void write() throws IOException {
File file = new File("E:\\a.txt");// 指定要写入的文件
if (!file.exists()) {// 如果文件不存在则创建
file.createNewFile();
}
// 获取该文件的缓冲输出流
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file));
// 写入信息
bufferedWriter.write("你好世界");
bufferedWriter.newLine();// 表示换行
bufferedWriter.write("hello world");
bufferedWriter.flush();// 清空缓冲区
bufferedWriter.close();// 关闭输出流
}
}
使用方式和FileInputStrem和FileOutputStream基本一致:
[java] view
plain copy
package org.example.io;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class TestBufferedString {
public static void main(String[] args) throws Exception {
// 指定要读取文件的缓冲输入字节流
BufferedInputStream in = new BufferedInputStream(new FileInputStream("F:\\test.jpg"));
File file = new File("E:\\test.jpg");
if (file != null) {
file.createNewFile();
}
// 指定要写入文件的缓冲输出字节流
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file));
byte[] bb = new byte[1024];// 用来存储每次读取到的字节数组
int n;// 每次读取到的字节数组的长度
while ((n = in.read(bb)) != -1) {
out.write(bb, 0, n);// 写入到输出流
}
out.close();// 关闭流
in.close();
}
}
Java中的流分为两种:
1) 字节流:数据流中最小的数据单元是字节
2) 字符流:数据流中最小的数据单元是字符,
Java中的字符是Unicode编码,一个字符占用两个字节。
在java.io包中有四个基本类:InputStream、OutputStream及Reader、Writer类,它们分别处理字节流和字符流:java中其他多种多样变化的流均是由
它派生得来:
:
1. InputStream抽象类
InputStream 为字节输入流,它本身为一个抽象类,必须依靠其子类实现各种功能,此抽象类是表示字节输入流的所有类的超类。 继承自InputStream 的流都是向程序中输入数据的,且数据单位为字节(8bit);
InputStream是输入字节数据用的类,所以InputStream类提供了3种重载的read方法.Inputstream类中的常用方法:
(1) public abstract int read( ):读取一个byte的数据,返回值是高位补0的int类型值。若返回值=-1说明没有读取到任何字节读取工作结束。
(2) public int read(byte b[ ]):读取b.length个字节的数据放到b数组中。返回值是读取的字节数。该方法实际上是调用下一个方法实现的
(3) public int read(byte b[ ], int off, int len):从输入流中最多读取len个字节的数据,存放到偏移量为off的b数组中。
(4) public int available( ):返回输入流中可以读取的字节数。注意:若输入阻塞,当前线程将被挂起,如果InputStream对象调用这个方法的话,它只会返回0,这个方法必须由继承InputStream类的子类对象调用才有用,
(5) public long skip(long n):忽略输入流中的n个字节,返回值是实际忽略的字节数, 跳过一些字节来读取
(6) public int close( ) :我们在使用完后,必须对我们打开的流进行关闭.
2.OutputStream抽象类
OutputStream提供了3个write方法来做数据的输出,这个是和InputStream是相对应的。 1. public void write(byte b[ ]):将参数b中的字节写到输出流。
2. public void write(byte b[ ], int off, int len) :将参数b的从偏移量off开始的len个字节写到输出流。
3. public abstract void write(int b) :先将int转换为byte类型,把低字节写入到输出流中。
4. public void flush( ) : 将数据缓冲区中数据全部输出,并清空缓冲区。
5. public void close( ) : 关闭输出流并释放与流相关的系统资源。
字符流Writer/Reader
[b] Java中字符是采用Unicode标准,一个字符是16位,即一个字符使用两个字节来表示。为此,JAVA中引入了处理字符的流。[/b]
1. Reader抽象类
主要方法:(1) public int read() throws IOException; //读取一个字符,返回值为读取的字符
(2) public int read(char cbuf[]) throws IOException; /*读取一系列字符到数组cbuf[]中,返回值为实际读取的字符的数量*/
(3) public abstract int read(char cbuf[],int off,int len) throws IOException; /*读取len个字符,从数组cbuf[]的下标off处开始存放,返回值为实际读取的字符数量,该方法必须由子类实现*/
2. Writer抽象类
主要方法:(1) public void write(int c) throws IOException; //将整型值c的低16位写入输出流
(2) public void write(char cbuf[]) throws IOException; //将字符数组cbuf[]写入输出流
(3)
public abstract void write(char cbuf[],int off,int len) throws IOException; //将字符数组cbuf[]中的从索引为off的位置处开始的len个字符写入输出流
(4)
public void write(String str) throws IOException; //将字符串str中的字符写入输出流
(5)
public void write(String str,int off,int len) throws IOException; //将字符串str 中从索引off开始处的len个字符写入输出流
(6)
flush( ) //刷空输出流,并输出所有被缓存的字节。
(7)close() 关闭流 public abstract void close() throws IOException
使用缓冲流的好处是,能够更高效的读写信息,原理是将数据先缓冲起来,然后一起写入或者读取出来。经常使用的是readLine()方法,表示一次读取一行数据。所以极力推荐大家使用,效率高。下面为实战演示:
一、通过BufferedReader和BufferedWriter来读写文件
[java] viewplain copy
package org.example.io;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class TestBufferedWriter {
public static void main(String[] args) throws Exception {
write();
read();
}
/**
* DOC 读取信息.
*
* @throws FileNotFoundException
* @throws IOException
*/
private static void read() throws FileNotFoundException, IOException {
File file = new File("E:\\a.txt");// 指定要读取的文件
// 获得该文件的缓冲输入流
BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
String line = "";// 用来保存每次读取一行的内容
while ((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}
bufferedReader.close();// 关闭输入流
}
/**
* DOC 写入信息.
*
* @throws IOException
*/
private static void write() throws IOException {
File file = new File("E:\\a.txt");// 指定要写入的文件
if (!file.exists()) {// 如果文件不存在则创建
file.createNewFile();
}
// 获取该文件的缓冲输出流
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file));
// 写入信息
bufferedWriter.write("你好世界");
bufferedWriter.newLine();// 表示换行
bufferedWriter.write("hello world");
bufferedWriter.flush();// 清空缓冲区
bufferedWriter.close();// 关闭输出流
}
}
二、使用BufferedInputStream和BufferedOuputStream读写图片
使用方式和FileInputStrem和FileOutputStream基本一致:[java] view
plain copy
package org.example.io;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class TestBufferedString {
public static void main(String[] args) throws Exception {
// 指定要读取文件的缓冲输入字节流
BufferedInputStream in = new BufferedInputStream(new FileInputStream("F:\\test.jpg"));
File file = new File("E:\\test.jpg");
if (file != null) {
file.createNewFile();
}
// 指定要写入文件的缓冲输出字节流
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file));
byte[] bb = new byte[1024];// 用来存储每次读取到的字节数组
int n;// 每次读取到的字节数组的长度
while ((n = in.read(bb)) != -1) {
out.write(bb, 0, n);// 写入到输出流
}
out.close();// 关闭流
in.close();
}
}
相关文章推荐
- 怎么在eclipse里构建一个Maven工程
- 30分钟入门Java8之lambda表达式
- MyEclipse把数据库中的表生成java实体类--利用Hibernate
- Spring的Controller映射规则
- 《深入理解java虚拟机》读书笔记-第10章早期(编译器)优化
- Spring定时任务的几种实现
- Java中处理时区的转换
- AS中引入library 以及RxJava
- Spring-Task 定时任务调度
- springmvc 获取request response
- SpringBoot之Scheduling Tasks
- exception is java.lang.LinkageError: loader constraint violation: loader
- MyEclipse自带maven找不到或自己外置安装
- 关于在Eclipse上使用opencv案例的相关问题总结
- 如何在eclipse下查看jar包源码
- FreeMarker、JAVA技术
- Eclipse和MyEclipse快捷键指南
- 使用java、javac命令行编译工程以及遇到的问题
- Java的动态代理的理解
- Spring Jar包