1.IO流概述
IO流:输入输出流
流是一组有顺序的,有起点和终点的字节集合是对数据传输的总称或抽象。即数据在两设备间的传输称为流。流的本质是数据传输,根据数据传输特性将流抽象为各种类,方便更直观的进行数据操作
IO流的分类
根据处理数据类型的不同分为:字符流 字节流
根据流向不同分为:输入流和输出流
注意:在区分输入与输出流的时候有时会懵掉,其实只要记住以内存为中心,从内存向文件中写数据是输出,从文件读到内存为输入流
2.字节输出流
OutputStream类的定义
public abstract class OutputStream
extends Object
implements Closable,Flushable
此抽象类是表示输出字节流的所有类的超类输出流接受输出字节并将这些字节发送到某个接收器
void close()
关闭此输出流并释放与此流相关的所有系统资源
void flush()
刷新此输出流并强制写出所有缓冲的输出字节
void write(byte []b)
将b.length个字节从指定的byte数组写入到此输出流
void write(byte[]b,int off,int len)
将指定byte数组中从偏移量off开始的len个字节写入此输入流
abstract void write(int b)
将指定的字节写入此输出流
OutputStream类只是一个抽象类,抽象类必须通过子类完成,现在要向文件中输出,使用FileOutputStream类
FileOutputStream(File file)
创建一个向指定File对象表示的文件中写入数据的文件输出流
FileOutputStream(File file,boolean append)
创建一个向指定File对象表示的文件中写入数据的文件输出流
FileOutputStream(String name)
创建一个向具有指定名称的文件中写入数据的输出文件流
FileOutputStream(String name,boolean append)\
创建一个向具有指定name的文件中写入数据的输出文件流
public class OutputStreamDemo {
/**
*字节输出流的方式一:每次输出一个字节
* 字节输出流的方式二:每次输出指定大小的字节
*/
public static void write1()
{
try {
//创建一个文件字节输出流对象
OutputStream outputStream = new FileOutputStream(new File("H:"+File.separator+"第一种输出方式.txt"));
//想文件中输出一个1
String info ="Hello IO";
//将字符转换成字节数组
byte[] bytes=info.getBytes();
for(int i=0;i<bytes.length;i++)
{
//向文件输出
outputStream.write(bytes[i]);
}
//关闭流
outputStream.close();
}catch (FileNotFoundException ex)
{
ex.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void write2()
{
try {
//参数true表示追加输出
OutputStream outputStream=new FileOutputStream(
new File("H:"+File.separator+"第二种输出方式.txt"),true);
String info = "Hello,Mydear";
byte []bytes = info.getBytes();
//向文件中输出
outputStream.write(bytes);
//输出一个字节数组中指定范围内的字节
//outputStream.write(bytes,0,5);
//最后别忘记关闭流
outputStream.close();
}catch (FileNotFoundException ex)
{
ex.printStackTrace();
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
public static void main(String args[])
{
write1();
write2();
}
}
3.字节输入流
InputStream类定义:
public abstract class InputStream
extends Object
implements Closeable
此抽象类是表示字节输入流的所有类的超类
void close()
关闭此输入流并释放与该流关联的所有系统资源
abstract int read()
从输入流中读取数据的下一个字节
int read(byte[] b)
从输入流中读取一定数量的字节,并将其存储在缓冲区数组b中
int read(byte[] b,int off,int len)
将输入流中最多len个数据字节读入到byte数组
FileInputStream从文件系统中的某个文件中获得输入字节。哪些文件可用取决于主机环境。FileInputStream用于读取诸如图像数据之类的原始字节流。要读取字符流,请考虑使用FileReader
FileInputStream(File file)
通过打开一个到实际文件的连接来创建一个FileInputStream,该文件通过文件系统中的File对象file指定
FileInputStream(String name)
通过打开一个到实际文件的连接来创建一个FileInputStream,该文件通过文件系统中的路径名name指定
public class InputStreamDemo {
/**
* read1()字节输入流的读取方式一:每次读取一个字节
* read2()字节输入流的读取方式二:一次性读取所有的字节
* read3()每次读取指定的字节,避免一次性开辟的数组过大,造成内存溢出
*/
public static void read1()
{
try {
//构造一个字节输入流对象
InputStream inputStream = new FileInputStream(
new File("H:"+ File.separator+"第一种输出方式.txt"));
//定义一个字节,-1表示没有数据
int b =-1;
//read方法返回的是读取的字节数,当没有数据的时候返回-1
while ((b=inputStream.read())!=-1)
{
System.out.println((char)b);
}
}catch (FileNotFoundException ex)
{
ex.printStackTrace();
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
public static void read2()
{
try {
File file = new File("H:"+ File.separator+"第一种输出方式.txt");
InputStream inputStream = new FileInputStream(file);
//根据文件大小构造字节数组
byte []bytes = new byte[(int)file.length()];
//暂时将文件的内容储存到这里
int len=inputStream.read(bytes);
//根据字节数组构造一个字符串
System.out.println(new String(bytes));
inputStream.close();
}catch (FileNotFoundException ex)
{
ex.printStackTrace();
}catch (IOException ex)
{
ex.printStackTrace();
}
}
public static void read3() {
try {
InputStream inputStream = new FileInputStream(
new File("H:" + File.separator + "第一种输出方式.txt"));
//指定每次要读取的字节数组
byte[] bytes = new byte[512];
//每次读取的实际长度
int len = -1;
StringBuilder stringBuilder = new StringBuilder();
//read当读完的时候才会返回-1,这样来控制循环
while ((len = inputStream.read(bytes)) != -1)
{
//注意这里要以实际的读取的长度来new字符串对象
stringBuilder.append(new String(bytes, 0, len));
}
System.out.println(stringBuilder);
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
}
public static void main(String args[])
{
System.out.println("=============");
//一个一个字节的读取
read1();
System.out.println("=============");
//一次性读取所有字节
read2();
System.out.println("=============");
//每次读取指定的字节
read3();
}
}