我是靠谱客的博主 俊逸铃铛,这篇文章主要介绍Java 文件操作demo,现在分享给大家,希望可以做个参考。

Java 实例 - 文件写入

以下实例演示了使用 write() 方法向文件写入内容:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.example.yan.java文件写入; import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; public class Main { public static void main(String[] args) { try { BufferedWriter out = new BufferedWriter(new FileWriter("nowcoder.txt")); out.write("牛客教程"); out.close(); System.out.println("文件创建成功!"); } catch (IOException e) { } } }

在这里插入图片描述

Java 实例 - 读取文件内容

以下实例演示了使用 readLine() 方法来读取文件 test.log 内容,其中 test.log 文件内容为:

牛客教程
www.nowcoder.com

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.example.yan.java文件写入; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class Main1 { public static void main(String[] args) { try { BufferedReader in = new BufferedReader(new FileReader("C:\Users\75763\Desktop\test.log")); String str; while ((str = in.readLine()) != null) { System.out.println(str); } System.out.println(str); } catch (IOException e) { } }

在这里插入图片描述

Java 实例 - 删除文件

以下实例演示了使用 delete() 方法将文件删除:

复制代码
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
package com.example.yan.java文件写入; import java.io.File; public class Main2 { public static void main(String[] args) { try { File file = new File("C:\Users\75763\Desktop\test.log"); if (file.delete()) { System.out.println(file.getName() + " 文件已被删除!"); } else { System.out.println("文件删除失败!"); } } catch (Exception e) { e.printStackTrace(); } } }

在这里插入图片描述

Java 实例 - 将文件内容复制到另一个文件

以下实例演示了使用 BufferedWriter 类的 read 和 write 方法将文件内容复制到另一个文件:

复制代码
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 com.example.yan.java文件写入; import java.io.*; public class Main4 { public static void main(String[] args) throws Exception { BufferedWriter out1 = new BufferedWriter(new FileWriter("srcfile")); out1.write("string to be copiedn"); out1.close(); InputStream in = new FileInputStream(new File("srcfile")); OutputStream out = new FileOutputStream (new File("destnfile")); byte[] buf = new byte[1024]; int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } in.close(); out.close(); BufferedReader in1 = new BufferedReader(new FileReader("destnfile")); String str; while ((str = in1.readLine()) != null) { System.out.println(str); } in1.close(); } }

在这里插入图片描述

Java 实例 - 向文件中追加数据

以下实例演示了使用 filewriter 方法向文件中追加数据:

复制代码
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
package com.example.yan.java文件写入; import java.io.*; public class Main5 { public static void main(String[] args) throws Exception { try { BufferedWriter out = new BufferedWriter(new FileWriter("filename")); out.write("aString1n"); out.close(); out = new BufferedWriter(new FileWriter("filename",true)); out.write("aString2"); out.close(); BufferedReader in = new BufferedReader(new FileReader("filename")); String str; while ((str = in.readLine()) != null) { System.out.println(str); } in.close(); } catch (IOException e) { System.out.println("exception occoured"+ e); } } }

在这里插入图片描述

Java 实例 - 创建临时文件

以下实例演示了使用 File 类的 createTempFile(String prefix, String suffix); 方法在默认临时目录来创建临时文件,参数 prefix 为前缀,suffix 为后缀:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import java.io.*; public class Main { public static void main(String[] args) throws Exception { File temp = File.createTempFile("test", ".txt"); System.out.println("文件路径: "+temp.getAbsolutePath()); temp.deleteOnExit(); BufferedWriter out = new BufferedWriter(new FileWriter(temp)); out.write("aString"); System.out.println("临时文件已创建:"); out.close(); } }

也可以使用 createTempFile(String prefix, String suffix, File directory) 中的 directory 参数来指定临时文件的目录:

