目录
- 代码由来
- 代码
- Java Files(Since Java7)
- JDK1.6以前的实现
- JDK1.7以后的实现
代码由来
程序员写代码读写文件是最基本的操作。尤其是读写byte[],因为所有类型的文件归根结底都是byte[]。但是很多人写的代码往往只是简单的实现功能,既不考虑代码执行效率,也不考虑代码的美观程度。所以我写下这篇博文记录下到目前为止我所知道的最美观简洁高效的读取文件的代码,分享出来供新人参考。
如果这篇博客帮助到了你,你可给它点个赞,这将是对我莫大的鼓励,谢谢!
代码
Java Files(Since Java7)
摘抄自廖雪峰老师的Java教程
// 读取、写入字节数组
byte[] data = Files.readAllBytes(Paths.get("/path/to/"));
Files.write(Paths.get("/path/to/"), data);
// 读取、写入字符 Since:Java11
// 默认使用UTF-8编码读取:
String content1 = Files.readString(Paths.get("/path/to/"));
// 可指定编码:
String content2 = Files.readString(Paths.get("/path/to/"), StandardCharsets.ISO_8859_1);
// 按行读取并返回每行内容:
List<String> lines = Files.readAllLines(Paths.get("/path/to/"));
// 写入文本并指定编码: Since:Java11
Files.writeString(Paths.get("/path/to/"), "文本内容...", StandardCharsets.ISO_8859_1);
// 按行写入文本:
Files.write(Paths.get("/path/to/"), lines);
JDK1.6以前的实现
/**
* 以byte[]方式读取文件
*
* @param fileName 文件名
* @return 文件字节数组
* @throws IOException
*/
public static byte[] readFileByBytes(String fileName) throws IOException {
InputStream in = null;
ByteArrayOutputStream out = null;
try {
in = new BufferedInputStream(new FileInputStream(fileName));
out = new ByteArrayOutputStream();
byte[] tempbytes = new byte[in.available()];
for (int i = 0; (i = in.read(tempbytes)) != -1;) {
out.write(tempbytes, 0, i);
}
} finally {
if (in != null) {
in.close();
}
}
return out.toByteArray();
}
/**
* 向文件写入byte[]
*
* @param fileName 文件名
* @param bytes 字节
* @param append 是否追加
* @throws IOException
*/
public static void writeFileByBytes(String fileName, byte[] bytes, boolean append) throws IOException {
OutputStream out = null;
try {
out = new BufferedOutputStream(new FileOutputStream(fileName, append));
out.write(bytes);
} finally {
if (out != null) {
out.close();
}
}
}
/**
* 从文件开头向文件写入byte[]
*
* @param fileName 文件名
* @param bytes 字节
* @throws IOException
*/
public static void writeFileByBytes(String fileName, byte[] bytes) throws IOException {
writeFileByBytes(fileName, bytes, false);
}
JDK1.7以后的实现
/**
* 以byte[]方式读取文件
*
* @param fileName 文件名
* @return 文件字节数组
* @throws IOException
*/
public static byte[] readFileByBytes(String fileName) throws IOException {
try (InputStream in = new BufferedInputStream(new FileInputStream(fileName));
ByteArrayOutputStream out = new ByteArrayOutputStream();) {
byte[] tempbytes = new byte[in.available()];
for (int i = 0; (i = in.read(tempbytes)) != -1;) {
out.write(tempbytes, 0, i);
}
return out.toByteArray();
}
}
/**
* 向文件写入byte[]
*
* @param fileName 文件名
* @param bytes 字节内容
* @param append 是否追加
* @throws IOException
*/
public static void writeFileByBytes(String fileName, byte[] bytes, boolean append) throws IOException {
try(OutputStream out = new BufferedOutputStream(new FileOutputStream(fileName, append))){
out.write(bytes);
}
}
/**
* 从文件开头向文件写入byte[]
*
* @param fileName 文件名
* @param bytes 字节
* @throws IOException
*/
public static void writeFileByBytes(String fileName, byte[] bytes) throws IOException {
writeFileByBytes(fileName, bytes, false);
}