* @version 1.0
* @author 韓衛召
*
* 多種方式讀文件的內容
* 按字節讀取文件內容,按字符讀取文件的內容,按行讀取文件的內容,隨即讀取文件的內容
*/
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.io.Reader;
import java.util.ResourceBundle;
public class ReadFromFile {
/**
* 以字節為單位讀取文件的內容,常用於二進制文件,如聲音,圖象,影象等文件
*
* @param filename
* 文件名
*/
public static void readFileByBytes(java.lang.String filename) {
File file = new File(filename);
InputStream in = null;
System.out.println("以字節為單位讀取文件的內容,壹次讀壹個字節: ");
// 壹次讀壹個字節
try {
in = new FileInputStream(file);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int tempbyte;
try {
// 不斷的讀取,直到文件結束
while ((tempbyte = in.read()) != -1) {
System.out.write(tempbyte);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return;
}
System.out.println("以字節為單位讀取文件內容,壹次讀多個字節: ");
// 壹次讀取多個字節
byte[] tempbytes = new byte[100];
int byteread = 0;
try {
in = new FileInputStream(filename);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ReadFromFile.showAvailabelBytes(in);
try {
while ((byteread = in.read(tempbytes)) != -1) {
// 讀取多個字節到數組中,byteead為壹次讀取的字節數
System.out.write(tempbytes, 0, byteread);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void readFileByteChars(java.lang.String filename) {
/**
* 以字符為單位讀取文件,常用與讀文本,數字等類型的文件
*/
File file = new File(filename);
Reader reader = null;
System.out.println("以字符為單位讀取文件內容,壹次讀壹個字節: ");
// 壹次讀壹個字符
try {
reader = new InputStreamReader(new FileInputStream(file));
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
int tempchar;
try {
while ((tempchar = reader.read()) != -1) {
// 在Window下,\r\n這兩個字符在壹起時,表示壹個換行
// 但如果這兩個字符分開顯示時,會換行兩次行
// 因此,屏蔽掉\r,或者\n;否則,將會多出來很多空行
if (((char) tempchar) != '\r') {
System.out.println((char) tempchar);
}
}
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("以字符為單位讀取文件內容,壹次讀多個字符: ");
char[] tempchars = new char[30];
int charread = 0;
try {
reader = new InputStreamReader(new FileInputStream(filename));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
// 讀入多個字符到字符數組中,charread為壹次讀取字符數
while ((charread = reader.read(tempchars)) != -1) {
if ((charread == tempchars.length)
&& (tempchars[tempchars.length - 1] != '\r')) {
System.out.println(tempchars);
} else {
for (int i = 0; i < charread; i++) {
if (tempchars[i] == '\r') {
continue;
} else {
System.out.print(tempchars[i]);
}
}
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
}
}
}
}
public static void readFileByLines(java.lang.String filename) {
File file = new File(filename);
BufferedReader reader = null;
// System.out.println("以行為單位讀取文件的內容,壹次讀壹整行: ");
try {
reader = new BufferedReader(new FileReader(file));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
java.lang.String tempString = null;
int line = 1;
try {
while ((tempString = reader.readLine()) != null) {
if(tempString.indexOf("福州")!=-1){
System.out.println("靠!出錯了!趕緊報警啊! " + line + ": " + tempString);
}
// System.out.println("line " + line + ": " + tempString);
line++;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public static void readFileByRandomAccess(java.lang.String filename) {
RandomAccessFile randomFile = null;
System.out.println("隨即讀取壹段文件內容: ");
try {
randomFile = new RandomAccessFile(filename, "r");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
long fileLength = 0;
try {
fileLength = randomFile.length();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int beginIndex = (fileLength > 4) ? 4 : 0;
try {
randomFile.seek(beginIndex);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
byte[] bytes = new byte[10];
int byteread = 0;
try {
while ((byteread = randomFile.read(bytes)) != -1) {
System.out.write(bytes, 0, byteread);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (randomFile != null) {
try {
randomFile.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
private static void showAvailabelBytes(InputStream in) {
try {
System.out.println("當前字節輸入流中的字節數為: " + in.available());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(java.lang.String args[]) {
ResourceBundle rb = ResourceBundle.getBundle("PreventTampering");
String filePath = rb.getString("filePath");
java.lang.String filename = filePath+"test.txt";
System.out.println(filename);
// ReadFromFile.readFileByBytes(filename);
// ReadFromFile.readFileByteChars(filename);
ReadFromFile.readFileByLines(filename);
// ReadFromFile.readFileByRandomAccess(filename);
}