本文已参与「新人创作礼」活动,一起开启掘金创作之路。
文件
普通文件是保存在硬盘上面的。
机械硬盘构造
1.盘片:存储数据的介质
2.磁头:寻找数据,这个读写是比内存的读取慢三四个数量级
文件的分类
1.文本文件: 里面存储的是字符
2.二进制文件: 里面存储的是字节
路径描述
1.绝对路径: 从磁盘开头
2.相对路径: 以 ./ 或者 ../ 表示
java操作文件
1.文件系统相关操作
指的是通过“文件资源管理器”能够完成的一些功能
1)列出目录中有哪些文件
2)创建文件
3)创建目录
4)删除文件
5)文件重命名
......
File类使用
public static void main(String[] args) throws IOException {
File file = new File("E:/text.txt");
System.out.println(file.getParent()); //获取文件父目录
System.out.println(file.getName()); //获取文件名字
System.out.println(file.getPath()); //获取文件路径
System.out.println(file.getAbsolutePath()); //获取到绝对路径 (基准路径和相对路径的拼接)
System.out.println(file.getCanonicalPath()); //获取到绝对路径
}
public static void main(String[] args) {
File file = new File("E:/text.txt");
System.out.println(file.exists()); //是不是存在
System.out.println(file.isDirectory()); //是不是文件夹
System.out.println(file.isFile()); //是不是文件
file.createNewFile(); //创建文件
file.delete(); //删除文件
file.mkdir(); //创建文件夹
file.mkdirs(); //创建多级文件夹
}
2.文件内容相关操作
1.打开文件
2.读文件
3.写文件
4.关闭文件
针对文件内容的读写,java标准库提供了一组类 首先按照文件的内容,分成了两组
1)字节流对象,针对的是二进制文件
抽象类 子类
读:InputStream -> FileInputStream
写:OutputStream -> FileOutputStream
2)字符流对象,针对的是文本文件
抽象类 子类
读:Reader -> FileReader
写:writer -> FileWriter
InputStream
普通的读取
public static void main(String[] args) throws IOException {
InputStream inputStream = new FileInputStream("E:/text.txt");
while(true){
int ch = inputStream.read();
if(ch == -1){ //文件读完之后就会返回一个-1
break;
}
System.out.print((char)ch);
}
inputStream.close();
}
运用了池思想的读取
public static void main(String[] args) throws IOException {
InputStream inputStream = new FileInputStream("E:/text.txt");
byte[] pool = new byte[1024];
while(true){
int ch = inputStream.read(pool);
if(ch == -1){
break;
}
for(int i = 0;i<ch;i++){
System.out.print((char)pool[i]);
}
}
inputStream.close();
}
两者的区别主要在于中间有一个池,就好像一个寺庙,寺庙下面有个湖泊,和尚每天上下山是很累的,这个时候弄一个水缸,花一天的时间把水缸装满,然后可以休息三五天。这就比较优化。
上面第二个代码的ch返回的就是读取到的长度。上面两个代码还是有点不好的,你能想到嘛?
那就是流关闭的时候最好是在finally里面,如果觉得啰嗦,还可以直接把inputStream放到try后面的括号
public static void main(String[] args){
try(InputStream inputStream = new FileInputStream("E:/text.txt")) {
while(true){
int ch = inputStream.read();
if(ch == -1){
break;
}
System.out.print((char)ch);
}
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
我们没有显示的调用,但是try会帮助我们
写文件
OutputStream
public static void main(String[] args) {
try(OutputStream outputStream = new FileOutputStream("e:/text.txt")) {
byte[] buffer = {97,98,99};
outputStream.write(buffer);
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
每次写的时候都会清除里面的内容,当然也有流可以追加。这边就不加介绍
Reader
public static void main(String[] args) {
try(Reader reader = new FileReader("E:/text.txt")) {
while(true){
int ch = reader.read();
if(ch == -1){
break;
}
System.out.print((char)ch);
}
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
Writer
public static void main(String[] args) {
try(Writer writer = new FileWriter("E:/text.txt")) {
char[] ch = {'a' , 'b' , 'c' , 'd'};
writer.write(ch);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
文件操作案例
查找文件并删除
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("输入路径");
String path = sc.next();
System.out.println("输入要删除的文件");
String name = sc.next();
File cur = new File(path);
if(!cur.isDirectory()){
System.out.println("输入路径有误");
return;
}
Delete(cur , name);
}
public static void Delete(File path , String name){
File[] files = path.listFiles();
if(files == null){
return;
}
for(File f: files){
if(!f.isDirectory()){
if(f.getName().equals(name)){
f.delete();
return;
}
}else{
Delete(f , name);
}
}
}
拷贝文件
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("文件地址");
String point = sc.next();
System.out.println("拷贝地址");
String path = sc.next();
File file = new File(point);
if(!file.isFile()){
System.out.println("输入的文件有误");
return;
}
try(InputStream inputStream = new FileInputStream(file)){
try(OutputStream outputStream = new FileOutputStream(path)){
byte[] pool = new byte[1024];
while(true){
int ch = inputStream.read(pool);
if(ch == -1){
break;
}
outputStream.write(pool , 0 , ch);
}
}
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}