文章目錄
-
-
-
-
- 周遊所有檔案
- 讀寫檔案内容
- 壓縮檔案
- 解壓檔案
-
-
-
周遊所有檔案
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性能的文章