IO 复习
创始人
2024-05-28 13:45:15
0

IO

把电脑硬盘中的数据读到程序中,称为输入,进行数据的read操作

把程序往外部设备写数据,称为输出,进行数据的write操作

File类

一个File对象可以表示计算机硬盘上的一个文件或目录(文件夹)

可以获取文件信息,创建文件,删除文件

但是不能对文件中的数据进行读写操作

一些File类的方法

public class FileDemo {public static void main(String[] args) {
​
​/*File类中构造方法*//*String p = "E:/";File f1 = new File(p,"demo.txt");File f2 = new File(p,"demo.txt");*/
​/*File pf = new File("E:");File f2 = new File(pf,"demo.txt");*/
​File f = new File("E:/demo.txt");System.out.println(f.canExecute());// 文件是否可以被执行System.out.println(f.canRead());// 是否可以读取System.out.println(f.canWrite());// 是否可以写
​System.out.println("---------------------------");System.out.println(f.exists());// 文件是否存在System.out.println(f.getAbsolutePath());//获得文件绝对地址
​System.out.println(f.getName());// 获取文件名字System.out.println(f.getParent());// 获取父级地址
​System.out.println(f.isAbsolute());//是否为绝对路径System.out.println("---------------------------");System.out.println(f.isDirectory());// 是否是文件夹System.out.println(f.isFile());// 是否是文件System.out.println(f.isHidden());// 是否隐藏文件
​System.out.println(new Date(f.lastModified()));//文件最后一次修改的时间System.out.println(f.length());//文件内容长度 以字节为单位
​}
}

流的分类

按照流(java提供的读写文件的类)的读写单位划分为

字节流:每次读写是以字节为单位(计算机中的所有数据存储都是字节为单位) 可以读取任意文件(视频,音频...)

字符流:每次都写是以字符为单位 只能读取纯文本文件(txt,java,html)

字节流

输入字节流:InputStream

FileInputStream就是节点流 直接负责对接文件,对文件进行读写操作

输出字节流:OutputStream

FileOutputStream

public class StreamDemo1 {
​public static void main(String[] args) throws IOException {
​//创建一个输入字节流对象 并为其指定要读的文件FileInputStream in = new FileInputStream("E:/demo.txt");//输入的源文件不存在,会报错FileOutputStream out = new FileOutputStream("D:/demo.txt");//输出时文件不存在是可以自动创建//in.read() 每次读到一个字节数据  并返回, 直到读完后返回-1.int b = 0;while ((b = in.read()) != -1) {out.write(b);}in.close();out.close();//关闭流通道 释放文件}
}
public class StreamDemo2 {
​public static void main(String[] args) throws IOException {
​FileInputStream in= new FileInputStream("E:/feige.exe");FileOutputStream out = new FileOutputStream("E:/demo.exe");//read()  write(int b) 每次只能读入 写出一个字节 效率低, 读写次数多//in.read(b); 每次读一个byte数组个字节内容,返回实际向数组装入的字节数量 读完也是返回-1byte[] b = new byte[1024];int size=0;// 每次最多读取b.length个字节数据为字节数组while((size=in.read(b))!=-1){System.out.println(size);//每次向外写出一个byte数组个字节,从第0个开始,写size个,效率高out.write(b,0,size);}in.close();out.close();}
}

根据封装类型不同流分为:

节点流: 封装的是某种特定的数据源,如文件、字符串、字符串数组等

FileInputStream就是节点流 直接负责对接文件,对文件进行读写操作

处理流:封装的是其他流对象,处理流提供了缓冲功能,提高读写效率,同时增加了一些新的方法

public class StreamDemo4 {
​public static void main(String[] args) throws IOException {
​//节点流  直接负责数据读和写FileInputStream in = new FileInputStream("E:/feige.exe");FileOutputStream out = new FileOutputStream("E:/demo.exe");
​//处理流/包装流/缓存流(带缓冲区)BufferedInputStream bin = new BufferedInputStream(in);BufferedOutputStream bout = new BufferedOutputStream(out);
​byte [] b  = new byte[1024];int size = 0;while((size=bin.read())!=-1){bout.write(b,0,size);}
​bin.close();bout.flush();//刷新缓存区bout.close();
​
​}
}

字符流

字符流只能读纯文本文件

输入字符流

Reader

InputStreamReader

输入转换流,可以把原始字节结合 编码转为字符

FileReader 可以读入一个字符

输出字符流

Writer

OutputStreamWriter

将字符转为字节

FileWriter 可以写出一个字符

FileReader reader = new FileReader("E:/demo.txt");
FileWriter writer = new FileWriter("E:/demo1.txt");
​
char[] cs  = new char[10];
int size = 0;
// reader.read();// 一次读到一个字符编码
while((size=reader.read(cs))!=-1){System.out.println(size);writer.write(cs,0,size);
}
reader.close();
writer.close();

