讀檔案
/**<br /> * 從檔案讀資料<br /> */<br />public void testRead() throws IOException {</p><p>String filename = "D://aaa.txt";<br />File file = new File(filename);</p><p>FileReader fr = new FileReader(file);</p><p>BufferedReader br = new BufferedReader(fr);</p><p>String s = null ;</p><p>while ( (s=br.readLine()) != null) {<br />System.out.println(s);<br />}</p><p>br.close();<br />fr.close();<br />}
寫檔案
/**<br /> * 往檔案寫資料<br /> */<br />public void testWrite() throws IOException {</p><p>String filename = "D://aaa.txt";<br />File file = new File(filename);</p><p>try {</p><p>FileWriter fw = new FileWriter(file);</p><p>BufferedWriter bw=new BufferedWriter(fw);<br />PrintWriter out = new PrintWriter(fw);</p><p>out.append("ssss");<br />out.append("/r/n");<br />out.append("ddddd");<br />//out.flush(); </p><p>out.close();<br />fw.close();</p><p>} catch (IOException e) {<br />System.out.println("Uh oh, got an IOException error!");<br />e.printStackTrace();<br />}<br />}