复制代码
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
import java.io.File; public class Main { public static void main(String[] args) { File f = null; try { // 创建临时文件 f = File.createTempFile("tmp", ".txt", new File("C:/")); // 输出绝对路径 System.out.println("File path: "+f.getAbsolutePath()); // 终止后删除临时文件 f.deleteOnExit(); // 创建临时文件 f = File.createTempFile("tmp", null, new File("D:/")); // 输出绝对路径 System.out.print("File path: "+f.getAbsolutePath()); // 终止后删除临时文件 f.deleteOnExit(); } catch(Exception e) { // 如果有错误输出内容 e.printStackTrace(); } } }

在创建完成后价格线程休眠,不然根本看不到创建就被删除了

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
public static void main(String[] args) throws IOException, InterruptedException { File temp = File.createTempFile("test", ".txt"); System.out.println("文件路径: "+temp.getAbsolutePath()); temp.deleteOnExit(); BufferedWriter out = new BufferedWriter(new FileWriter(temp)); out.write("aString"); out.flush(); System.out.println("临时文件已创建:"); Thread.sleep(10000); System.out.println("临时文件已删除"); out.close(); }

Java 实例 - 修改文件最后的修改日期

以下实例演示了使用 File 类的 fileToChange.lastModified() 和 fileToChange setLastModified() 方法来修改文件最后的修改日期:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import java.io.File; import java.util.Date; public class Main { public static void main(String[] args) throws Exception { File fileToChange = new File("C:/myjavafile.txt"); fileToChange.createNewFile(); Date filetime = new Date(fileToChange.lastModified()); System.out.println(filetime.toString()); System.out.println(fileToChange.setLastModified(System.currentTimeMillis())); filetime = new Date(fileToChange.lastModified()); System.out.println(filetime.toString()); } }

以上代码运行输出结果为:

复制代码
1
2
3
4
Sat Mar 21 22:00:48 CST 2015 true Fri Apr 10 11:09:19 CST 2015

Java 实例 - 获取文件大小

以下实例演示了使用 File 类的 file.exists() 和 file.length() 方法来获取文件大小,以字节计算(1KB=1024字节 ):

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import java.io.File; public class Main { public static long getFileSize(String filename) { File file = new File(filename); if (!file.exists() || !file.isFile()) { System.out.println("文件不存在"); return -1; } return file.length(); } public static void main(String[] args) { long size = getFileSize("c:/java.txt"); System.out.println("java.txt文件大小为: " + size); } }

以上代码运行输出结果为(java.txt 文件位于 C 盘):

复制代码
1
2
java.txt文件大小为: 480

Java 实例 - 文件重命名

以下实例演示了使用 File 类的 oldName.renameTo(newName) 方法来重命名文件:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import java.io.File; public class Main { public static void main(String[] args) { File oldName = new File("C:/program.txt"); File newName = new File("C:/java.txt"); if(oldName.renameTo(newName)) { System.out.println("已重命名"); } else { System.out.println("Error"); } } }

以上代码运行输出结果为(执行该程序前你可以先创建 program.txt 文件):

复制代码
1
2
已重命名

Java 实例 - 设置文件只读

以下实例演示了使用 File 类的 file.setReadOnly() 和 file.canWrite() 方法来设置文件只读:

复制代码
1
2
3
4
5
6
7
8
9
10
import java.io.File; public class Main { public static void main(String[] args) { File file = new File("C:/java.txt"); System.out.println(file.setReadOnly()); System.out.println(file.canWrite()); } }

以上代码运行输出结果为:

复制代码
1
2
3
true false

Java 实例 - 检测文件是否存在

以下实例演示了使用 File 类的 file.exists() 方法来检测文件是否存在:

复制代码
1
2
3
4
5
6
7
8
9
import java.io.File; public class Main { public static void main(String[] args) { File file = new File("C:/java.txt"); System.out.println(file.exists()); } }

以上代码运行输出结果为(如果你的 C 盘中存在文件 java.txt):

复制代码
1
2
true

Java 实例 - 在指定目录中创建文件

以下实例演示了使用 File 类的 file.createTempFile() 方法在指定目录中创建文件:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package com.example.yan.java文件写入; import java.io.File; public class Main7 { public static void main(String[] args) throws Exception { File file = null; File dir = new File("D://"); file = File.createTempFile ("JavaTemp", ".javatemp", dir); System.out.println(file.getPath()); } }

在这里插入图片描述

复制代码
1
2
Java 实例 - 获取文件修改时间

以下实例演示了使用 File 类的 file.lastModified() 方法来获取文件最后的修改时间​

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
package com.example.yan.java文件写入; import java.io.File; import java.util.Date; public class Main8 { public static void main(String[] args) { File file = new File("Main.java"); Long lastModified = file.lastModified(); Date date = new Date(lastModified); System.out.println(date); } }

在这里插入图片描述

Java 实例 - 创建文件

以下实例演示了使用 File 类的 File() 构造函数和 file.createNewFile() 方法来创建一个新的文件

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import java.io.File; import java.io.IOException; public class Main { public static void main(String[] args) { try{ File file = new File("C:/myfile.txt"); if(file.createNewFile()) System.out.println("文件创建成功!"); else System.out.println("出错了,该文件已经存在。"); } catch(IOException ioe) { ioe.printStackTrace(); } } }

以上代码运行输出结果为:

复制代码
1
2
文件创建成功!

Java 实例 - 文件路径比较

以下实例演示了使用 File 类的 filename.compareTo (another filename) 方法来比较两个文件路径是否在同一个目录下:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import java.io.File; public class Main { public static void main(String[] args) { File file1 = new File("C:/File/demo1.txt"); File file2 = new File("C:/java/demo1.txt"); if(file1.compareTo(file2) == 0) { System.out.println("文件路径一致!"); } else { System.out.println("文件路径不一致!"); } } }

以上代码运行输出结果为:

复制代码
1
2
文件路径不一致!

最后

以上就是俊逸铃铛最近收集整理的关于Java 文件操作demo的全部内容,更多相关Java内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(104)

评论列表共有 0 条评论

立即
投稿
返回
顶部