1.IO流的分类
- 根据处理数据类型的不同分为:字符流和字节流
-
根据数据流向不同分为:输入流和输出流
2.节点流和处理流
3.常用例子
3.1基本分为4个步骤:
1.创建File对象
2.创建流对象
3.进行读/写操作
4.关闭流对象
//抽象基类 节点流 缓冲流(处理流的一种)
//InputStream FileInputStream(字节流,图片,视频等) BufferedInputStream
//OutputStream FileOutputStream BufferedOutputStream
//Reader FileReader(字符流,处理txt等文本文件) BufferedReader
//Writer FileWriter BufferedWriter
public class FileTest {
//FileReader
@Test
public void test1() {
FileReader reader = null;
try {
//实例化文件
File file = new File("./helloworld.txt");
//创建读入/写入对象
reader = new FileReader(file);
//读入/写入
//int read = reader.read();
//方式一:
// int read;
// while ((read = reader.read()) != -1){
// System.out.print((char)read);
// }
//方式二:
int len;
char[] chars = new char[5];
while ((len = reader.read(chars)) != -1) {
//len表示读取的字符数,保存在chars数组中
for (int i = 0; i < len; i++) {
System.out.print(chars[i]);
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//关闭
try {
if (reader != null)
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//FileWriter
@Test
public void test2() {
FileWriter fw = null; //如果append为true,则在原文件中追加,否则覆盖文件
try {
File file = new File("./helloword2.txt");
fw = new FileWriter(file, false);
Writer a = fw.append('a');
fw.write("helloworld2!");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fw != null)
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//复制
@Test
public void test3() {
FileReader fr = null;
FileWriter fw = null;
try {
File file = new File("helloworld.txt");
File file1 = new File("copy_helloword.txt");
fr = new FileReader(file);
fw = new FileWriter(file1);
char[] chars = new char[1];
int len;
while ((len = fr.read(chars)) != -1) {
System.out.println(chars[0]);
fw.write(chars, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fr != null) {
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fw != null) {
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//图片复制 FileOutputWriter,FileInputReader
@Test
public void test4() {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
File src = new File("./pic.png");
File dest = new File("./pic2.png");
fis = new FileInputStream(src);
fos = new FileOutputStream(dest);
byte[] bytes = new byte[1024];
int len;
while ((len = fis.read(bytes)) != -1) {
fos.write(bytes, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if(fis != null){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(fos != null){
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//使用缓冲流(内部有个有容器,当读取到一次数量的数据时,一次性写入)
@Test
public void test5(){
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
FileInputStream fis;
FileOutputStream fos;
try {
File src = new File("./pic.png");
File dest = new File("./pic3.png");
//创建节点流
fis = new FileInputStream(src);
fos = new FileOutputStream(dest);
//使用处理流来包裹节点流
bis = new BufferedInputStream(fis);
bos = new BufferedOutputStream(fos);
byte[] bytes = new byte[1024];
int len;
while((len = bis.read(bytes)) != -1){
bos.write(bytes,0,len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//只要关闭外层的流,内层的流会自动关闭
if(bis !=null){
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(bos != null){
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//使用对象流
@Test
public void test6(){
try {
File file = new File("object.txt");
//序列化,将对象写入文件中
//1.类必须实现Serializable接口
//2.需要一个UID,如果没有的话,运行时会自动生成一个,但是当类改变时,反序列化时会出现错误
// private static final long serialVersionUID = -6849794470754667710L;
FileOutputStream fos = new FileOutputStream(file,false);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(new Person("yang",18));
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void test7(){
try {
File file = new File("./object.txt");
FileInputStream fis = new FileInputStream(file);
ObjectInputStream ois = new ObjectInputStream(fis);
Person person = (Person) ois.readObject();
System.out.println(person);
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
class Person implements Serializable{
String name;
int age;
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
4.自我总结
要进行IO的操作,首要要确定是要进行读操作还是写操作,读的话就是Input/Reader,写的话就是Output/Writer,其次确定操作的是字符还是字节,字符流的话就是使用Reader,字节流的话就是使用Stream,同时在使用readf()进行读操作时,使用char[] 或 byte[] 数组来存储每一次读入的数据,然后使用write()将char[] 或 byte[]中的数据写入指定文件中。