一、File类
(一)、引入
File类是java.io包中很重要的一个类
可以表示文件,还可以表示目录;在程序中一个File类对象可以代表一个文件或目录
File对象可以对文件或目录的属性进行操作
不能对文件的具体数据进行操作,即不能直接对文件进行读/写操作
(二)、构造方法
File f = new File("D:/demo.txt");
(三)、常用方法
boolean exists()
boolean idFile()
boolean isDirectory()
String getName()
long length()
boolean createNewFile() throw IOException
boolean delete()
public String[] list()
public File[] listFiles()
import java.io.File;
public class Demo1 {
public static void main(String[] args) {
/*
一个File类的对象可以表示计算机硬盘上的一个文件/目录
文件系统中: 文件夹,文件
File类是没有办法对文件的内容进行读写操作的,
*/
File f1 = new File("D:/demo");
File f2 = new File("D:/demo.txt");
//获取文件信息,判断文件信息
System.out.println(f2.length());//文件内容的长度,以字节为单位
System.out.println(f2.canWrite());//是否可写
System.out.println(f2.getParent());//获取父级
System.out.println(f2.isDirectory());//是否是文件夹
System.out.println(f2.isFile());//是否是文件
System.out.println(f2.exists());//是否存在
}
}
public class Demo3 {
public static void main(String[] args) {
File f2 = new File("D:/demo/a/b");
//f2.mkdir();//只能创建单级文件夹
//f2.mkdirs();
//f2.delete();
// delete(),删除文件夹时,文件夹必须为空,否则文件夹删除不掉.
//listFiles();
// 返回当前目录中的子级, 将子级装入到一个File类型数组中
File[] fs = f2.listFiles();
for(File f : fs){
System.out.println(f);
f.delete();//删除子集文件
}
f2.delete();//删除空文件夹
}
}
二、输入及输出的概念
输入输出(I/O)
把电脑硬盘上的数据读到程序中,称为输入,即input,进行数据的read操作
从程序往外部设备写数据,称为输出,即output,进行数据的write操作
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class Demo1 {
public static void main(String[] args) throws IOException {
//FileInputStream相当于一个输入的管道
//File f = new File("D:/demo.txt");
//FileInputStream inp = new FileInputStream(f);
FileInputStream in = new FileInputStream("D:/demo.txt");
int b = in.read();
//read(); 每read()一次,就从文件中读到一个字节,读完返回-1
System.out.println(b);//转为byte类型输出
int b1 = in.read();
System.out.println(b1);
int b2= in.read();
System.out.println(b2);
int b3= in.read();
System.out.println(b3);
int b4 = in.read();
System.out.println(b4);
}
}
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class Demo3 {
public static void main(String[] args) throws IOException {
FileInputStream in = new FileInputStream("D:/D.txt");//24个字节
FileOutputStream out = new FileOutputStream("D:/A.txt");//会自行创建A.txt
//read(b);一次读一个指定的数组长度个内容,读完还是返回-1
byte[] b = new byte[10];
//容器 前2次,数组中每次都装满了,是10, 第3次实际装了4
int length = 0;
//记录数组中实际装入的元素个数
while((length = in.read(b))!=-1){
System.out.println(length);
//一次写出一个byte数组个内容,但是从第0个开始写,写length个
out.write(b,0,length);
}
in.close();//关闭管道
out.close();//关闭管道
}
}
三、输入流与输出流
流按着数据的传输方向分为:
(一)、输入流:
往程序中读叫输入流。
InputStream和OutputStream的子类都是字节流 可以读写二进制文件,主要处理音频、图片、歌曲、字节流,处理单元 为1个字节
(二)、输出流:
从程序中往外写叫输出流。
Reader和Writer的子类都是字符流 主要处理字符或字符串,字符流处理单元为1个字符。 字节流将读取到的字节数据,去指定的编码表中获取对应文字。
四、字节流与字符流
从数据流编码格式上划分为
(一)、字节流
字节流中常用类
字节输入流 FileInputStream
字节输出流 FileOutputStream
(二)、字符流
字符流中常用类
字符输入流 FileReader
字符输出流 FileWriter
五、输入输出节点字节流
(一)、InputStream的基本方法
读取一个字节并以整数的形式返回(0~255),如果返回-1已到输入流的末尾
int read() throws IOException
读取一系列字节并存储到一个数组buffer,返回实际读取的字节数,如果读取前已到输入流的 末尾返回-1
int read(byte[] buffer) throws IOException
关闭流释放内存资源
void close() throws IOException
(二)、OutputStream的基本方法
向输出流中写入一个字节数据,该字节数据为参数b的低8位
void write(int b) throws IOException
将一个字节类型的数组中的从指定位置(off)开始的 len个字节写入到输出流
void write(byte[] b, int off, int len) throws IOException
关闭流释放内存资源
void close() throws IOException
六、节点流与处理流
(一)节点流
流中如果直接操作的是文件,字符串等,就属于节点流.
FileInputStream字节输入流
FileOutputStream字节输出流
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Demo4 {
public static void main(String[] args) {
FileInputStream in = null;
FileOutputStream out = null;
try {
in = new FileInputStream("D:/DD.txt");//异常
out = new FileOutputStream("D:/DDD.txt");
byte[] b = new byte[20];
int length = 0;
while((length = in.read(b))!=-1){
out.write(b,0,length);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
System.out.println("系统找不到文件");
}catch (IOException e) {
e.printStackTrace();
System.out.println("读写异常");
}finally {
if(in!=null){
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(out!=null){
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
(二)处理流
包装流
BufferedInputStream缓冲字节输入流
BufferedOutputStream缓冲字节输出流
DataInputStream
DataOutputStream
ObjectInputStream
ObjectOutputStream
import java.io.*;
public class Demo5 {
public static void main(String[] args) throws IOException {
/*
FileInputStream,FileOutputStream直接操作文件,属于节点流
内部带缓冲数组的包装处理流
BufferedInputStream,BufferedOutSstream,流对象,包含的是一个节点流对象
*/
FileInputStream in = new FileInputStream("D:/vue.js");
BufferedInputStream bin = new BufferedInputStream(in);
FileOutputStream out =new FileOutputStream("D:/VUE/vue.js");
BufferedOutputStream bout = new BufferedOutputStream(out,2048);
byte [] b = new byte[1024*10];
int length = 0;
while((length=bin.read(b))!=-1){
bout.write(b,0,length);
}
bin.close();
bout.flush();//刷新缓冲区
bout.close();
}
}
七、输入输出节点字符流
字符流,每次读取的单位是一个字符
计算机中所有的数据存储时,都是以字节为单位存储的.
utf-8编码表中, 一个汉字由3个字节组成()
字节—>字符(转换流)
字符流 只能 读取 纯文本 文件
Reader 字符输入流 父类
InputStreamReader 转换流 把字节按照编码表 转为字符
FileReader 文件输入字符流
Writer 字符输出流父类
OutputStreamWriter 转换流 把字符转为字节
FileWriter 文件输出字符流
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Demo1 {
public static void main(String[] args) throws IOException {
FileReader reader = new FileReader("D:/DEMO1.txt");
//int c = reader.read();
//一次读到一个字符的内容,底层使用了转换流,把3个字节转为一个字符
FileWriter writer = new FileWriter("D:/DEMO2.txt");
int c = 0;
while ((c=reader.read())!=-1){
writer.write(c);
}
reader.close();
writer.close();
}
}
import java.io.*;
public class Demo3 {
public static void main(String[] args) throws IOException {
//节点流,直接读文件
FileReader reader = new FileReader("D:/DEMO5.txt");
BufferedReader bufferedReader = new BufferedReader(reader);
FileWriter writer = new FileWriter("D:/DEMO6.txt",true);
// 是否追加写
BufferedWriter bufferedWriter = new BufferedWriter(writer);
String line = null; //一次读一行数据进来
while ((line = bufferedReader.readLine())!=null){
bufferedWriter.write(line);//一次写出一个字符串
bufferedWriter.newLine();//插入换行符
}
bufferedReader.close();
bufferedWriter.close();
}
}
字符流: 节点流和处理流
BufferedReader
BufferedWriter
八、Print流
只做输出没有输入 分为字节打印流和字符打印流
字符打印流 print方法可以打印各种类型数据
PrintWriter 打印字符流 从程序中向外输出数据
在javaweb项目中,服务器端向客户端响应数据以打印流的方式响应
(从服务器端 向 浏览器打印输出内容)
九、对象输入输出流
引入
在程序中创建对象,都在内存(堆)中,程序运行完毕,对象数据就不复存在了,但是,在大多时候,我们需要在程序运行完毕后保留程序运行中的一些数据,Java恰好提供了一种功能,即将内存中的对象,直接输出到一个文件中,来做到数据的持久化保存。
对象输出流: 又称为 对象序列化 ObjectOutputStream
流将指定的对象写入到文件的过程
对象输入流: 又称为 对象反序列化 ObjectInputStream
将指定序列化好的文件读出来的过程
需要序列化的类,必须要实现 Serializable接口,Serializable接口中没有任何
方法
当一个类声明实现Serializable接口后, 表明该类可被序列化。 在类中可以生成一个编号
<–对象已经序列化出去后做了修改,该对象依然可以被正确反序列化.–>
<–如果不显示生成序列号,那么将会隐式生产,但是隐式生成后,类一旦发生
改变,序列号也会随之改变–>
<–当某个属性不需要序列化时,使用transient修饰即可–>
private transient String name;