Java學習——讀寫txt檔案
阿新 • • 發佈:2018-11-19
package HHH; import java.io.*; import static java.lang.System.out; public class OpenFile { public static void main(String[] args) { // TODO Auto-generated method stub try{ FileInputStream rf = new FileInputStream("openFile.txt"); FileOutputStream wf= new FileOutputStream("write.txt"); int n=512; byte buffer[] = new byte[n];//buffer就是個byte型別陣列 while((rf.read(buffer,0,n) != -1) && (n>0))//讀取輸入流 { out.print(new String(buffer));//在螢幕中輸出,強制轉換成字串 wf.write(buffer,0,buffer.length); } out.println(); rf.close(); wf.close(); }catch(IOException ioe){ out.println(ioe); }catch(Exception e){ out.println(e); } } }