 //节点流 直接包含数据
FileReader reader = new FileReader("E:/demo.txt");
FileWriter writer = new FileWriter("E:/demo2.txt",true);//后面输出不会覆盖前面的,会保留前面的内容
​
//字符处理流BufferedReader breader = new BufferedReader(reader);BufferedWriter bwriter = new BufferedWriter(writer);//readLine() 一次读一行数据  一次缓冲一行数据String line  = null;while((line=breader.readLine())!=null){bwriter.write(line);//一次写出一行数据bwriter.newLine();//插入一个换行符}
​breader.close();bwriter.flush();//缓冲流关闭前需要刷新bwriter.close();

字符处理流这里我们可以一行一行的读和写,这样加大了读写操作的效率

对象输入流和对象输出流

对象---指的是java程序运行时产生的对象

将程序运行时,创建的对象,输出到一个文件中

为什么要将运行时的对象输出到文中?

为了实现数据 持久保存 有时,服务器要关闭(程序结束运行),结束运行时,将一些保存下来

对象输出-->对象序列化

对象输入流 将文件中存储的对象信息 在输入到程序中-->对象反序列化(java中创建对象的方式之一)

java中创建对象 不仅仅只有new一种 对象反序列化 反射

创建一个User类 并且实现Serializable接口,如果不实现的话,那么此类对象信息将无法被输出到文件中

//需要被序列化的类,必须实现Serializable接口,会为类生成一个序列化id号,是唯一与类对应
public class User implements Serializable {
​private Integer id;private String name;
​public User(Integer id, String name) {this.id = id;this.name = name;}
​public Integer getId() {return id;}
​public void setId(Integer id) {this.id = id;}
​public String getName() {return name;}
​public void setName(String name) {this.name = name;}
}

public class ObjectStream {
​public static void main(String[] args) throws IOException, ClassNotFoundException {
​/* Date date = new Date();String s = new String("abc");User user = new User(1, "jim");
​// 讲对象信息输出到文件中  称为对象序列化  对象的类必须实现java.io.Serializable接口FileOutputStream out = new FileOutputStream("E:/obj.txt");ObjectOutputStream objectOut = new ObjectOutputStream(out);objectOut.writeObject(date);objectOut.writeObject(s);objectOut.writeObject(user);objectOut.flush();objectOut.close();*/
​// 对象反序列化  将文件中的信息输入到程序  再创建一个对象FileInputStream in = new FileInputStream("E:/obj.txt");ObjectInputStream objIn = new ObjectInputStream(in);Date date = (Date) objIn.readObject();String s = (String) objIn.readObject();User user = (User) objIn.readObject();
​System.out.println(date);System.out.println(s);System.out.println(user.getId()+":"+user.getName());}
}

相关内容

热门资讯

个人养老查询,养老保险个人怎么...   我们在查询养老保险的时候,最重要的是查询养老保险的余额和养老保险的缴费记录。那么养老保险的具体情...
全额事业编一个月多少钱,福建省...   事业编是大部分人都想要追求的,不仅是稳定更是为了自己的未来考虑。每年都有很多人考事业编,但是不是...
哈哈贷征信不好好下款吗?哈哈贷...   网络贷款似乎已经逐渐成为人们贷款的首选方式,其申请方式简单,放款速度快的特点收到了许多人的青睐。...
UU跑腿一个月大概赚多少钱?U...   跑腿业务在现代生活中越来越吃香,跑腿业务员也随之增加。作为UU跑腿的业务员一个月能赚多少钱呢?接...
急需些西方玄幻小说素材 急需些西方玄幻小说素材看一看异界药shi(师)吧任务,sorry ~
马上金融贷款可靠吗?马上金融怎...   马上金融可能很多人不是非常的了解,其实马上金融旗下的商品还是非常多的,当大家需要资金的话,是可以...
微博借钱逾期到底多严重?微博催...   相信很多人都在微博上借过款,也非常受大家喜欢。微博借款的利息相对于其他平台是比较高的,借款的额度...
适合事业编的25个副业 详细介... 很多朋友在事业编制单位工作,想要搞一些副业为自己增加一些收入。但是事业编是有限制的,有一些副业是不能...
美团送外卖多少钱一单,外卖员一... 暑假期间,由于许多实习公司并不收暑假工,部分大学生们又真的想在假期有一份收入。于是很多人去跑外卖,那...
怎么加入顺丰同城急送兼职 需要... 如今找合适的工作不太容易,但是在闲暇时间找份兼职还是比较简单的。最近很多朋友对顺丰同城急送兼职比较感...