/**
*以字節讀取文件,常用於讀取二進制文件,如圖片、聲音、圖像等文件。
*/
public static void readFileByBytes(字符串文件名){
File file =新文件(文件名);
InputStream in = null
嘗試{
System.out.println("以字節為單位讀取文件內容,壹次壹個字節:");
//壹次讀取壹個字節
in = new FileInputStream(文件);
int tempbyte
while ((tempbyte = in.read())!= -1) {
system . out . write(tempbyte);
}
in . close();
} catch (IOException e) {
e . printstacktrace();
返回;
}
嘗試{
System.out.println("以字節為單位讀取文件內容,壹次讀取多個字節:");
//壹次讀取多個字節
byte[] tempbytes =新字節[100];
int byteread = 0;
in =新文件輸入流(文件名);
readfromfile . showavailablebytes(in);
//將多個字節讀入壹個字節數組,其中byteread是壹次讀取的字節數。
while((byteread = in . read(temp bytes))!= -1) {
System.out.write(tempbytes,0,byteread);
}
} catch(異常e1) {
e 1 . printstacktrace();
}最後{
如果(在!= null) {
嘗試{
in . close();
} catch (IOException e1) {
}
}
}
}
/**
*用字符讀取文件,常用於讀取文本、數字等類型的文件。
*/
public static void readFileByChars(字符串文件名){
File file =新文件(文件名);
Reader reader = null
嘗試{
System.out.println("以字符為單位讀取文件內容,壹次讀取壹個字節:");
//壹次讀取壹個字符
reader = new InputStreamReader(new file inputstream(file));
int tempchar
while ((tempchar = reader.read())!= -1) {
//對於windows,\ r \當這兩個字符在壹起時,它們表示壹個新行。
//但是這兩個字符分開顯示的話,就要換兩次線。
//因此,mask \r或mask \ n..否則會有很多空行。
if (((char) tempchar)!= '\r') {
system . out . print((char)temp char);
}
}
reader . close();
} catch(異常e) {
e . printstacktrace();
}
嘗試{
System.out.println("以字符為單位讀取文件內容,壹次讀取多個字節:");
//壹次讀取多個字符
char[]tempchars = new char[30];
int char read = 0;
reader = new InputStreamReader(new file inputstream(文件名));
//將多個字符讀入字符數組,其中charread是壹次讀取的字符數。
while((charread = reader . read(tempchars))!= -1) {
//同時阻止\r不顯示。
if ((charread == tempchars.length)
& amp& amp(tempchars[tempchars . length-1]!= ' \ r '){
system . out . print(tempchars);
}否則{
for(int I = 0;我& ltcharreadi++) {
if (tempchars[i] == '\r') {
繼續;
}否則{
system . out . print(tempchars[I]);
}
}
}
}
} catch(異常e1) {
e 1 . printstacktrace();
}最後{
如果(讀者!= null) {
嘗試{
reader . close();
} catch (IOException e1) {
}
}
}
}
/**
*以行為單位讀取文件,這通常用於讀取面向行的格式化文件。
*/
public static void readfile bylines(字符串文件名){
File file =新文件(文件名);
BufferedReader reader = null
嘗試{
System.out.println("以行為單位讀取文件內容,壹次壹整行:");
reader = new buffered reader(new file reader(file));
String tempString = null
int line = 1;
//壹次讀取壹行,直到讀取null是文件的結尾。
while((tempString = reader . readline())!= null) {
//顯示行號
system . out . println(" line "+line+":"+tempString);
line++;
}
reader . close();
} catch (IOException e) {
e . printstacktrace();
}最後{
如果(讀者!= null) {
嘗試{
reader . close();
} catch (IOException e1) {
}
}
}
}
/**
*隨機讀取文件內容
*/
public static void readFileByRandomAccess(字符串文件名){
random access file random file = null;
嘗試{
System.out.println("隨機讀取文件:");
//以只讀方式打開隨機訪問文件流。
randomFile = new RandomAccessFile(文件名,“r”);
//文件長度,字節數
long file length = random file . length();
//讀取文件的起始位置
int begin index =(file length & gt;4) ?4 : 0;
//將讀取文件的起始位置移動到beginIndex位置。
random file . seek(begin index);
byte[] bytes =新字節[10];
int byteread = 0;
//壹次讀取10字節。如果文件內容少於10字節,則讀取剩余的字節。
//將壹次讀取的字節數賦給byteread。
while((byteread = random file . read(bytes))!= -1) {
System.out.write(bytes,0,byteread);
}
} catch (IOException e) {
e . printstacktrace();
}最後{
if (randomFile!= null) {
嘗試{
random file . close();
} catch (IOException e1) {
}
}
}
}
/**
*顯示輸入流中剩余的字節數。
*/
私有靜態void showAvailableBytes(InputStream in){
嘗試{
System.out.println("當前字節輸入流中的字節數為:"+in . available());
} catch (IOException e) {
e . printstacktrace();
}
}
公共靜態void main(String[] args) {
string fileName = " C:/temp/new temp . txt ";
ReadFromFile.readFileByBytes(文件名);
ReadFromFile.readFileByChars(文件名);
ReadFromFile.readFileByLines(文件名);
readfromfile . readfilebyrandomaccess(文件名);
}
}
復制代碼
5.將內容追加到文件的末尾。
公共類AppendToFile {
/**
*壹種追加文件的方法:使用RandomAccessFile。
*/
public static void appendMethodA(字符串文件名,字符串內容){
嘗試{
//打開壹個隨機訪問文件流,讀寫。
random access file random file = new random access file(文件名,“rw”);
//文件長度,字節數
long file length = random file . length();
//將寫文件指針移動到文件末尾。
randomFile.seek(文件長度);
randomFile.writeBytes(內容);
random file . close();
} catch (IOException e) {
e . printstacktrace();
}
}
/**
* B追加文件的方法:使用FileWriter。
*/
public static void appendMethodB(字符串文件名,字符串內容){
嘗試{
//打開壹個文件寫入器,構造函數中的第二個參數true表示文件是作為append寫入的。
FileWriter writer = new FileWriter(fileName,true);
writer.write(內容);
writer . close();
} catch (IOException e) {
e . printstacktrace();
}
}
公共靜態void main(String[] args) {
string fileName = " C:/temp/new temp . txt ";
String content = "new append!";
//按照方法a添加文件。
AppendToFile.appendMethodA(文件名,內容);
AppendToFile.appendMethodA(文件名,“追加結束。\ n ");
//顯示文件內容
ReadFromFile.readFileByLines(文件名);
//按照方法b添加文件。
AppendToFile.appendMethodB(文件名,內容);
AppendToFile.appendMethodB(文件名,“追加結束。\ n ");
//顯示文件內容
ReadFromFile.readFileByLines(文件名);
}
}