- 字节流是原生的操作,而字符流是经过处理后的操作
- 对于IO操作属于资源处理,所有的资源处理操作(IO操作、数据库操作、网络)最后必须要进行关闭。
- 字符流适合处理中文,字节流适合处理一切数据类型,图片、音乐、文字都可以使用字节流
- 字节流一定是优先考虑的,只有在处理中文时才会考虑字符流。因为所有的字符都需要通过内存缓冲来进行处理
一、字节输出流OutputStream(二进制文件的写入)
实现程序输出内容到文件的处理,程序到磁盘文件
- 类的定义结构:
public abstract class OutputStream implements Closeable, Flushable
1. Closeable: public void close() throws IOException;
2. Flushable: public void flush() throws IOException;
- 其他的方法:
//这俩个类接口的方法
1.输出单个字节:public abstract void write(int b) throws IOException;
2.将给定的字节数组内容全部输出:public void write(byte b[]) throws IOException
3.将部分字节数组内容输出:public void write(byte b[], int off, int len) throws IOException
4.void flush () throws IOException 将输出流中缓存的数据全部写到目的地
由于OutputStream是一个抽象类,所以要想为父类实例化,就必须要使用子类。由于方法名称都由父类声明好了,所以我们在此处只需要关系子类的构造方法。如果要进行文件的操作,可以使用FileOutputStream类来处理,该类继承于OutputStream类。这个类的构造方法如下:
- 接收File类(覆盖):public FileOutputStream(File file) throws FileNotFoundException
- 接收File类(追加):public FileOutputStream(File file, boolean append)
public class FileTest1 {
public static void main(String[] args) throws Exception {
File file=new File(File.separator + "F:" + File.separator + "Java" +
File.separator + "Test1"
+ File.separator + "HH.txt");
//如果父类目录不存在创建多级目录
if(!file.getParentFile().exists()){
file.getParentFile().mkdirs();
}
// OutputStream是一个抽象类,所以需要通过子类进行实例化
// 此时只能操作File类
OutputStream output1=new FileOutputStream(file);
//追加接收File类(追加):
OutputStream output=new FileOutputStream(file,true);
// 要求输出到文件的内容
String message="hello java !!!";
// 1.将全部内容变为字节数组
output.write(message.getBytes());
//2.将部分内容变为字节数组
output.write(message.getBytes(),0,5);
output.close();
}
}
二、字节输入流InputStream(二进制文件的读取)
从二进制磁盘文件中读取字节数据
- 类的定义:
public abstract class InputStream implements Closeable
只实现了closeable方法,该InputStream类中其他的方法
1. 读取部分数据到字节数组中,如果读取满了则返回长度(len),如果没有读取满则返回读取的数据个数,如果读取到最后没有数据了返回-1:public int read(byte b[], int off,int len) throws IOException
2. 读取数据到字节数组中,返回数据的读取个数。如果此时开辟的字节数组大小大于读取的数据大小,则返回的就是读取个数;如果要读取的数据大于数组的内容,那么这个时候返回的就是数组长度;如果没有数据了还在读,则返回-1:public int read(byte b[]) throws IOException
3.读取单个字节,每次读取一个字节的内容,直到没有数据了返回-1:public abstract int read() throwsIOException;
同OutputStream的使用一样,InputStream是一个抽象类,如果要对其实例化,同样也需使用子类。如果要对文件进行处理,则使用FileInputStream
//实现文件信息的读取
public class InputStreamTest {
public static void main(String[] args) throws IOException {
File file = new File(File.separator + "F:" + File.separator + "Java"
+ File.separator + "Test1" + File.separator + "HH.txt");
// 2.必须保证文件存在才能进行处理
if (file.exists()) {
InputStream input = new FileInputStream(file);
byte[] data = new byte[1024]; // 每次可以读取的最大数量
int len = input.read(data); // 此时的数据读取到了数组之中
String result = new String(data, 0, len); // 将字节数组转为String
System.out.println("读取内容【" + result + "】");
input.close();
}
}
}
三、字符输入流Reader
该类也是一个抽象类,通过字符数组进行读取操作,用于写入字符流到磁盘,Writer类中提供有方法直接向目标源写入字符串,而在Reader类中没有方法可以直接读取字符串类型,这个时候只能通过字符数组进行读取操作
public class ReaderTest {
public static void main(String[] args) throws IOException {
File file=new File(File.separator + "F:" + File.separator + "Java" +
File.separator + "Test1"
+ File.separator + "BB.txt");
// 2.必须保证文件存在才能进行处理
if (file.exists()) {
Reader in = new FileReader(file) ;
char[] data = new char[1024] ;
int len = in.read(data) ; // 将数据读取到字符数组中
String result = new String(data, 0, len) ;
System.out.println("读取内容【"+result+"】") ;
in.close();
}
}
}
字符适合处理中文数据
四、字符输出流Writer
- 类的定义:
public abstract class Writer implements Appendable, Closeable, Flushable
- 有直接输出字符串的方法public void write(String str) throws IOException,如果要操作文件使用FileWriter子类
public class WriterTest {
public static void main(String[] args) throws IOException {
File file=new File(File.separator + "F:" + File.separator + "Java" +
File.separator + "Test1"
+ File.separator + "BB.txt");
if(!file.getParentFile().exists()){
file.getParentFile().mkdirs();
}
String a="ok!";
Writer out=new FileWriter(file);
out.write(a);
out.close();
}
五、AutoCloseable自动关闭支持
这种自动关闭处理是需要结合try…catch语句进行调用。
//使用自动关闭处理之前的操作
public class AutoCloseable1 {
public static void main(String[] args) {
File file = new File("F:" + File.separator + "Java" + File.separator + "Test1" + File.separator + "DD.txt");
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
try (OutputStream out = new FileOutputStream(file)) {
String a = "java";
out.write(a.getBytes());
} catch (Exception e) {
e.printStackTrace();
}
}
}
//try...catch
class Message implements java.lang.AutoCloseable {
public Message() {
System.out.println("第一条消息");
}
@Override
public void close() throws Exception {
System.out.println("自动关闭");
}
public void print() {
System.out.println("www.java");
}
}
public class AutoCloseable {
public static void main(String[] args) {
try (Message m = new Message()) {
m.print();
} catch (Exception e) {
e.printStackTrace();
}
}
}
如果字符流不关闭,则需要强制刷新,一般使用close手工关闭out.flush(); // 写上此语句表示强制清空缓冲内容,所有内容都输出
六、转换流
- OutputStreamWriter:将字节输出流变为字符输出流(Writer对于文字的输出要比OutputStream方便)
- InputStreamReader:将字节输入流变为字符输入流(InputStream读取的是字节,不方便中文的处理)
public class OutputStreamWriterDemo {
public static void main(String[] args) throws IOException {
OutputStream u = new FileOutputStream("F:/Java/Test1/HH.txt");
Writer out = new OutputStreamWriter(u, "UTF-8");// 字节流转为字符流
out.write(" UTF-8");
out.close();
}
}