活动地址:CSDN21天学习挑战赛
目录
在我们没有接触IO流和文件操作之前,我们全部的操作,数据都是保存在内存中的。每当我们重新启动程序数据就都消失了。
如果我们想要将数据永久储存,我们就需要将数据存储到硬盘里面。这样我们就需要对File进行操作,想要对文件的的读写,就知道什么是IO流
java的File类
File类的对象代表操作系统的文件(文件、文件夹),File类在java.io.File包下。
File类提供了诸如:创建文件对象代表文件,获取文件信息(大小、修改时间)、删除文件、创建文件(文件夹)等功能
File的使用
File类创建对象
方法名称 | 说明 |
---|---|
public File(String pathname) | 根据文件路径创建文件对象 |
public File(String parent, String child) | 根据父路径名字符串和子路径名字符串创建文件对象 |
public File(File parent, String child) | 根据父路径对应文件对象和子路径名字符串创建文件对象 |
File对象可以定位文件和文件夹
File封装的对象仅仅是一个路径名,这个路径可以是存在的,也可以是不存在的(会在当前的路劲重新创建一个文件)。
构造File对象时,既可以传入绝对路径,也可以传入相对路径。绝对路径是以根目录开头的完整路径
例如 : File f = new File("C:\\Windows\\aaa.txt");
相对路径是指,在当先项目的目录下的文件
例如:项目在的位置是 C:\xiangmu 可以 File f = new File("aaa.txt");表示绝对路径的C:\xiangmu\aaa.txt
注意Windows平台使用\
作为路径分隔符,在Java字符串中需要用\\
表示一个\
。Linux平台使用/
作为路径分隔符
File类的常用方法
方法名称 | 说明 | |
---|---|---|
public boolean isDirectory() | 判断此路径名表示的File是否为文件夹 | |
public boolean isFile() | 判断此路径名表示的File是否为文件 | |
public boolean exists() | 判断此路径名表示的File是否存在 | |
public long length() | 返回文件的大小(字节数量) | |
public String getAbsolutePath() | 返回文件的绝对路径 | |
public String getPath() | 返回定义文件时使用的路径 | |
public String getName() | 返回文件的名称,带后缀 | |
public long lastModified() | 返回文件的最后修改时间(时间毫秒值) | |
public boolean createNewFile() | 创建一个新的空的文件 | |
public boolean mkdir() | 只能创建一级文件夹 | |
public boolean mkdirs() | 可以创建多级文件夹 | |
public boolean delete() | 删除由此抽象路径名表示的文件或空文件夹 | |
public String[] list() | 获取当前目录下所有的"一级文件名称"到一个字符串数组中去返回。 | |
public File[] listFiles()(常用) | 获取当前目录下所有的"一级文件对象"到一个文件对象数组中去返回(重点) |
public class FileTest {
public static void main(String[] args) throws IOException {
// 新建一个File对象;
File file=new File("src/main/java/com/test");
//判断file对象是否存在指定目录下
System.out.println(file.exists());
//若文件不存在,则以文件的形式新建
if(!file.exists())
file.createNewFile();
File file1=new File("src/main/java/com/test");
//若文件不存在,则以目录的形式新建
if(!file1.exists())
file1.mkdir();
System.out.println(file.exists());
//获取文件的路径
String s=file.getPath();//获取文件相对路径
String s2=file.getAbsolutePath();//获取文件的绝对路径
System.out.println(s);
System.out.println(s2);
System.out.println(file.getParentFile());//获取父目录
System.out.println(file.getName());//获取文件名
System.out.println(file1.getName());
System.out.println(file.isDirectory());//判断文件是否是一个目录
System.out.println(file1.isDirectory());//
//获取文件最后一次修改的时间:该时间默认是从1970年到现在为止的总毫秒数
Long sumHaomiao=file.lastModified();//返回值类型为长整形;
System.out.println(sumHaomiao);//1660651959734
Date date=new Date(sumHaomiao);//返回值类型为String,该时间格式为系统默认格式
System.out.println(date);//Tue Aug 16 20:12:39 CST 2022
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss SSS");
String string=sdf.format(sumHaomiao);//按指规则格式化时间
System.out.println(string);//2022-08-16 08:12:39 734
String String1=sdf.format(date);//2022-08-16 08:12:39 734
Long l=file.length();//获取文件大小
System.out.println(l);//0
System.out.println(file1.length());//0
//获取当前路径下的所有子文件
File file2=new File("src/main");
File[] f1=file2.listFiles();
for(File file3:f1){//遍历文件数组
System.out.println(file3.getName());
}
}
}
IO流
I表示intput,把硬盘文件中的数据读入到内存的过程,称之输入,负责读。
O表示output,把内存中的数据写出到硬盘文件的过程,称之输出,负责写。
分类
-
按流的方向分类:输入流,输出流
-
按处理/传输的数据单元:字节流,字符流 。
-
使用什么流进行数据传输,就看源数据是以什么形式存在的
四大IO流:(所有IO类的抽象父类)
InputStream字节输入流:以内存为基准,来自磁盘文件/网络中的数据以字节的形式读入到内存中去的流称为字节输入流。
OutputStream字节输出流:以内存为基准,把内存中的数据以字节写出到磁盘文件或者网络中去的流称为字节输出流。
Reader字符输入流:以内存为基准,来自磁盘文件/网络中的数据以字符的形式读入到内存中去的流称为字符输入流。
Writer字符输出流:以内存为基准,把内存中的数据以字符写出到磁盘文件或者网络介质中去的流称为字符输出流。
InputStream
以内存为基准,把磁盘文件中的数据以字节的形式读取到内存中去
构造器 | 说明 |
---|---|
public FileInputStream(File file) | 创建字节输入流管道与源文件对象接通 |
public FileInputStream(String pathname) | 创建字节输入流管道与源文件路径接通 |
方法名称 | 说明 |
---|---|
public int read() | 每次读取一个字节返回,如果字节已经没有可读的返回-1 |
public int read(byte[] buffer) | 每次读取一个字节数组返回,如果字节已经没有可读的返回-1 |
public byte[] readAllBytes() throws IOException | 直接将当前字节输入流对应的文件对象的字节数据装到一个字节数组返回 |
OutputStream
以内存为基准,把内存中的数据以字节的形式写出到磁盘文件中去的流。
构造器 | 说明 |
---|---|
public FileOutputStream(File file) | 创建字节输出流管道与源文件对象接通 |
public FileOutputStream(File file,boolean append) | 创建字节输出流管道与源文件对象接通,可追加数据 |
public FileOutputStream(String filepath) | 创建字节输出流管道与源文件路径接通 |
public FileOutputStream(String filepath,boolean append) | 创建字节输出流管道与源文件路径接通,可追加数据 |
方法名称 | 说明 |
---|---|
public void write(int a) | 写一个字节出去 |
public void write(byte[] buffer) | 写一个字节数组出去 |
public void write(byte[] buffer , int pos , int len) | 写一个字节数组的一部分出去。 |
flush() | 刷新流,还可以继续写数据 |
close() | 关闭流,释放资源,但是在关闭之前会先刷新流。一旦关闭,就不能再写数据 |
任何文件的底层都是字节,拷贝是一字不漏的转移字节,只要前后文件格式、编码一致没有任何问题。
Reader
构造方法 | 说明 |
---|---|
protected Reader() | 创建一个新的字符流 reader,其重要部分将同步其自身的 reader。 |
protected Reader(Object lock) | 创建一个新的字符流 reader,其重要部分将同步给定的对象。 |
方法摘要 | |
---|---|
abstract void close() | 关闭该流。 |
void [mark(int readAheadLimit) | 标记流中的当前位置。 |
boolean markSupported() | 判断此流是否支持 mark() 操作。 |
int read() | 读取单个字符。 |
int read(char[] c) | 将字符读入数组。 |
abstract int read(char[] cbuf, int off, int len) | 将字符读入数组的某一部分。` |
int read(CharBUffer target) | 试图将字符读入指定的字符缓冲区。 |
booleanr ready() | ready() 判断是否准备读取此流。 |
void reset() | 重置该流。 |
long skip(long n) | 跳过字符。 |
Writer
构造方法 | 说明 |
---|---|
protected Writer() | 创建一个新的字符流 writer,其关键部分将同步 writer 自身。 |
protected Writer() | 创建一个新的字符流 writer,其关键部分将同步给定的对象 |
方法 | 说明 |
---|---|
Writer append(char c) | 将指定字符追加到此 writer。 |
Writer append(CharSequence csq) | 将指定字符序列追加到此 writer。 |
Writer append(CharSequence csq,int start,int end) | 将指定字符序列的子序列追加到此 writer.Appendable 。 |
abstract void close() | 关闭此流,但要先刷新它。 |
abstract void flush() | 刷新此流。 |
void write(char[] c) | 写入字符数组。 |
abstract void write(char[] c,int off,int end) | 写入字符数组的某一部分。 |
void write(char c) | 写入单个字符。 |
void write(String s) | 写入字符串。 |
void write(String s,int off,int end) | 写入字符串的某一部分。 |
IO流:资源释放的方式
try-catch-finally
finally:放在try-catch后面的,无论是正常执行还是异常执行代码,最后一定要执行,除非JVM退出。
作用:一般用于进行最后的资源释放操作
try {
FileOutputStream f = new FileOutputStream("a.txt");
f.write(97);
} catch (IOException e) {
e.printStackTrace();
}finally{
f.close();
}