File类简介
?
|
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
|
package com.file;
import java.io.File;
import java.io.IOException;
/**
* Created by elijahliu on 2017/2/10.
*/
public class filetest {
public static void main(String[] args) {
File file = new File("hello.txt");
//是否存在
if (file.exists()) {
//文件
System.out.println(file.isFile());
//路径(文件夹)
System.out.println(file.isDirectory());
File nameto = new File("new Hello.txt");
file.renameTo(nameto);//这里就是重命名文件的操作,直接新建一个file对象然后使用renameTo方法可以重命名文件
} else {
System.out.println("文件不存在");
try {
file.createNewFile();
System.out.println("文件已被创建");
} catch (IOException e) {
System.out.println("文件无法创建");
}
}
if (file.exists()) {
//删除文件
file.delete();
System.out.println("删除文件");
} else {
}
}
}
|
文件夹操作
?
|
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
|
package com.file;
import java.io.File;
/**
* Created by elijahliu on 2017/2/11.
*/
public class HelloFolder {
public static void main(String[] args) {
File folder = new File("my new folder");
if (folder.mkdir()) {//创建文件夹 判断是否成功
System.out.println("文件夹创建完成");
File newfolder = new File("myn new foleder - new");
folder.renameTo(newfolder);//这里重命名了文件夹 文件夹的重命名是可以单独更改一级的文件夹名的 而这一级下面的文件夹不变 保存目录结构
if (folder.delete()) {
System.out.print("done");//这里的删除只能删除空文件夹,如果文件夹中有东西,那么则不能删除,不问三七二十一直接删除一个非空文件夹是非常不负责任的
} else {
System.out.println("fail");
}
}else{
if (folder.exists()) {
System.out.println("文件夹已经存在不用创建");
}else{
System.out.println("文件夹创建失败");
}
}
File folders = new File("my new folder/one/two/three/main");
folders.mkdirs();//在java中用mkdir只能创建一个,mkdirs可以创建多级目录
}
}
|
文件属性设置
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
package com.file;
import java.io.File;
/**
* Created by elijahliu on 2017/2/11.
*/
public class SetFileProperty {
public static void main(String[] args){
File file = new File("test.file");
if (file.exists()){
file.setWritable(true);//可写
file.setReadable(true);//可读
file.setReadOnly();//只读
}
}
}
|
遍历文件夹
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
public void printFiles(File dir,int tab) {//tab为不同目录结构的缩进量
if (dir.isDirectory()) {
File next[] = dir.listFiles();//判断如果是目录 则返回目录所有的文件名数组用于遍历文件夹
for (int i = 0;i<next.length;i++) {//层次缩进输出
System.out.print("---");
}
for(int i = 0;i<next.length;i++) {//这里用了递归获取目录结构
System.out.println(next[i].getName());
if (next[i].isFile()) {
printFiles(next[i],++tab);
}
}
}
}
|
文件简单读写
?
|
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
42
43
44
45
46
47
48
49
50
51
52
53
|
package com.file;
import java.io.*;
/**
* Created by elijahliu on 2017/2/11.
*/
public class ReadFile {
public static void main(String[] args) {
File file = new File("new Hello.txt");
if(file.exists()){
System.err.print("exsit");
try (FileInputStream fis = new FileInputStream(file)) {//文件输入流 这是字节流
InputStreamReader isr = new InputStreamReader(fis,"UTF-8");//inputstreamReader是一个字节流,将字节流和字符流转化的时候,就需要制定一个编码方式,不然就会乱码
BufferedReader br = new BufferedReader(isr);//字符缓冲区
String line;
while((line = br.readLine())!=null){//这里将缓冲区里的内容如果非空就读出来打印
System.out.println(line);
}
br.close();//最后将各个线程关闭
isr.close();
fis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
File newfile = new File("newtext.txt");
try {
FileOutputStream fos = new FileOutputStream(newfile);//这里如果文件不存在会自动创建文件
OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");//和读取一样这里是转化的是字节和字符流
BufferedWriter bw = new BufferedWriter(osw);//这里是写入缓冲区
bw.write("厉害了我的哥");//写入字符串
bw.close();//和上面一样 这里后打开的先关闭 先打开的后关闭
osw.close();
fos.close();
System.out.println("done");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。
原文链接:http://www.jianshu.com/p/5770760f83c1#
相关文章
猜你喜欢
- ASP.NET本地开发时常见的配置错误及解决方法? 2025-06-10
- ASP.NET自助建站系统的数据库备份与恢复操作指南 2025-06-10
- 个人网站服务器域名解析设置指南:从购买到绑定全流程 2025-06-10
- 个人网站搭建:如何挑选具有弹性扩展能力的服务器? 2025-06-10
- 个人服务器网站搭建:如何选择适合自己的建站程序或框架? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-27 50
-
Java通过调用C/C++实现的DLL动态库——JNI的方法
2025-05-27 36 -
2025-05-27 28
-
2025-05-25 24
-
2025-05-29 46
热门评论

