import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class IODemo {
private static void copyFile(String srcPath ,String destPath) throws IOException {
FileInputStream fileInputStream = new FileInputStream(srcPath);
FileOutputStream fileOutputStream = new FileOutputStream(destPath);
byte[] buffer = new byte[1024];
int len = -1;
while ((len = fileInputStream.read(buffer)) != -1) {
fileOutputStream.write(buffer,0,len);
}
fileInputStream.close();
fileOutputStream.close();
}
public static void main(String[] args) throws IOException {
copyFile("e:/1/123.jpg","e:/1/1234.jpg");
}
}
在操作上述方法时,存在一个致命问题,在某些情况下,可能close方法无法被调用,当方法执行时,一旦触发了IOException异常,此时方法就会立刻被终止,从而导致下面的close方法无法被调用。
改进后:
private static void copyFile2(String srcPath ,String destPath) {
FileInputStream fileInputStream = null;
FileOutputStream fileOutputStream = null;
try {
fileInputStream = new FileInputStream(srcPath);
fileOutputStream = new FileOutputStream(destPath);
byte[] buffer = new byte[1024];
int len = -1;
while ((len = fileInputStream.read(buffer)) != -1) {
fileOutputStream.write(buffer,0,len);
}
}catch (IOException e) {
e.printStackTrace();
}finally {
try {
if (fileInputStream!=null) {
fileInputStream.close();
}
if (fileOutputStream!=null) {
fileOutputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
这时,虽然问题已经解决,但由于处理各种异常,显得代码不直观,可以采用下面这种代码:
private static void copyFile3() {
//try语句会在代码执行完毕后自动调用close方法,前提是这个类必须实现Closable接口
try(FileInputStream fileInputStream = new FileInputStream("e:/1/123.jpg");
FileOutputStream fileOutputStream = new FileOutputStream("e:/1/1234.jpg")) {
byte[] buffer = new byte[1024];
int len = -1;
while ((len = fileInputStream.read(buffer)) != -1) {
fileOutputStream.write(buffer,0,len);
}
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
}
利用try语句自动调用close。
BufferedInputStream,BufferdeOutputStream
内置了缓存区
private static void copyFile() throws IOException {
//先得创建FileInputStream和FileOutputStream
FileInputStream fileInputStream = new FileInputStream("e:/1/123.jpg");
FileOutputStream fileOutputStream = new FileOutputStream("e:/1/1234.jpg");
BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
byte[] buffer = new byte[1024];
int len = -1;
while ((len = bufferedInputStream.read(buffer)) != -1) {
bufferedOutputStream.write(buffer,0,len);
}
//此处设计四个流对象
//调用这组close,就会自动把内部包含的FileInputStream,FileOutputStream关闭
//也没有先后关闭顺序
bufferedInputStream.close();
bufferedOutputStream.close();
}
此时上述的代码也会遇到之前无法调用close的问题,解决办法也一样。
private static void copyFile2() {
try(BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream("e:/1/123.jpg");
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream("e:/1/1234.jpg"))) {
int len = -1;
byte[] buffer = new byte[1024];
while ((len = bufferedInputStream.read(buffer))!=-1) {
bufferedOutputStream.write(buffer,0,len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
刚开始读,当文件剩余内容比较多,一次read就会把缓冲区填满,此时len长度就是缓冲区长度,当文件剩余内容不足1024时,就会把剩余内容都读出来,当文件读完了,再次调用read就会返回-1。
每次的read,缓存区的内容就会变一次,write就会把当前缓存区内容写到文件里。
接下来,我们来感受一下,带缓存区和不带缓存区的差距。
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class IODemo3 {
public static void main(String[] args) {
testNoBuffer();
//testBuffer();
}
private static void testBuffer() {
long beg = System.currentTimeMillis();
try(BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream("d:/1024.jpg"))) {
int ch = -1;
while ((ch = bufferedInputStream.read())!=-1) {
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
long end = System.currentTimeMillis();
System.out.println(" buffer:"+(end-beg)+"ms");
}
private static void testNoBuffer() {
long beg = System.currentTimeMillis();
try(FileInputStream fileInputStream = new FileInputStream("d:/1024.jpg")) {
int ch = -1;
while ((ch = fileInputStream.read())!=-1) {
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
long end = System.currentTimeMillis();
System.out.println("no buffer:"+(end-beg)+"ms");
}
}
我们可以发现两者的差别还是很大的。
字符流对象
和字节流非常相似,只是字节流读写操作以byte为单位。缓存区就是一个byte【】,字符流读写操作以char为单位,缓存区就是一个char【】。
import java.io.*;
public class IODemo4 {
public static void main(String[] args) {
copyFile3();
}
private static void copyFile() {
//处理文本文件
try(FileReader fileReader = new FileReader("d:/890.txt");
FileWriter fileWriter = new FileWriter("d:/8901.txt")) {
char[] buffer = new char[1024];
int len = -1;
while ((len = fileReader.read(buffer))!=-1) {
fileWriter.write(buffer,0,len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private static void copyFile2() {
//处理文本文件
try(BufferedReader bufferedReader = new BufferedReader(new FileReader("d:/890.txt"));
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("d:/8901.txt"))) {
char[] buffer = new char[1024];
int len = -1;
while ((len = bufferedReader.read(buffer))!=-1) {
bufferedWriter.write(buffer,0,len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private static void copyFile3() {
//带缓存区的字符流有一种特殊的用法,按行读取
try(BufferedReader bufferedReader = new BufferedReader(new FileReader("d:/890.txt"));
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("d:/8901.txt"))) {
String line = "";
//readLine表示读取一行,读到换行符为止
while ((line = bufferedReader.readLine()) != null) {
System.out.println("line:"+line);
bufferedWriter.write(line);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
readLine读到一行数据,会自动把最末尾的换行符去掉。
序列化和反序列化
序列化:把一个结构化数据变成一个二进制的bit流(可以把这个bit流保存到文件,或者通过网络传输)
反序列化:把二进制bit流还原回原来的对象
反序列化和序列化的最大目的就是为了让对象能够通过网络传输,或者能够在文件中保存。
Java中自带的序列化方式:
ObjectInputStream:负责反序列化
ObjectOutputStream:负责序列化
import java.io.*;
class Student implements Serializable {
public String name;
public int age;
public int score;
}
public class IODemo5 {
public static void main(String[] args) throws IOException {
Student s = new Student();
s.name = "张三";
s.age = 20;
s.score = 100;
serializeStudent(s);
}
private static void serializeStudent(Student s) throws IOException {
ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("d:/student.txt"));
objectOutputStream.writeObject(s);
objectOutputStream.close();
}
}
import java.io.*;
class Student implements Serializable {
public String name;
public int age;
public int score;
}
public class IODemo5 {
public static void main(String[] args) throws IOException, ClassNotFoundException {
/* Student s = new Student();
s.name = "张三";
s.age = 20;
s.score = 100;
serializeStudent(s);*/
Student s = deserializeStudent();
System.out.println(s.age);
System.out.println(s.name);
System.out.println(s.score);
}
private static void serializeStudent(Student s) throws IOException {
ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("d:/student.txt"));
objectOutputStream.writeObject(s);
objectOutputStream.close();
}
private static Student deserializeStudent() throws IOException, ClassNotFoundException {
ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream("d:/student.txt"));
Student s =(Student) objectInputStream.readObject();
return s;
}
}
对于反序列化有可能会遇到某个字段不太一样,编译器会有专门的解决方法,根据类来生成uid的值。如果反序列化程序读取uid和自己类的uid不一样,就不在反序列了。