文章目录
-
-
-
-
- 遍历所有文件
- 读写文件内容
- 压缩文件
- 解压文件
-
-
-
遍历所有文件
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
public class Demo {
public static List<String> fileList = new ArrayList<>(); // 文件标准路径的List集合
/**
* 遍历目录下所有文件
* @param dirpath
* @throws IOException
*/
public static void traverseFiles(String dirpath) throws IOException {
File dirFile = new File(dirpath);
if (!dirFile.exists()) {
return;
}
if (!dirFile.isDirectory()) {
fileList.add(dirFile.getCanonicalPath());
return;
}
String[] fileNames = dirFile.list();
for (String string : fileNames) {
File file = new File(dirFile.getCanonicalPath(), string);
if (file.isDirectory()) {
traverseFiles(file.getCanonicalPath());
} else {
fileList.add(file.getCanonicalPath());
}
}
}
/**
* 遍历Zip压缩包中所有文件
* @param dirpath
* @throws IOException
*/
public static void traverseZipFiles(String dirpath) throws IOException {
ZipInputStream zin = new ZipInputStream(new BufferedInputStream(new FileInputStream(new File(zipath))));
ZipEntry zipEntry;
while ((zipEntry = zin.getNextEntry()) != null) {
if (zipEntry.isDirectory()) {
} else {
fileList.add(zipEntry.getName());
}
}
zin.close();
}
}
读写文件内容
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
public class Demo {
/**
* 以字节为单位读写文件,常用于对二进制文件的操作
*
* @param rfilePath
* @param wfilePath
* @throws IOException
*/
public static void rwFileByBytes(String rfilePath, String wfilePath) throws IOException {
File rFile = new File(rfilePath);
if (!rFile.exists()) {
return;
}
File wFile = new File(wfilePath);
// 一次读写一个字节
BufferedInputStream bin = new BufferedInputStream(new FileInputStream(rFile));
BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(wFile));
int nextByte;
while ((nextByte = bin.read()) != -1) {
bout.write(nextByte);
}
bin.close();
bout.close();
// 一次读写多个字节,自建直接缓冲区
InputStream in = new FileInputStream(rFile);
OutputStream out = new FileOutputStream(wFile);
byte[] temp = new byte[1024];
int totalNum;
while((totalNum = in.read(temp)) != -1) {
out.write(temp, 0, totalNum);
}
in.close();
out.close();
}
/**
* 以字符为单位读写单位, 常用于对文本文件的操作
*
* @param rfilePath
* @param wfilePath
* @throws IOException
*/
public static void rwFileByChars(String rfilePath, String wfilePath) throws IOException {
File rFile = new File(rfilePath);
if (!rFile.exists()) {
return;
}
File wFile = new File(wfilePath);
// 一次读写一个字符
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(rFile)));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(wFile)));
int charRead;
while((charRead = br.read()) != -1) {
// 对于windows下,\r\n这两个字符在一起时,表示一个换行。但如果这两个字符分开显示时,会换两次行。因此,屏蔽掉\r,或者屏蔽\n。否则,将会多出很多空行。
if (((char) charRead) != '\r') {
bw.write(charRead);
}
}
br.close();
bw.close();
// 一次读写多个字符, 自建直接缓冲区
InputStreamReader reader = new InputStreamReader(new FileInputStream(rFile));
OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(wFile));
char[] temp = new char[256];
int totalNum;
while ((totalNum = reader.read(temp)) != -1) {
if ((totalNum == temp.length) && (temp[temp.length - 1] != '\r')) {
writer.write(temp, 0, totalNum);
} else {
for (int i = 0; i < totalNum; i++) {
if (temp[i] == '\r') {
continue;
} else {
writer.write(temp[i]);
}
}
}
}
reader.close();
writer.close();
}
/**
* 以行为单位读写文件, 常用于读写面向行的格式化文件
*
* @param rfilePath
* @param wfilePath
* @throws IOException
*/
public static void rwFileByLines(String rfilePath, String wfilePath) throws IOException {
File rFile = new File(rfilePath);
if (!rFile.exists()) {
return;
}
File wFile = new File(wfilePath);
BufferedReader reader = new BufferedReader(new FileReader(rFile));
BufferedWriter writer = new BufferedWriter(new FileWriter(wFile));
// BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(rFile), "gbk"));
// BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(wFile), "gbk"));
String line = null;
while ((line = reader.readLine()) != null) {
writer.write(line);
}
reader.close();
writer.close();
}
}
压缩文件
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class Demo {
/**
* 压缩方法
*
* @param file 要压缩的文件
* @param zos zip输出流
* @param bos 输出流
* @param baseDir 要压缩文件所在文件夹
* @throws IOException
*/
public static void compress(File file, ZipOutputStream zos, BufferedOutputStream bos, String baseDir) throws IOException {
BufferedInputStream bis = null;
try {
if (!file.exists()) {
return;
}
if (file.isFile()) {
// 保证压缩层次
String entryPath = file.getCanonicalPath().replace(baseDir, "");
// 创建一个要压缩文件的文件名的条目
ZipEntry entry = new ZipEntry(entryPath);
// 将此条目放入创建好的压缩包里
zos.putNextEntry(entry);
byte[] buffer = new byte[4096];
bis = new BufferedInputStream(new FileInputStream(file));
int len;
while ((len = bis.read(buffer)) > 0) {
bos.write(buffer, 0, len);
bos.flush();
}
} else {
File[] files = file.listFiles();
for (File f : files) {
compress(f, zos, bos, baseDir);
}
}
} finally {
if (null != bis) {
bis.close();
}
}
}
/**
* 创建zip压缩文件
*
* @param srcPath 要压缩文件所在文件夹
* @param desPath 生成的压缩文件
* @return
*/
public static File createZipFile(String srcPath, String desPath) {
File file = new File(srcPath);
if (!file.exists()) {
return file;
}
File zipFile = new File(desPath);
ZipOutputStream zos = null;
BufferedOutputStream bos = null;
try {
zos = new ZipOutputStream(new FileOutputStream(zipFile));
bos = new BufferedOutputStream(zos);
compress(file, zos, bos, srcPath);
} catch (FileNotFoundException e) {
zipFile = null;
e.printStackTrace();
} catch (IOException e) {
zipFile = null;
e.printStackTrace();
} finally {
if (null != zos) {
try {
zos.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return zipFile;
}
}
解压文件
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
public class Demo {
/**
* 解压zip压缩包
*
* @param srcPath 压缩包路径
* @param desPath 解压路径
*/
public static void unZip(String srcPath, String desPath) {
File srcFile = new File(srcPath);
if (!srcFile.exists()) {
return;
}
InputStream ins = null;
FileOutputStream fos = null;
try {
ZipFile zipFile = new ZipFile(srcFile);
Enumeration<?> entries = zipFile.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = (ZipEntry) entries.nextElement();
String path = desPath + File.separator + entry.getName();
File file = new File(path);
if (entry.isDirectory()) {
file.mkdirs();
} else {
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
file.createNewFile();
ins = zipFile.getInputStream(entry);
fos = new FileOutputStream(file);
int len;
byte[] buffer = new byte[1024];
while ((len = ins.read(buffer)) != -1) {
fos.write(buffer, 0, len);
}
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (null != fos) {
fos.close();
}
if (null != ins) {
ins.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
一篇关于Java的IO性能的文章