Java中操作檔案主要使用io包中的輸入輸出流,我這裡要說的是操作普通檔案,如jpg、pdf等位元組流檔案和txt等字元流檔案。XML檔案、Excel和屬性檔案有專門的工具去操作,這裡就不再贅述。
Java中io流可分為位元組流和字元流兩類。位元組流主要用來操作位元組類型的檔案,如jpg、pdf、rar等,字元流主要用來操作字元類型的檔案,如txt、html、jsp等等。根據流的方向又可分為輸入流和輸出流,輸入流是指從檔案中将内容讀到記憶體中的流,輸出流是指将記憶體中的内容寫到檔案中的流。
讀取位元組檔案主要使用FileInputStream類。輸出位元組檔案主要使用FileOutputStream,它有幾個重載的構造方法。在這些構造方法中,首先要傳入的就是要輸出的檔案對象或者問價路徑,其次是一個Boolean型的append參數,這個參數如果為true,表示将内容追加到檔案的後邊。如果為false表示用内容替換檔案中的字元。
讀取字元檔案主要使用FileInputStream。由于每個字元檔案的編碼可能不同,是以使用InputStreamReader将前邊的FileInputStream按照指定字元集包裝一下,在InputStreamReader的構造方法中會有一個參數來指定字元集。為了提高讀取的效率,用BufferedReader将前邊的InputStreamReader包轉一下,這樣一次就可以讀入一行,否則一次隻能讀取一個字元。寫入字元檔案主要使用FileOutputStream,可以指定是追加到檔案中,還是替換檔案原有的内容。用OutputStreamWriter将FileOutputStream包裝一下,這樣就可以指定輸出的字元集。為了提高寫入的效率,使用BufferedWriter将OutputStreamWriter包裝起來。
具體代碼如下所示:
package com.test.file;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
public class FileOperation {
private static final int SIZE = 1024;
public static void mergeByteFile(String srcFilePath, String targetFilePath) throws IOException {
File srcFile = new File(srcFilePath);
File targetFile = new File(targetFilePath);
mergeByteFile(srcFile, targetFile);
}
public static void mergeByteFile(File srcFile, File targetFile) throws IOException {
byte[] buff = new byte[SIZE];
FileInputStream in = null;
FileOutputStream out = null;
try {
in = new FileInputStream(srcFile);
out = new FileOutputStream(targetFile, true);
// 為了提高效率,将内容讀到byte數組中
int data = in.read(buff);
while (data != -1) {
out.write(buff);
data = in.read(buff);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
}
}
public static void readCharacterFile(String filePath) throws IOException {
File file = new File(filePath);
readCharacterFile(file);
}
public static void readCharacterFile(File file) throws IOException {
InputStream inputStream = null;
try {
inputStream = new FileInputStream(file);
// 以指定的編碼格式讀入檔案
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
// 将其包裝為BufferedReader類,以便可以一次讀入一行
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
// 讀取一行
String content = bufferedReader.readLine();
// while (bufferedReader.ready()) {
while (content != null) {
System.out.println(content);
content = bufferedReader.readLine();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} finally {
if (inputStream != null) {
inputStream.close();
}
}
}
public static void mergeCharacterFile(String srcFilePath, String targetFilePath) throws IOException {
File srcFile = new File(srcFilePath);
File targetFile = new File(targetFilePath);
mergeCharacterFile(srcFile, targetFile);
}
public static void mergeCharacterFile(File srcFile, File targetFile) throws IOException {
InputStream inputStream = null;
OutputStream outputStream = null;
StringBuffer stringBuffer = new StringBuffer("\r\n");
try {
// 以srcFile為源構造檔案輸入流
inputStream = new FileInputStream(srcFile);
// 以指定的編碼格式讀入檔案
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
// 将其包裝為BufferedReader類,以便可以一次讀入一行
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String content = bufferedReader.readLine();
// 以targetFile為目标構造檔案輸出流
outputStream = new FileOutputStream(targetFile, true);
// 以指定的編碼格式輸出
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream, "UTF-8");
// 構造緩存輸出流
BufferedWriter bufferedWrite = new BufferedWriter(outputStreamWriter);
// 内容不為null就一直讀,将讀出的内容放在StringBuffer中
while (content != null) {
// 在讀出的每行的後邊都加上換行符
stringBuffer.append(content + "\r\n");
content = bufferedReader.readLine();
}
// 将讀出的内容一次性寫入到目标檔案中
bufferedWrite.write(stringBuffer.toString());
bufferedWrite.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} finally {
if (inputStream != null) {
inputStream.close();
}
if (outputStream != null) {
outputStream.close();
}
}
}
public static void main(String[] args) {
File srcFile = new File("H:/ioTest/Ajax-Web2.pdf");
File targetFile = new File("H:/ioTest/Ajax.pdf");
try {
mergeByteFile(srcFile, targetFile);
} catch (IOException e) {
e.printStackTrace();
}
try {
readCharacterFile("E:/Workspace/MyEclipse6/tms/WebRoot/error.jsp");
mergeCharacterFile("H:/ioTest/route.txt", "H:/ioTest/study.txt");
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("end......");
}
}