目录
1.文件
1.1狭义上的文件
硬盘上保存的数据,都是“文件”来组织的,本质上都是二进制或是字符组织的数组,被打包成一个文件放在硬盘上。常见的文件有图片(png),文本(txt),可执行文件(exe),音频,视频……
文件夹也是一种特殊的文件,也叫目录。
文件通常储存在硬盘上。
1.2广义上的文件
操作系统的主要功能就是对计算机资源进行统一管理与分配。
对于Linux来说,所有的计算设备都会被描述(抽象)成文件。例如:网卡,键盘,打印机……
当一个进程启动后去申请计算机资源时,系统会把他用到的所有资源以文件的形式分配给进程,并加入到文件描述符表中。
1.APP1通过网络发送数据,相当于往网卡里写数据(写文件的操作)。
2.APP1要接受APP2的响应,想到于在网卡里读数据(读文件的操作)。
2.树形结构组织和目录
在Windows中查看树:
Windows中的此电脑:
C盘:在具体的盘符下还可以保存真实的目录与文件
D盘
在Linux中没有此电脑和盘符的概念,是以根目录开始向下发展的n叉树。
如何访问一个具体的文件
C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2022.2
位置表述bin文件的所有目录
C:表示盘符,磁盘驱动器
\表示分隔符
Windows中默认的分隔符是反斜杠(\)
在其他系统中默认的分隔符基本上都是斜杠(/)
绝对路径:从根目录开始一直到目标程序的表示方式。
相对路径:从当前目录开始到目标程序的表示方式。
C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2022.2\bin\idea64.exe
要求:访问idea64.exe
情况1:
当前目录为:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2022.2\bin
方法:.\idea64.exe
.表示当前目录
情况2:
当前目录为:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2022.2
方法:.\bin\idea.exe
情况3:
当前目录为:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2022.2\jbr
方法:../bin/idea.exe
..表示上一层目录
如果要通过相对路径的方式访问到目标文件,那么就先确认自己当前的工作目录。
对于idea来说,起始工作目录就是工程的目录。
对于cmd来说,起始工作目录是当前用户目录。
3.Java操作文件
操作系统的一个重要功能就是对文件的管理,每个操作系统都有自己的一套系统API调用,Java作为一个跨平台语言,JVM针对不同的操作系统做了一层封装,我们只需要使用JDK提供的关于文件操作的API就可以完成不同系统下的文件操作
1.File类
io:Input/Output
输入是以内存为参照物的,输入指的是从外部输入到内存,输出指的是把内存的数据输出到硬盘。
1.属性
pathSeparator:根据不同的系统返回系统默认的分隔符
2.构造方法
反斜杠需要转义
import java.io.File;
public class Domo01_File {
public static void main(String[] args) {
//这只是一个Java的对象,并不一定真实存在
File file=new File("C:/wuyong/test.txt");
System.out.println(file);
}
}
3.常用方法
public class Domo02_File {
public static void main(String[] args) throws IOException {
//指定绝对路径
File file=new File("C:/wuyong/test.txt");
//获取父目录
System.out.println(file.getParent());
//获取文件名
System.out.println(file.getName());
//获取路径
System.out.println(file.getPath());
//获取绝对路径
System.out.println(file.getAbsoluteFile());
//获取修饰过的文件路径
System.out.println(file.getCanonicalPath());
//判断文件是否真实存在
System.out.println(file.exists());
//判断文件是否是一个目录
System.out.println(file.isDirectory());
//判断文件是否是一个普通文件
System.out.println(file.isFile());
}
}
public class Domo03_File {
public static void main(String[] args) throws IOException {
//指定相对路径
File file=new File("./test.txt");
//获取父目录
System.out.println(file.getParent());
//获取文件名
System.out.println(file.getName());
//获取路径
System.out.println(file.getPath());
//获取绝对路径
System.out.println(file.getAbsoluteFile());
//获取修饰过的文件路径
System.out.println(file.getCanonicalPath());
//判断文件是否真实存在
System.out.println(file.exists());
//判断文件是否是一个目录
System.out.println(file.isDirectory());
//判断文件是否是一个普通文件
System.out.println(file.isFile());
}
}
public class Domo04_Create {
public static void main(String[] args) throws IOException {
//通过绝对路径创建file对象
File file=new File("C:/wuyong/test.txt");
boolean value = file.createNewFile();
if(value){
System.out.println("创建成功");
}else {
System.out.println("创建失败");
}
}
}
public class Domo05_Delete {
public static void main(String[] args) {
File file=new File("C:/wuyong/test.txt");
boolean result=file.delete();
if(result){
System.out.println("删除成功");
}else{
System.out.println("删除失败");
}
}
}
public class Domo06_FileLIst {
public static void main(String[] args) {
//指定一个目录的路径
File file=new File("C:/wuyong");
String[]list= file.list();
System.out.println(Arrays.toString(list));
File[]list1=file.listFiles();
System.out.println(Arrays.toString(list1));
}
}
public class Domo07_Mkdir {
public static void main(String[] args) {
File file=new File("C:/wuyong/test.txt");
//创建单个目录
boolean result=file.mkdir();
if(result){
System.out.println("创建成功");
}else {
System.out.println("创建失败");
}
}
}
public class Domo08_Mkdirs {
public static void main(String[] args) {
File file=new File("C:/wuyong/test.txt/a/b");
boolean result=file.mkdirs();
if(result){
System.out.println("创建成功");
}else {
System.out.println("创建失败");
}
}
}
public class Domo09_Rename {
public static void main(String[] args) {
//定义源文件
File sourceFile=new File("C:/wuyong/test.txt");
//定义目标文件
File destFile=new File("C:/wuyong/haha.txt");
boolean result=sourceFile.renameTo(destFile);
if(result){
System.out.println("修改成功");
}else {
System.out.println("修改失败");
}
}
}
public class Domo10_CanWrite {
public static void main(String[] args) {
File file=new File("C:/wuyong/test.txt");
System.out.println(file.canRead());
System.out.println(file.canWrite());
}
}
4.文件内容的读写——数据流
字节流(处理二进制文件)
抽象类 实现类
输入流:InputStream FileInputStream
输出流:OutputStream FileOutputStream
字符流(处理文本文件)
输入流:Read FileReader
输出流:Write FileWriter
1.InputStream概述
public class Domo11_InputStream_Read01 {
public static void main(String[] args) throws IOException {
//创建一个文件对象
File file=new File("C:/wuyong/text.txt");
//创建一个输入流
InputStream inputStream = new FileInputStream(file);
//读取文件内容
while (true){
int read=inputStream.read();
if(read==-1){
break;
}
System.out.println(read);
}
}
}
public class Domo12_InputStream_Read02 {
public static void main(String[] args) throws IOException {
//创建一个文件对象
File file=new File("C:/wuyong/text.txt");
//创建一个输入流
InputStream inputStream = new FileInputStream(file);
//定义一个数组,用来保存读取到的数据
byte[]bytes=new byte[1024];
//读取文件内容
while (true){
//读到的数据会被填充到数组中,返回读取数据的长度
int len =inputStream.read(bytes);
if(len ==-1){
break;
}
for (int i = 0; i < len; i++) {
System.out.println(bytes[i]);
}
}
}
}
2.FileInputStream概述
bytes作为一个输出型的参数,数据会被填充到数组里。
传入方法时是一个空数组,读取数组长度个字节,并返回读取到的字节数,供调用方法做判断。
打开一个文件,相当于把文件放入文件描述符表中。
用完后要关闭,如果不关闭,文件描述符表会被填满,导致无法打开文件
3.OutputStream
缓冲区是内存中的一个区域。
文件内容一般都先写到缓冲区里,缓冲区里的文件什么时候写入文件是由操作系统决定的
如果缓冲区的内容还没有写完,写入文件就可以使用flsh()方法。
完成写方法之后,使用fluh()强制刷新缓冲区,确保文件内容被立即写入。
用输入流的方式写入文件,会把之前的内容覆盖掉。
4.FileReader
public class Domo14_FileReader {
public static void main(String[] args) throws IOException {
File file = new File("C:/wuyong/test.txt");
FileReader fileReader = new FileReader(file);
while (true){
int read=fileReader.read();
if(read==-1){
break;
}
System.out.println(read);
}
fileReader.close();
}
}
5.FileWriter
没有换行符,如果需要换行,需要手动加入换行符
public class Domo15_FileWriter {
public static void main(String[] args) throws IOException {
File file = new File("C:/wuyong/test.txt");
FileWriter fileWriter=new FileWriter(file);
fileWriter.write("你好");
fileWriter.write("hello");
fileWriter.flush();
fileWriter.close();
}
}
6.Scanner简化读操作
public class Domo16_Scanner {
public static void main(String[] args) throws IOException {
FileInputStream fileInputStream=new FileInputStream("C:/wuyong/test.txt");
//根据输入流创建scanner文件
Scanner scanner=new Scanner(fileInputStream,"UTF-8");
//循环读取
while (scanner.hasNext()){
String next=scanner.next();
System.out.println(next);
}
fileInputStream.close();
scanner.close();
}
}
nextLine读取一行
next()读取一个单词
7.PrintWriter
Println与平常的输出方法一样
public class Domo17_PrintWriter {
public static void main(String[] args) throws IOException {
//创建一个输出流
FileOutputStream fileOutputStream=new FileOutputStream("C:/wuyong/test.txt");
PrintWriter printWriter=new PrintWriter(fileOutputStream);
printWriter.println("你好世界!");
printWriter.println("hello world");
printWriter.flush();
fileOutputStream.close();
}
}
5.练习
public class Exe01 {
public static void main(String[] args) throws IOException {
//1.接收用户的扫描路径
System.out.println("请输入要扫描的路径");
Scanner scanner=new Scanner(System.in);
String rootPath=scanner.next();
//2.判断路径是否有效
File root=new File(rootPath);
//2.1判断路径是否存在
if(!root.exists()){
System.out.println("路径不存在");
return;
}
//2.1判断file是不是一个目录
if(!root.isDirectory()){
System.out.println("指定的路径不是一个有效目录");
return;
}
//3.接受关键字
System.out.println("请输入关键字");
String key=scanner.next();
if(key==null||"".equals(key)){
System.out.println("关键字不能为空");
return;
}
//4.扫描目录下的所有文件
scan(root,key);
}
private static void scan(File root, String key) throws IOException {
//1.先获取root目录下的所有文件
File[]files=root.listFiles();
if(files==null||files.length==0){
return;
}
for (int i = 0; i < files.length; i++) {
File temp=files[i];
if(temp.isFile()){
//如果是文件,判断文件名中是否包含关键字
String fileName=temp.getName();
if(fileName.contains(key)){
System.out.println("找到文件"+temp.getCanonicalPath()+",是否删除");
Scanner scanner=new Scanner(System.in);
String choice=scanner.next();
if(choice.equalsIgnoreCase("y")){
temp.delete();
}
}
}else {
//如果是目录,就递归
scan(temp,key);
}
}
}
}
public class Exe02 {
public static void main(String[] args) {
//1.接受用户的源文件路径
System.out.println("请输入源文件路径");
Scanner scanner=new Scanner(System.in);
String sourcePath=scanner.next();
File sourceFile=new File(sourcePath);
//2.1文件是否存在
if(!sourceFile.exists()){
System.out.println("源文件不存在");
return;
}
//2.2判断是不是一个文件
if(!sourceFile.isFile()){
System.out.println("源文件无效");
return;
}
//3.接受目标文件
System.out.println("请输入目标文件");
String destPath=scanner.next();
File destFile=new File(destPath);
//3.1判断目标文件是否存在
if(destFile.exists()){
System.out.println("目标文件已存在");
return;
}
if(!destFile.getParentFile().exists()){
System.out.println("目标文件的父目录找不到");
return;
}
//循环读取源文件
try(FileInputStream inputStream=new FileInputStream(sourceFile);
FileOutputStream fileOutputStream=new FileOutputStream(destFile)) {
//定义一个byte数组
byte[]bytes=new byte[1024];
while (true){
int len=inputStream.read(bytes);
if(len==-1){
break;
}
fileOutputStream.write(bytes);
fileOutputStream.flush();
}
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}