1. File类
1. file方法使用
方法名 | 作用 |
---|---|
boolean createNewFile() | 当此File对象所对应的文件不存在时,该方法将创建一个该File对象所指定的新文件,成功返回 true,否则返回 false |
boolean delete() | 删除 File 对象所对应的文件或路径 |
String getName() | 返回此 File 对象所对应的路径名 |
String getPath() | 返回任意文档内容 |
String getAbsolutePath() | 返回此 File 对象所对应的绝对路径名 |
boolean exists() | 判断 File 对象所对应的文件或目录是否存在 |
boolean isFile() | 判断 File 对象所对应的是否是文件,true则代表是文件 |
boolean createNewFile() | 判断 File 对象所对应的是否是目录,而不是文件 |
---|---|
boolean delete() | 删除 File 对象所对应的文件或路径 |
String getName() | 返回此 File 对象所对应的路径名 |
String getPath() | 返回任意文档内容 |
String getAbsolutePath() | 返回此 File 对象所对应的绝对路径名 |
boolean exists() | 判断 File 对象所对应的文件或目录是否存在 |
boolean isFile() | 判断 File 对象所对应的是否是文件,true则代表是文件 |
public class Text1 {
public static void main(String[] args) {
try {
exaple11();
exaple22();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void exaple11() throws Exception {
File file=new File("E://Text55//aa.txt");
if(!file.exists()) {
//创建文件
file.createNewFile();
}
System.out.println("判断文件是否存在:"+file.exists());
System.out.println("文件名:"+file.getName());
System.out.println("文件路径:"+file.getPath());
System.out.println("文件绝对路径:"+file.getAbsolutePath());
System.out.println("规范路径"+file.getCanonicalPath());
System.out.println("文件大小:"+file.length());
System.out.println("判断是否为文件:"+new File("E://Text55").isFile());
System.out.println("获取所以目录和文件:"+new File("E://Text55").list().length);
}
//创建多级文件夹
public static void exaple22() throws Exception {
File file=new File("E://Text66//Text77//Text88");
file.mkdirs();
}
}
2. 文件过滤器
Java中提供了FilenameFilter接口可以快速帮助实现文件的查找过滤。
在 File 类的 list() 方法中可以接受一个 FilenameFilter 参数,通过该参数可以仅列出符合条件的文件。
使用时需要实现FilenameFilter接口,且重写accept方法,该方法中完成对文件过滤的逻辑判断处理。
public static void filePaht() {
File file=new File("E:\\fil1");
if(file.isDirectory()) {
String[]list=file.list(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return name.endsWith("txt");
}
});
System.out.println("tex文件有:"+list.length);
for(String name:list)
System.out.println(name);
}
}
//accept方法名必须遵循接口规范,保持一致
//endsWith是判断是否以指定字符串结尾
2. 字节流
按照流向分类:
输入流:只能从中读取数据,而不能向其写入数据。
输出流:只能向其写入数据,而不能从中读取数据。
以程序为参照物,将数据从数据源中读取到程序内存的过程称为输入。
以程序为参照物,将数据从内存中写入到目标的过程称为输出。
按照数据格式分类:
字节流:数据传输以字节形式实现,可以读写任何文件(文本、图片、视频等)
字符流:数据传输以字符形式实现,一般针对文本文件读写更有优势。
InputStream抽象类(字节流中的输入流)
方法名 | 作用 |
---|---|
public abstract int read() | 从输入流中读取单个字节,返回所读取的字节数据(字节数据可直接转换为 int 类型) |
public int read(byte[ ] b) | 从输入流中最多读取 b.length 个字节的数据,并将其存储在字节数组中,返回实际读取的字节数 |
public void close() | 将输入流所占用的资源释放关闭 |
OutputStream(字节流中的输出流)
方法名 | 作用 |
---|---|
public abstract void write(int b) | 将指定的字节写入到输出流中 |
public void write(byte[ ] b) | 将字节数组中的数据写入到输出流中 |
public void write(byte[ ] b,int off,int len) | 将字节数组中从 off 位置开始,长度为 len 的字节 写入到输出流中 |
public void flush() | 刷新缓冲区 |
public void close() | 将输出流所占用的资源释放关闭 |
InputStream的子类FileInputStream(输入流)
OutputStream的子类FileOutputStream(输出流)
// 第七个案例:使用文件流对象对文件进行存操作--将数据写入文件中--使用字节流和字节数组的方法将"Hello World!!Hello World!!Hello World!!"写入aa.txt文件中
public static void demo1() throws IOException {
File file=new File("E:\\fil1\\aa.txt");
if(file.exists()) {
file.createNewFile();
}
OutputStream outputStream=new FileOutputStream(file,true);
String str="hello world!hello world!,今天天气真好";
outputStream.write(str.getBytes());
outputStream.close();
}
// 使用文件流对象对文件进行取操作--将数据从文件中取出进行显示--使用字节流和字节数组的方法
public static void demo2() throws IOException {
File file=new File("E:\\fil1\\aa.txt");
InputStream inputSteams=new FileInputStream(file);
byte[]by=new byte[(int) file.length()];
inputSteams.read(by);
System.out.println(new String(by,"utf-8"));
inputSteams.close();
}
3. 字符流
Reader抽象类(输入流)
方法名 | 作用 |
---|---|
public int read() | 从输入流中读取字符,返回读取字符数据 |
public int read(char[ ] c) | 从输入流中最多读取 c.length 个字符的数据,并将其存储在字符数组中,返回实际读取的字符数 |
public abstract void close() | 将输入流所占用的资源释放关闭 |
Writer抽象类(输出流)
方法名 | 作用 |
---|---|
public void write(String str) | 将字符串 str 写入到指定的输出流中 |
public void write(char [ ] c) | 将字符数组中的数据写入到输出流中 |
public abstract void flush() | 刷新缓冲区 |
public abstract void close() | 将输出流所占用的资源释放关闭 |
FileReader是Reader的子类,该类用于将文本文件的数据读取到程序中。
FileWriter是Writer的子类,该类用于将程序中的文本写入到文件中
public static void demo5() throws IOException {
FileReader fd=new FileReader("E:\\fil1\\aa.txt");
FileWriter fw=new FileWriter("E:\\fil1\\bb.txt",true);
fw.write("\n这是新加入的数据=====\n");
int data;
while((data=fd.read())!=-1){
// 将数据写入到文件中
fw.write(data);
}
fd.close();
fw.close();
}
//如果写入后有乱码,可以将记事本文件另存为UTF-8编码格式
4. 字节缓冲流和字符缓冲流
1. 字节缓冲流
首先要明确一个概念:对文件或其他目标频繁的读写操作,效率低,性能差。
使用缓冲流的好处是,能够高效的读写信息,原理是将数据先缓冲起来,然后一起写入或者读取出来。
BufferedInputStream:为另一个输入流添加一些功能,在创建BufferedInputStream时,会创建一个内部缓冲区数组,用于缓冲数据。
BufferedOutputStream:通过设置这种输出流,应用程序就可以将各个字节写入底层输出流中,而不必针对每次字节写入调用底层系统。
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
* 缓存的目的:解决写入文件操作时,频繁的操作文件所带来的性能降低的问题
* BufferedOutputStream内部默认的缓存大小是8kb,每次写入时存储到缓存中的byte数组中, 会把数组中的数据写入文件,并且下标归零
*/
public class BufferedStreamDemo {
public static void main(String[] args) {
byteWriter();
byteReader();
}
private static void byteReader() {
File file = new File("F:/test.txt");
try {
InputStream in = new FileInputStream(file);
// 构造一个字节缓冲流
BufferedInputStream bis = new BufferedInputStream(in);
byte[] flush = new byte[1024];
int len = -1;
while ((len = bis.read(flush)) != -1) {
System.out.println(new String(flush, 0, len));
}
bis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}// byteReader
private static void byteWriter() {
File file = new File("F:/test.txt");
try {
OutputStream out = new FileOutputStream(file, true);
BufferedOutputStream bos = new BufferedOutputStream(out);
String info = "你爱Java吗?";
bos.write(info.getBytes());
bos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}// byteWriter
}
2. 字符缓冲流
BufferedReader:从字符输入流中读取文本,缓冲各个字节,从而实现字符、数组和行的高效读取。
BufferedWriter:将文本写入字符输出流,缓冲各个字符,从而提供单个字符、数组和字符串的高效写入。
private static void charWriter() {
File file = new File("F:/test.txt");
try {
Writer writer = new FileWriter(file, true);
BufferedWriter bw = new BufferedWriter(writer);
bw.write("我爱Java SE");
bw.flush();
bw.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}// charWriter
private static void charReader() {
File file = new File("F:/test.txt");
try {
Reader reader = new FileReader(file);
// 为字符流提供缓存,达到高效读取的目的
BufferedReader br = new BufferedReader(reader);
char[] buf = new char[1024];
int len = -1;
while ((len = br.read(buf)) != -1) {
System.out.println(new String(buf, 0, len));
}
br.close();// 不用关闭reader,是因为关闭br时内部已经帮我们关闭了reader
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}// charReader
OutputStreamWriter是字符流通向字节流的桥梁
继承父类的共性成员方法:
void write(int c):写入单个字符
void write(char[] cbuf):写入字符数组
abstract void write(char[] cbuf, int off, int len):写入字符数组的某一部分,off:数组的开始索引,len:写的字符个数
void writ(String str):写入字符串
void writ(String str, int off, int len):写入字符数组的某一部分,off:数组的开始索引,len:写的字符个数
void flush():刷新该流的缓冲
void close():关闭此流,但要先刷新它
构造方法:
OutputStreamWriter(OutputStream out):创建使用默认字符编码的OutputStreamWriter
OutputStreamWriter(OutputStream out, String charsetName):创建使用指定字符集OutputStreamWriter
参数:
OutputStreamWriter:字节输出流,可以用来写转换之后的字节到文件中
String charsetName:指定的编码表名称且不区分大小写,不指定则使用默认(IDEA默认UTF-8编码)
使用步骤:
1.创建OutputStreamWriter对象,构造方法中传递字节输出流和指定的编码表名称
2.使用OutputStreamWriter对象中的方法write,把字符转换为字节储存缓冲区中(编码)
3.使用OutputStreamWriter对象中的方法flush,把内存缓冲区中的字节刷新到文件中(使用字节流写字节的过程)
4.释放资源
InputStreamReader类是从字节流到字符流的桥接器
java.io.InputStreamReader extends Reader
InputStreamReader:是字节流通向字符流的桥梁。它使用指定的charset读取字节并将其解码为字符。
继承父类的共性成员方法:
int read():读取单个字符并返回
int read(char[] cbuf):一次读取多个字符,将字符读入数组
void close():关闭该流并释放与之相关联的所有资源
构造方法:
InputStreamReader(InputStreamReader in):创建一个使用默认字符集的InputStreamReader
InputStreamReader(InputStreamReader in, String charsetName):创建使用指定字符集的InputStreamReader
参数:
InputStream in:字符输入流,用来读取文件中保存的字节
String charsetName:指定的编码表名称且不区分大小写,不指定则使用默认(IDEA默认UTF-8编码)
使用步骤:
1.创建InputStreamReader对象,构造方法中传递输入流和指定的编码表名称
2.使用InputStreamReader对象中的方法read读取文件
3.释放资源
注意事项:
构造方法中给你指定的编码表名称要和文件的编码表相同,否则会发生乱码