java本地文件操作


学而不思则罔,思而不学则殆。


java本地文件操作

一、File类简介
二、文件的创建、删除、重命名
三、文件夹的创建、重命名、删除
四、文件属性的读取
五、文件属性的设置
六、遍历文件夹
七、文件的简单读写

一、File类的简介

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package file;
import java.io.File;
public class HelloFile {
public static void main(String[] args) {
//创建文件对象
File file = new File("hello.txt");
//判断文件 ,返回布尔
file.isFile();
System.out.println(file.isFile());
//路径(文件夹)
file.isDirectory();
System.out.println(file.isDirectory());
}
}

运行结果:

1
2
false
false

二、文件的创建、删除、重命名

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
public class HelloFile {
public static void main(String[] args) {
//创建文件对象
File file = new File("hello.txt");
//hello.txt会存在于当前目录
//相对路径格式:xxx//hello.txt
//绝对路径格式:D://AAA//BBB//hello.txt
//"../hello.txt" 表示上一级文件结构
//是否存在
if(file.exists()){
//文件
System.out.println(file.isFile());
//路径(文件夹)
System.out.println(file.isDirectory());
//重命名
//文件结构必须处于同一个分区
//文件处于不同的分区,需要使用文件的拷贝,而不是重命名
// File nameto = new File("src/new Hello.txt");
// file.renameTo(nameto);
//文件删除
// file.delete();
// System.out.println("文件删除成功");
}else {
System.out.println("文件不存在");
//创建文件
try {
file.createNewFile();
System.out.println("文件已经成功创建");
} catch (IOException e) {
System.out.println("文件无法创建");
}
}
}
}

三、文件夹的创建、重命名、删除

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
//文件夹的创建、重命名、删除
public class HelloFile {
public static void main(String[] args) {
//文件夹的创建
File folder = new File("my new folder");
folder.mkdir();
System.out.println("文件夹创建完成");
//关于folder.mkdir(),返回一个布尔类型
if(folder.mkdir()){
System.out.println("文件夹创建完成");
}else{
if(folder.exists()){
System.out.println("文件夹已经存在不用创建了");
}else{
System.out.println("文件夹创建失败");
}
}
//文件夹的重命名
File folder = new File("my new folder");
File newfolder = new File("my new folder-new");
if(folder.renameTo(newfolder)){//返回值布尔类型
System.out.println("done");
}else{
System.out.println("fail");
}
//文件夹的删除
File folder = new File("my new folder/two");
folder.delete();//返回值布尔类型
//被删除的文件夹必须为空才会删除成功
if(folder.delete()){
System.out.println("done");
}else{
System.out.println("fail");
}
}
}

四、文件属性的读取

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public class HelloFile {
public static void main(String[] args) {
//文件属性的读取
File file = new File("text.txt");
//判断文件是否存在
System.out.println("判断文件是否存在"+file.exists());
//读取文件名称
System.out.println("读取文件名称"+file.getName());
//读取文件路径
System.out.println("读取文件路径"+file.getPath());
//读取文件绝对路径
System.out.println("读取文件绝对路径"+file.getAbsolutePath());
//获取文件父级路径
System.out.println("获取文件父级路径"+new File(file.getAbsolutePath()).getParent());
//读取文件大小
System.out.println("读取文件大小"+file.length()+"byte");//字节
System.out.println("读取文件大小"+(float)file.length()/1000+"KB");
//判断文件是否被隐藏
System.out.println("判断文件是否被隐藏"+file.isHidden());
//判断文件是否可读
System.out.println("判断文件是否可读"+file.canRead());
//判断文件是否可写
System.out.println("判断文件是否可写"+file.canWrite());
//判断文件是否为文件夹
System.out.println("判断文件是否为文件夹"+file.isDirectory());
}

五、文件属性的设置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class HelloFile {
public static void main(String[] args) {
//文件属性的设置
File file = new File("text.txt");
if (file.exists()){
//将文件设定为可写
file.setWritable(true);
file.setWritable(false);
//将文件设定为可读
file.setReadable(false);
file.setReadable(true);
//将文件设定为只读
file.setReadOnly();
}

六、遍历文件夹

1
2
3
4
5
6
7
8
9
10
11
12
13
//遍历文件夹
public static void printFiles(File dir){
printFiles(new File("绝对路径"));
if (dir.isDirectory()) {
File next[] = dir.listFiles();//listFiles()返回一个数组
for(int i= 0;i<next.length;i++){
System.out.println(next[i].getName());
if(next[i].isDirectory()){
printFiles(next[i]);
}
}
}
}

七、文件的简单读写

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package file;
import java.io.File;
import java.io.IOException;
public class HelloFile {
public static void main(String[] args) {
File file = new File("test.txt");
if(file.exists()){
System.err.println("exist");
try{
FileInputStream fis = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(fis,"UTF-8");
BufferedReader br = new InputStreamReader(isr);
String line;
while(line = br.readLine() != NUll){
System.out.println(line);
}
br.close();
isr.close();
fis.close();
}catch (FileNotFoundException e) {
}catch (UnsupportedEncodingException e) {
e.printStackTrace();
}catch (Exception e) {
}
}
}
谢谢你请我吃糖果!