系统学习JAVA第十七天
第一阶段在2021.2.1结束了!
一、数据传输
IO输入和输出,硬盘之间的数据交换
1、文件读写流程
①创建文件的容器
②判断方向 合适的类 创建和文件之间的通道
③ 调用对应的方法
④ 查看
⑤关闭
2、字节流
优点:相比于字符流来说传输快
缺点:因为大多数文件都是字符所以解析麻烦
应用场景:图片、视频、音频、压缩包,使用字节流的文档英文
InputStream、FileInputStream
read方法是读取当前文件,一般是用byte数组存储数据,它有返回值,返回的是当前文件的字节数,当返回值为-1时文档就到
了末尾
OuputStream、FileOutputStream
write方法是将字节写入当前文件,默认是覆盖原来文件中的内容,将append设置为true就不会覆盖原来的内容了
//字节输入流
InputStream is = new FileInputStream(file);
//字节输出流
OutputStream os = new FileOutputStream(file);
3、字符流
优点:解析方便
缺点:相比于字节流来说传输慢
应用场景:中文
Reader、FileReader
read方法是读取当前文件,一般是用char数组存储数据,它有返回值,返回的是当前文件的字符数,当返回值为-1时文档就到
了末尾
Writer、FileWriter
write方法是将字符写入当前文件,默认是覆盖原来文件中的内容,将append设置为true就不会覆盖原来的内容了
//字符输入流
Reader r = new FileReader(file);
//字符输出流
Writer w = new FileWriter(file);
4、缓冲的字节流
缓冲的字节流速度比普通的字节流快,但是没有其他的特殊方法
BufferedInputStream
read方法是读取当前文件,一般是用byte数组存储数据,它有返回值,返回的是当前文件的字节数,当返回值为-1时文档就到
了末尾
BufferedOutputStream
write方法是将字节写入当前文件,默认是覆盖原来文件中的内容,将append设置为true就不会覆盖原来的内容了
//缓冲字节输入流
// InputStream iss = new FileInputStream(file);
// BufferedInputStream bis = new BufferedInputStream(is);
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
//缓冲字节输出流
// OutputStream oss = new FileOutputStream(file);
// BufferedOutputStream bos = new BufferedOutputStream(oss);
BufferedOutputStream bos =new BufferedOutputStream(new FileOutputStream(file));
5、缓冲的字符流
读取文档按行读取,写出文档按行书写,可以使用缓冲字符流
BufferedReader
read方法是读取当前文件,一般是用char数组存储数据,它有返回值,返回的是当前文件的字符数,当返回值为-1时文档就到
了末尾
readLine方法可以实现文件的按行读的效果,它有返回值,返回的是当前文件的字符串, 当返回值为null时文档就到了末尾
BufferedWriter
write方法是将字节写入当前文件,默认是覆盖原来文件中的内容,将append设置为true就不会覆盖原来的内容了
newLine方法可以实现文件的按行写的效果, 搭配write实习按行写
//缓冲字符输入流
// Reader rr = new FileReader(file);
// BufferedReader br = new BufferedReader(rr);
BufferedReader br = new BufferedReader(new FileReader(file));
//缓冲字符输出流
// Writer ww = new FileWriter(file);
// BufferedWriter wr = new BufferedWriter(ww);
BufferedWriter bw = new BufferedWriter(new FileWriter(file));
6、将字节流转换为缓冲的字符流
如果现在我们有字节流,实现按行读写?将字节流转字符流
输入流:InputStreamReader
输出流:OutputStreamWriter
//因为只有缓冲字符流能一行一行的读取和写出数据
//所以如果想将字节文件也一行一行的读或者写,就需要将缓冲字节流转换成缓冲字符流
//字节流————>字符流————>缓冲字符流
//读取
// InputStream is = new FileInputStream(file);
// InputStreamReader isr = new InputStreamReader(is);
// BufferedReader bf = new BufferedReader(isr);
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
//写出
// OutputStream os = new FileOutputStream(file);
// OutputStreamWriter osw = new OutputStreamWriter(os);
// BufferedWriter bw = new BufferedWriter(osw);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)));
7、面向对象——>字节流转成对象
存储对象,对象需要具有转换成字节流的能力,所以要实现序列化的接口(implements Serializable)
序列化:对象转成字节流的功能
反序列化:读取对象就是把字节流转成对象
输入流:ObjectInputStream
输出流:ObjectOutputStream
//对象流,需要通过字节流来实现对象的读取和写入
//字节流————>对象字节流
//输入
// InputStream is = new FileInputStream(file);
// ObjectInputStream ois = new ObjectInputStream(is);
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
//输出
// OutputStream os = new FileOutputStream(file);
// ObjectOutputStream oos = new ObjectOutputStream(os);
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
二、案例
我将所有要使用到的输入输出流都封装在了IO这个类中,一个是父类Animal实现了Serializable接口,一个是子类Cat继承了Animal父类,最后还有一个是测试类Test(Test当中的大多数代码都被我注释了,要接口注释只需要删掉前面的//即可,或者选中要解开注释的代码按下Ctrl+?)
1、IO类
package com.tangxin16.zuoye;
import java.io.*;
public class IO {
//字节输入流方法 输入一个字节文件就可以读取它里面的全部内容
public void Input(File file){
//如果没有就新建一个文件
if(file.exists()==false){
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
InputStream is = null;
try {
is = new FileInputStream(file);
//定义byte数组最大容量为1024
byte[] bytes = new byte[1024];
//每次最多只读1024个字节
int a = is.read(bytes);
//如果a的值不等于-1说明文件还没有读到最后一个字节,所以就继续读
if(a != -1){
a=is.read(bytes);
}
System.out.println(new String(bytes));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//字节输出流 输入一个文件,要输入的字符串,和是否要拼接在原来字符串后面的参数true就是要拼接,false就是不需要
//这样就可以将你要输入的字符串输入到文本当中
public void Output(File file,String s,Boolean add){
OutputStream os = null;
try {
if(add == true)
{
//如果add传进来的是true那么就在字符后面继续添加你输入的字符
os = new FileOutputStream(file,true);
}else {
//否则就替换掉原来的字符
os = new FileOutputStream(file);
}
os.write(s.getBytes());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//将一个文件中的字符复制到另一个文件当中 输入2个文本文件和一个是否需要拼接的boolean类型
public void CopyFile(File ifile,File ofile,boolean add){
if(ifile.exists()==false){
try {
ifile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
InputStream is = null;
OutputStream os = null;
try {
is = new FileInputStream(ifile);
if(add == true){
os = new FileOutputStream(ofile,true);
}else {
os = new FileOutputStream(ofile);
}
//先读再取每次最多读和存1024个字节
byte[] bytes = new byte[1024];
int a = is.read(bytes);
while (a!=-1){
os.write(bytes,0,a);
a = is.read(bytes);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
is.close();
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//使用字符流读取文件
public void Reader(File file){
Reader r = null;
try {
r = new FileReader(file);
char[] chars = new char[512];
int a = r.read(chars);
while (a != -1){
a = r.read(chars);
}
System.out.println(chars);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
r.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//使用字符流写入文件
public void Writer(File file,String s,boolean add){
Writer w = null;
try {
if(add == true){
w = new FileWriter(file,true);
}else {
w = new FileWriter(file);
}
w.write(s);
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
w.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//将对象输出到文本文件 输入一个文本文件,一个动物对象,一个是否拼接的boolean类型
public void ObjectOutFile(File file,Animal[] animal,boolean add){
ObjectOutputStream oos = null;
try {
if(add==true){
//因为对象输出流不能直接输出需要借助字节输出流才行
oos = new ObjectOutputStream(new FileOutputStream(file,true));
}else {
oos = new ObjectOutputStream(new FileOutputStream(file));
}
oos.writeObject(animal);
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//对象输入流,用来读取文本中的对象 需要输入一个文本和一个对象
public Object ObjectInFile(File file,Animal[] animal){
ObjectInputStream ois = null;
try {
ois = new ObjectInputStream(new FileInputStream(file));
animal = (Animal[]) ois.readObject();
for (Animal a:animal
) {
System.out.println(a);
}
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}finally {
try {
ois.close();
return animal;
} catch (IOException e) {
e.printStackTrace();
return animal;
}
}
}
//使用缓冲字节流读取文件中的字节
public void BffInput(File file){
if(file.exists()==false){
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
BufferedInputStream bis = null;
try {
bis = new BufferedInputStream(new FileInputStream(file));
byte[] bytes = new byte[1024];
int a = bis.read(bytes);
while (a!=-1){
a = bis.read(bytes);
}
System.out.println(new String(bytes));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//使用缓冲字节流将字节写入文件中
public void BffOutput(File file,String s,Boolean add){
BufferedOutputStream bos = null;
try {
if(add == true){
bos = new BufferedOutputStream(new FileOutputStream(file,true));
}else {
bos = new BufferedOutputStream(new FileOutputStream(file));
}
bos.write(s.getBytes());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//使用缓冲字符流读取文件中的字符
public void BffReader(File file){
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(file));
String s = br.readLine();
while (s != null){
System.out.println(s);
s = br.readLine();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//使用缓冲字符流写入文件
public void BffWriter(File file,String s,boolean add){
BufferedWriter br = null;
try {
if(add == true){
br = new BufferedWriter(new FileWriter(file,true));
}else {
br = new BufferedWriter(new FileWriter(file));
}
//一次写入10行
for (int i = 0; i < 10; i++) {
br.write(s+i);
br.newLine();
}
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//用缓冲字符流一行一行的将一个文件中的字符复制到另一个文件当中
public void BffCopyFileString(File ifile,File ofile,boolean add){
BufferedReader br = null;
BufferedWriter bw = null;
try {
br = new BufferedReader(new FileReader(ifile));
if(add == true){
bw = new BufferedWriter(new FileWriter(ofile,true));
}else {
bw = new BufferedWriter(new FileWriter(ofile));
}
String str = br.readLine();
while (str!=null){
bw.write(str);
bw.newLine();
str = br.readLine();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
br.close();
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//用缓冲字节流转换为缓冲字符流后一行一行的将一个文件中的字符复制到另一个文件当中
public void BffCopyFileByte(File ifile,File ofile,boolean add){
if(ifile.exists()==false){
try {
ifile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
BufferedReader br = null;
BufferedWriter bw = null;
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream(ifile)));
if(add == true){
bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(ofile)));
}else {
bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(ofile)));
}
String str = br.readLine();
while (str!=null){
bw.write(str);
bw.newLine();
str = br.readLine();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
br.close();
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
2、Animal类
package com.tangxin16.zuoye;
import java.io.Serializable;
public abstract class Animal implements Serializable {
public void run(){}
}
3、Cat类
package com.tangxin16.zuoye;
public class Cat extends Animal{
private String name;
private int age;
public Cat(int age,String name) {
this.age = age;
this.name = name;
}
public Cat(){
}
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;
}
@Override
public void run(){
System.out.println("猫在跑");
}
@Override
public String toString() {
return "Cat{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
4、Test类
package com.tangxin16.zuoye;
import java.io.File;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
//读的文件
File ifile = new File("C:\\Users\\tangxin\\Desktop\\java\\Iabc.txt");
//写的文件
File ofile = new File("C:\\Users\\tangxin\\Desktop\\java\\Oabc.txt");
//创建IO类的对象
IO io = new IO();
// //读取ifile文件中的字节
// io.Input(ifile);
// //Scanner输入语句
// Scanner scanner = new Scanner(System.in);
// //将输入的语句赋值给s
// String s = scanner.nextLine();
// //将字符串s的值写人ofile文件,传入ture的意思是s添加在上一次字节的后面
// io.Output(ofile,s,true);
// //读取ofile文件中的字节
// io.Input(ofile);
// //将ifile文件中的字节复制到ofile文件中,因为传入的是true所以这次的传入是添加在上一次字节的后面
// io.CopyFile(ifile,ofile,true);
// //读取ofile文件中的字节
// io.Input(ofile);
// //Scanner输入语句
// Scanner scanner = new Scanner(System.in);
// //将输入的语句赋值给s
// String s = scanner.nextLine();
// //使用字符流写出
// io.Writer(ofile,s,true);
// //使用字符流读取
// io.Reader(ofile);
// //新建一个animal类的数组
// Animal[] animal = new Cat[10];
// //为animal下的子类Cat赋值,每循环一次赋一次值
// for (int i = 0; i < animal.length; i++) {
// animal[i] = new Cat(i,"小憨憨"+i);
// }
// //写入animal的对象到ofile文件中,且覆盖原来的内容
// io.ObjectOutFile(ofile,animal,false);
// //从ofile文件中读取animal的对象
// io.ObjectInFile(ofile,animal);
// //Scanner输入语句
// Scanner scanner = new Scanner(System.in);
// //将输入的语句赋值给s
// String s = scanner.nextLine();
// //使用缓冲字节流写入
// io.BffOutput(ofile,s,false);
// //使用缓冲字节流读取
// io.BffInput(ofile);
// //Scanner输入语句
// Scanner scanner = new Scanner(System.in);
// //将输入的语句赋值给s
// String s = scanner.nextLine();
// //使用缓冲字符流写入
// io.BffWriter(ifile,s,false);
// //使用缓冲字符流读取
// io.BffReader(ofile);
// //用缓冲字符流将ifile中的字符数据,复制到ofile文件中,按行进行读取和输入,ture表示拼接
// io.BffCopyFileString(ifile,ofile,true);
// //使用缓冲字符流读取
// io.BffReader(ofile);
// //用缓冲字节流转换为缓冲字符流后将ifile中的字符数据,复制到ofile文件中,按行进行读取和输入,ture表示拼接
// io.BffCopyFileByte(ifile,ofile,true);
// //使用缓冲字符流读取
// io.BffReader(ofile);
}
}