目录
1.什么是文件?
1.在cmd中查看指定目录的树形结构语法
2.文件路径
从当前目录开始找到目标程序(一个点)
返回到上一级目录,再找目标程序(两个点)
2.Java中文件操作
1.File概述
1.属性
2. 构造方法
3.常用方法
代码展示:
4.常用方法2
3. 文件内容的读写---数据流
1.InputStream概述
2. FileInputStream
构造方法:
1.输入数据到文件
3.字符流的输出流
4.Scanner类
5.printWrite类
4.练习扫描文件并删除
1.步骤
2.代码:
3.结果
4.进行普通文件的复制
1.步骤
2.代码:
3.结果:
1.什么是文件?
1.狭义上的文件
硬盘上保存的数据,都是“文件”来组织的,本质上都是二进制或是字符组织的数组,被打包成一个文件存在硬盘上。常见的文件有图片(png),文本(txt),可执行文件(exe)等
2.广义上的文件
操作系统的主要功能就是对计算机资源进行统一管理与分配。对于Linux来说,所有的计算设备都会被描述(抽象)成文件。(例如:网卡,键盘,打印机)等
树形结构组织:随着文件越来越多,如何进行文件的组织呢?就是按照层级结构进行组织---也就是数据结构中的树形结构。而我们平时所谓的文件夹或者目录,就是专门用来存放管理信息的
3.在cmd中查看指定目录的树形结构
tree 文件名
2.文件路径
绝对路径:从根目录开始一直到目标程序的表示方式
相对路径:从当前目录开始,表示目标程序路径的方式
从当前目录开始找到目标程序(一个点)
语法:.+目标程序目录
返回到上一级目录,再找目标程序(两个点)
语法: ..+目标程序目录
2.Java中文件操作
- 输入输出是以内存为参照物的,
- 输入指的是从外部输入到内存,
- 输出指的是把内存中的数据输出到外部(磁盘,网卡等)
1.File概述
1.属性
根据不同的系统返回系统默认的分隔符
2. 构造方法
一般常用第二种
File file = new File("D:\\temp");
File file1 = new File("D:/temp");
反斜杠需要进行转义
3.常用方法
代码展示:
public static void main(String[] args) throws IOException {
// 指定绝对或相对路径并创建一个File对象
File file = new File(".\\src\\main\\resources\\static\\image");
// 获取父目录
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());
// 获取是否已存在
System.out.println(file.exists());
// 获取是不是一个文件
System.out.println(file.isFile());
// 获取是不是一个目录
System.out.println(file.isDirectory());
}
平常我们使用标准路径获得的路径比较整洁
4.常用方法2
1.创建一个文件
public static void main(String[] args) throws IOException {
File file = new File("D:\\temp\\test\\hello.txt");
boolean res = file.createNewFile();
if (res) {
System.out.println("创建成功!");
} else {
System.out.println("创建失败");
}
}
2.删除一个文件
public static void main(String[] args) throws IOException {
File file = new File("D:\\temp\\test\\hello.txt");
boolean res = file.delete();
if (res) {
System.out.println("删除成功!");
} else {
System.out.println("删除失败");
}
}
3返回file对象代表的目录下的所有文件,以file对象表示
public static void main(String[] args) {
File file = new File("D:\\temp\\test");
//获取目录下的文件和子目录
String[] list = file.list();
System.out.println(Arrays.toString(list));
//获取目录下的文件对象数组
File[] files = file.listFiles();
System.out.println(Arrays.toString(files));
}
4.创建单个目录
public static void main(String[] args) {
File file = new File("D:\\temp\\test01");
//创建单个目录
boolean res = file.mkdir();
if (res) {
System.out.println("创建成功!");
} else {
System.out.println("创建失败");
}
}
5.创建一连串目录
public static void main(String[] args) {
File file = new File("D:/temp/test01/a/b/c");
//创建单个目录
boolean res = file.mkdirs();
if (res) {
System.out.println("创建成功!");
} else {
System.out.println("创建失败");
}
}
6.修改文件名称
public static void main(String[] args) {
File file = new File("D:/temp/test/hello.txt");
File file1 = new File("D:/temp/test/hi.txt");
//创建单个目录
boolean res = file.renameTo(file1);
if (res) {
System.out.println("修改成功!");
} else {
System.out.println("修改失败");
}
}
7.查看文件读写状态
public static void main(String[] args) {
File file = new File("D:/temp/test/hello.txt");
System.out.println(file.canRead());
System.out.println(file.canWrite());
}
3. 文件内容的读写---数据流
1.InputStream概述
InputStream只是一个抽象类,要使用还需要具体的实现类。从文件中读取,使用FileInputStream
方法:
1.读取文件中的数据
public static void main(String[] args) throws IOException {
File file = new File("D:/temp/test/hello.txt");
InputStream inputStream = new FileInputStream(file);
while (true) {
int resd = inputStream.read();
if (resd == -1) {
break;
}
System.out.println(resd);
}
}
2.用数组保存读取出来的数据
public static void main(String[] args) throws IOException {
File file = new File("D:/temp/test/hello.txt");
InputStream inputStream = new FileInputStream(file);
byte[] bytes = new byte[1024];
while (true) {
int resd = inputStream.read(bytes);
if (resd == -1) {
break;
}
for (int i = 0; i < resd; i++) {
System.out.println(bytes[i]);
}
}
}
2. FileInputStream
构造方法:
1.输入数据到文件
调用write方法就表示通过输出流,把内容写到指定的文件中
文件内容一般先写到缓冲区,缓冲区什么时候将内容写入文件,取决于操作系统
强制写入文件,调用 flash()方法,确保文件内容被立即写入
public static void main(String[] args) throws IOException {
File file = new File("D:/temp/test/hello.txt");
//创建一个输出流
FileOutputStream outputStream = new FileOutputStream(file);
outputStream.write('a');
outputStream.write('b');
outputStream.write('c');
//刷新缓存区
outputStream.flush();
//关闭输出流
outputStream.close();
}
用输出流的方式去写文件,会把之前的文件内容清空
2.读取字符
public static void main(String[] args) throws IOException {
File file = new File("D:/temp/test/hello.txt");
//根据file对象创建一个Reader面向字符的输入流
FileReader fileReader = new FileReader(file);
while (true) {
int res = fileReader.read();
if (res == -1) {
break;
}
//打印
System.out.println(res);
}
//关闭输出流
fileReader.close();
}
3.字符流的输出流
public static void main(String[] args) throws IOException {
File file = new File("D:/temp/test/hello.txt");
//根据file对象创建一个Reader面向字符的输入流
FileWriter fileWriter = new FileWriter(file);
fileWriter.write("你好");
fileWriter.write("小锦鲤");
//刷新
fileWriter.flush();
//关闭输出流
fileWriter.close();
}
换行需要自己写转义字符\n进行换行
用输出流的方式去写文件,会把之前的文件内容清空
4.Scanner类
public static void main(String[] args) throws IOException {
//根据file对象创建一个Reader面向字符的输入流
FileInputStream inputStream = new FileInputStream("D:/temp/test/hello.txt");
Scanner sc = new Scanner(inputStream,"UTF-8");
while (sc.hasNext()) {
String s = sc.nextLine();
System.out.println(s);
}
}
5.printWrite类
public static void main(String[] args) throws IOException {
//根据file对象创建一个Reader面向字符的输入流
FileOutputStream outputStream = new FileOutputStream("D:/temp/test/hello.txt");
PrintWriter printWriter = new PrintWriter(outputStream);
printWriter.println("小锦鲤");
printWriter.println("hello");
printWriter.flush();
printWriter.close();
}
4.练习扫描文件并删除
1.步骤
2.代码:
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
public class deleteFile {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
System.out.println("请输入要删除的文件的绝对路径");
String path = sc.next();
File file = new File(path);
//检查是否有效
//1.检查路径是否存在
if (!file.exists()) {
System.out.println("路径不存在哦~");
return;
}
//2.检查是否为有效目录
if (!file.isDirectory()) {
System.out.println("不是有效目录哦~");
return;
}
//让用户输入目标字符
System.out.println("请输入要删除的文件名称");
String name = sc.next();
//判断名称是否合法
//使用 "".equals(name) 避免出现异常
if (name == null || "".equals(name)) {
System.out.println("名称不可以为空哦~");
return;
}
//递归进行删除
delete(file, name);
}
public static void delete(File file, String name) throws IOException {
//获取file下所有文件
File[] files = file.listFiles();
//递归终止条件
if (files == null || files.length == 0) {
return;
}
//遍历数组中的每个文件
for (int i = 0; i < files.length; i++) {
//判断是否为文件
if (files[i].isFile()) {
//是文件,判断是否为目标文件
String fileName = files[i].getName();
if (fileName.contains(name)) {
System.out.println("找到文件"+files[i].getCanonicalFile()+"是否确定删除?(Y/N)");
//接受用户的选择
Scanner sc = new Scanner(System.in);
String order = sc.next();
//删除(忽视大小写)
if ("Y".equalsIgnoreCase(order)) {
files[i].delete();
System.out.println(files[i].getCanonicalFile()+"删除成功~");
}
}
} else {
//是目录则继续递归
delete(files[i], name);
}
}
}
}
3.结果
4.进行普通文件的复制
1.步骤
文章来源:https://www.toymoban.com/news/detail-457446.html
2.代码:
import java.io.*;
import java.util.Scanner;
public class copyFile {
public static void main(String[] args) throws FileNotFoundException {
System.out.println("请输入要复制的文件的路径");
Scanner sc = new Scanner(System.in);
String sourceName = sc.next();
//创建文件类实例
File sourcefile = new File(sourceName);
//判断文件是否存在
if (!sourcefile.exists()) {
System.out.println("文件不存在~");
return;
}
//判断是否是文件
if (!sourcefile.isFile()) {
System.out.println("源文件不是一个有效的文件");
return;
}
//接受用户输入的目标文件
System.out.println("请输入目标文件路径(绝对路径)");
String destName = sc.next();
File destFile = new File(destName);
//判断目标文件路径是否存在
if (destFile.exists()) {
System.out.println("目标文件已存在~");
}
//判断目标文件的夫目录是否存在
if (!destFile.getParentFile().exists()) {
System.out.println("目标文件路径父目录不存在");
}
//循环读取源文件到目标路径
try {
FileInputStream inputStream = new FileInputStream(sourcefile);
FileOutputStream outputStream = new FileOutputStream(destFile);
//定义byte【】的数组用来做输出型参数
byte[] bytes = new byte[1024];
while (true) {
int len = inputStream.read(bytes);
if (len == -1) {
break;
}
//写入目标文件
outputStream.write(bytes);
//刷新缓存
outputStream.flush();
}
System.out.println("复制成功");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
3.结果:
文章来源地址https://www.toymoban.com/news/detail-457446.html
到了这里,关于【Java EE 初阶】文件操作的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!