java.io (5) - 小結 - 讀取與寫入檔案內容
package ch12io;
import java.io.*;
public class BufReader {
public static void main(String[] args) throws IOException {
//互動式寫入檔案
FileWriter fw = null;
BufferedWriter bw = null;
BufferedReader br = null;
try {
//利用FileWriter讀入檔案
fw = new FileWriter("test.txt", true);
//將BufferedWriter與FileWriter介接起來
bw = new BufferedWriter(fw);
//從Console作連續輸入,每次輸入記錄為一行
br = new BufferedReader(new InputStreamReader(System.in));
while(true){
System.out.print("please enter a string: ");
String str = br.readLine();
System.out.println("The string u entered is: " + str);
if(str.equals("q")) //鍵入"q"以離開輸入模式
break;
bw.write(str);//寫入test.txt內
bw.newLine();//換行
}
} catch (IOException ex) { System.out.println("ex: "+ex); }
finally{
bw.close();
}
//3種讀取方式
//第1種:FileInputStream
FileInputStream fir = null;
try {
fir = new FileInputStream("test.txt");
int totalbytes = fir.available();
byte[] buffer = new byte[totalbytes];
if(fir.read(buffer)==totalbytes){
String str1 = new String(buffer);
System.out.println("\"檔案內容為: \"");
System.out.print(str1);
}
} catch (FileNotFoundException ex1) {System.out.println("ex1. "+ex1); }
//第2種:FileReader
FileReader fr = null;
fr = new FileReader("test.txt");
char[] buffer1 = new char[1];
while(fr.read(buffer1)!=-1){ //-1意謂到檔案尾端
String str1 = new String(buffer1);
System.out.print(str1);
}
//第3種:BufferedReader結合FileReader
FileReader fer = null;
BufferedReader bdr = null;
fer = new FileReader("test.txt");
bdr = new BufferedReader(fer);
String line = null;
while( (line=bdr.readLine())!=null ){//讀取一行,判斷是否為檔案結尾
System.out.println(line);
}
}
}
import java.io.*;
public class BufReader {
public static void main(String[] args) throws IOException {
//互動式寫入檔案
FileWriter fw = null;
BufferedWriter bw = null;
BufferedReader br = null;
try {
//利用FileWriter讀入檔案
fw = new FileWriter("test.txt", true);
//將BufferedWriter與FileWriter介接起來
bw = new BufferedWriter(fw);
//從Console作連續輸入,每次輸入記錄為一行
br = new BufferedReader(new InputStreamReader(System.in));
while(true){
System.out.print("please enter a string: ");
String str = br.readLine();
System.out.println("The string u entered is: " + str);
if(str.equals("q")) //鍵入"q"以離開輸入模式
break;
bw.write(str);//寫入test.txt內
bw.newLine();//換行
}
} catch (IOException ex) { System.out.println("ex: "+ex); }
finally{
bw.close();
}
//3種讀取方式
//第1種:FileInputStream
FileInputStream fir = null;
try {
fir = new FileInputStream("test.txt");
int totalbytes = fir.available();
byte[] buffer = new byte[totalbytes];
if(fir.read(buffer)==totalbytes){
String str1 = new String(buffer);
System.out.println("\"檔案內容為: \"");
System.out.print(str1);
}
} catch (FileNotFoundException ex1) {System.out.println("ex1. "+ex1); }
//第2種:FileReader
FileReader fr = null;
fr = new FileReader("test.txt");
char[] buffer1 = new char[1];
while(fr.read(buffer1)!=-1){ //-1意謂到檔案尾端
String str1 = new String(buffer1);
System.out.print(str1);
}
//第3種:BufferedReader結合FileReader
FileReader fer = null;
BufferedReader bdr = null;
fer = new FileReader("test.txt");
bdr = new BufferedReader(fer);
String line = null;
while( (line=bdr.readLine())!=null ){//讀取一行,判斷是否為檔案結尾
System.out.println(line);
}
}
}
0 個意見:
張貼留言
訂閱 張貼留言 [Atom]
<< 首頁