I/O流,就是input,output流
流的分类:
按操作数据单位不同分为:字节流(8bit),字符流(16bit)
按数据流的流向的不同分为:输入流,输出流
按流的角色的不同分为:字节流,处理流
比较常用的一些流如下:
抽象基类 | 节点流 | 缓冲流 |
---|---|---|
InputStream | FileInputStream | BufferedInputStream |
outputStream | FileOutputStream | BufferedOutputStream |
Reader | FileReader | BufferedFileReader |
Writer | FileWriter | BufferedFileWriter |
下面给出一个缓冲字节流例子实现文件的读写(其他流的操作与此大致相同):
public class BufferedTest {
@Test
public void BufferedStream(){
BufferedInputStream bufferedInputStream = null;
BufferedOutputStream bufferedOutputStream = null;
try {
File srcFile = new File("300648.jpg");
File destFile = new File("3006482.jpg");
FileInputStream fileInputStream = new FileInputStream(srcFile);
FileOutputStream fileOutputStream = new FileOutputStream(destFile);
bufferedInputStream = new BufferedInputStream(fileInputStream);
bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
byte[] carBuffer = new byte[5];
int length;
while((length = bufferedInputStream.read(carBuffer)) !=-1 ){
bufferedOutputStream.write(carBuffer,0,length);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//资源关闭
//先关闭外层的流,在关闭外层的流的时候,内层流也会自动关闭
if (bufferedOutputStream != null){
try {
bufferedOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (bufferedInputStream != null){
try {
bufferedInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
缓冲流的作用是加快文件的读写速度,至于为什么能加快其实就是缓冲流不是直接与硬盘交互,而是先把文件存储到一块内存区域。
详情可参考:https://blog.csdn.net/weixin_40321672/article/details/85262332
转换流
InputStreamReader, OutputStreamWriter转换流,都属于字符流
给出一个解码与编码的例子:
public class InputStreamReaderTest {
@Test
public void test2(){
InputStreamReader inputStreamReader = null;
OutputStreamWriter outputStreamWriter = null;
try {
File file1 = new File("test.txt");
File file2 = new File("test3.txt");
FileInputStream fileInputStream = new FileInputStream(file1);
FileOutputStream fileOutputStream = new FileOutputStream(file2);
//参数2指明了字符集,具体使用具体哪个字符集,取决于文件保存时的字符集
inputStreamReader = new InputStreamReader(fileInputStream,"UTF-8");
outputStreamWriter = new OutputStreamWriter(fileOutputStream,"GBK");
char[] cbuf = new char[20];
int length;
while ((length = inputStreamReader.read(cbuf)) != -1){
outputStreamWriter.write(cbuf,0,length);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (inputStreamReader != null){
try {
inputStreamReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (outputStreamWriter != null){
try {
outputStreamWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
对象流与对象的序列化
对象流:
/**
* 对象流的使用
* 1.ObjectInputStream 和 ObjectOutputStream
*/
public class ObjectInputOutputStreamTest {
/*
序列化过程:将内存中的java对象保存在磁盘中或通过网络传输出去
使用ObjectOutputStream实现
*/
@Test
public void testObjectOutputStream(){
ObjectOutputStream objectOutputStream = null;
try {
objectOutputStream = new ObjectOutputStream(new FileOutputStream("person.dat"));
objectOutputStream.writeObject(new Person2("小明","1234",4));
objectOutputStream.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (objectOutputStream != null){
try {
objectOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@Test
public void testObjectInputStream(){
ObjectInputStream objectInputStream = null;
try {
objectInputStream = new ObjectInputStream(new FileInputStream("person.dat"));
Person2 person2 = (Person2) objectInputStream.readObject();
System.out.println(person2);
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
} finally {
if (objectInputStream != null){
try {
objectInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
序列化的步骤:
1.需要实现接口:Serializable
2.当前类提供一个全局常量:serialVersionUID
处理当前实体类需要实现Serizable接口以外,还必须保证其内部所有属性也是可序列化的