本文實例講述了Java編程中文件讀寫的方法。分享給大家供大家參考,具體如下:
Java中文件讀寫操作的作用是什么?
回答這個問題時應該先想到的是Java只是一門語言,我們的一種使用工具而已,這樣答案就明晰了,就是將外來的各種數(shù)據(jù)寫入到某一個文件中去,用以保存下來;或者從文件中將其數(shù)據(jù)讀取出來,供我們使用。就如下電影過程,從網(wǎng)絡資源中下載一部電影保存于你電腦中(寫文件),當你想看的時候就用播放器打開(讀文件)。
Java中如何對文件進行讀寫操作?
先理一理,Java中的流分兩種,字節(jié)流和字符流,其中字節(jié)流的兩個基類是InputStream和OutputStream;字符流的兩個基類是Reader和Writer。所謂文件流,即我們對文件的操作留不開流。由此可知我們要用到某個類必然繼承如上的四個基類之一。Java中一切都是類,一切都是對象。自然會想到文件操作有哪些類:
如下四個直接用到的類:
字節(jié)流中:FileInputStream和FileOutputStream
字符流中:FileReader和FileWriter
找到類就好辦事了。剩下來的就是去找實現(xiàn)方法啦。
兩種選擇方案在這里,這就牽涉到我們如何選擇合適的文件讀寫方式呢?
選擇條件的區(qū)別:
以字節(jié)為單位讀取文件,常用于讀二進制文件,如圖片、聲音、影像等文件。
以字符為單位讀取文件,常用于讀文本,數(shù)字等類型的文件.
至于是否選擇用Buffer來對文件輸入輸出流進行封裝,就要看文件的大小,若是大文件的讀寫,則選擇Buffer這個桶來提供文件讀寫效率。
如下是簡單運用實例:
1、運用字節(jié)流對文件進行直接讀寫:
注:FileOutputStream(file, true);里面true參數(shù)表示不覆蓋原文件,直接在文件后面追加添加內容。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
public class FileTest { static File file = new File( "d:/test.txt" ); public static void main(String[] args) { try { FileOutputStream out = new FileOutputStream(file, true ); String s = "Hello,world!\r\n" ; out.write(s.getBytes()); out.flush(); out.close(); //FileInputStream in = new FileInputStream(file); //byte [] b = new byte[20]; //in.read(b, 0, b.length); //System.out.println(new String(b)); //in.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } |
2、運用字符流對文件進行直接讀寫:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
public class File03 { static File file = new File( "d:/test.txt" ); public static void main(String[] args) { try { FileWriter fw = new FileWriter(file, true ); fw.write( "Hello,world!\r\n" ); fw.flush(); fw.close(); //FileReader fr = new FileReader(file); //int i=0; //String s =""; //while( ( i = fr.read() )!= -1) //{ // s = s +(char)i; //} //System.out.println(s); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } |
文件讀寫流用Buffer封裝之后的運用:
1、對字節(jié)流封裝后對文件進行讀寫:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
static File file = new File( "d:/test.txt" ); public static void main(String[] args) { try { // FileOutputStream out = new FileOutputStream(file, true); // BufferedOutputStream bout = new BufferedOutputStream(out); // String s = "I have a dream!"; // bout.write(s.getBytes()); // bout.flush(); // bout.close(); FileInputStream in = new FileInputStream(file); BufferedInputStream bin = new BufferedInputStream(in); byte [] b = new byte [ 15 ]; bin.read(b); bin.close(); System.out.println( new String(b)); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } |
2、對字符流封裝后對文件進行讀寫:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
public class File03 { static File file = new File( "d:/test.txt" ); public static void main(String[] args) { try { // FileWriter fw = new FileWriter(file, true); // BufferedWriter bw = new BufferedWriter(fw); // String nextLine = System.getProperty("line.separator"); // bw.write("Hello,world!" + nextLine); // bw.flush(); // bw.close(); FileReader fr = new FileReader(file); BufferedReader br = new BufferedReader(fr); int i = 0 ; String s = "" ; String temp = null ; while ((temp=br.readLine())!= null ) { s = s+temp; } System.out.println(s); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } |
希望本文所述對大家Java程序設計有所幫助。