Java:文件写入读取操作和工具类
【摘要】 文件读写示例
package com.demo;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class FileUtil { public static void main(String[] args) ...
文件读写示例
package com.demo;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class FileUtil { public static void main(String[] args) throws IOException { // 覆盖写入 PrintWriter writer = new PrintWriter(new FileWriter("data.txt")); writer.println("hello"); writer.println("world"); writer.close(); // 追加写入 PrintWriter appendWriter = new PrintWriter(new FileWriter("data.txt", true)); appendWriter.println("hello"); appendWriter.println("world"); appendWriter.close(); // 按行读取 List<String> list = new ArrayList<>(); Scanner scanner = new Scanner(new FileReader("data.txt")); while (scanner.hasNextLine()) { String line = scanner.nextLine(); list.add(line); } scanner.close(); System.out.println(list); // [hello, world, hello, world] }
}
- 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
工具类封装
写入
package com.demo.fileutil;
import java.io.IOException;
import java.io.PrintWriter;
public class FileWriter { private PrintWriter writer; public FileWriter(String fileName) throws IOException { this(fileName, false); } public FileWriter(String fileName, boolean append) throws IOException { this.writer = new PrintWriter(new java.io.FileWriter(fileName, append)); } public void println(String content){ this.writer.println(content); } public void print(String content){ this.writer.print(content); } public void close(){ this.writer.close(); }
}
- 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.demo.fileutil;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class FileReader { private String fileName; public FileReader(String fileName) { this.fileName = fileName; } public List<String> readLines() throws FileNotFoundException { List<String> list = new ArrayList<>(); Scanner scanner = new Scanner(new java.io.FileReader(this.fileName)); while (scanner.hasNextLine()) { String line = scanner.nextLine(); list.add(line); } scanner.close(); return list; } public String readAll() throws IOException { StringBuilder sb = new StringBuilder(); java.io.FileReader reader = new java.io.FileReader(this.fileName); int len = 0; char[] data = new char[1024]; while ((len = reader.read(data)) != -1) { sb.append(data, 0, len); } reader.close(); return sb.toString(); }
}
- 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
测试
package com.demo;
import com.demo.fileutil.FileReader;
import com.demo.fileutil.FileWriter;
import java.io.IOException;
import java.util.List;
public class TestFileUtil { public static void main(String[] args) throws IOException { FileWriter writer = new FileWriter("data.txt", true); writer.println("hello"); writer.println("world"); writer.close(); FileReader reader = new FileReader("data.txt"); String content = reader.readAll(); System.out.print(content); // hello // world List<String> list = reader.readLines(); System.out.println(list); // [hello, world] }
}
- 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
文章来源: pengshiyu.blog.csdn.net,作者:彭世瑜,版权归原作者所有,如需转载,请联系作者。
原文链接:pengshiyu.blog.csdn.net/article/details/108572624
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)