Java --- I/O(1) 存取file === ## 幫助做IO的各種class都在 java.io.* 中 --- ## (1) 建立新.txt檔並寫入資料 ### ==FileWriter== 為提供各種method之class For instance : 新建一文字檔叫test,並在內容寫入100. ```java= public static void main(String arg[]) { FileWriter fw = new FileWriter("test.txt"); try { fw.write("100"); fw.flush(); } catch(IOException e) { ; } finally { fw.close(); } } ``` ## (2) 讀取某file的內容 ### ==FileReader== 為提供各種method之class For instance: 讀取某file的所有內容 ```java= public static void main(String arg[]) { String content = ""; FileReader fr = new FileReader("test.txt"); BufferedReader br = new BufferedReader(fr); try { while(br.ready()) { content += br.readLine(); content += "\n"; } } catch(IOException e) { ; } finally { fr.close(); } } ``` ###### tags: `java` `教學`
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up