1.這是第一篇文章,寫的不錯,你須要逐字逐句的去看,會發現分清楚字節流和字符流就好多了,而後咱們須要看第二篇博客,如何相互轉化。 html
第三篇是個例子,寫的通常,不怎麼樣,能夠參考,看看。 網絡
http://blog.sina.com.cn/s/blog_4a9f789a0100ik3p.html
工具
Java中文件讀寫操做的做用是什麼? spa
回答這個問題時應該先想到的是Java只是一門語言,咱們的一種使用工具而已,這樣答案就明晰了,就是將外來的各類數據寫入到某一個文件中去,用以保存下來;或者從文件中將其數據讀取出來,供咱們使用。就以下電影過程,從網絡資源中下載一部電影保存於你電腦中(寫文件),當你想看的時候就用播放器打開(讀文件)。
Java中如何對文件進行讀寫操做?
先理一理,Java中的流分兩種,字節流和字符流,其中字節流的兩個基類是InputStream和OutputStream;字符流的兩個基類是Reader和Writer。所謂文件流,即咱們對文件的操做留不開流。由此可知咱們要用到某個類必然繼承如上的四個基類之一。Java中一切都是類,一切都是對象。天然會想到文件操做有哪些類:
以下四個直接用到的類:
字節流中:File
InputStream和FileOutputStream
字符流中:FileReader和FileWriter
找到類就好辦事了。剩下來的就是去找實現方法啦。
兩種選擇方案在這裏,這就牽涉到咱們如何選擇合適的文件讀寫方式呢?
選擇條件的區別:
以字節爲單位讀取文件,經常使用於讀二進制文件,如圖片、聲音、影像等文件。
以字符爲單位讀取文件,經常使用於讀文本,數字等類型的文件.
至因而否選擇用Buffer來對文件輸入輸出流進行封裝,就要看文件的大小,如果大文件的讀寫,則選擇Buffer這個桶來提供文件讀寫效率。
以下是簡單運用實例:
一、運用字節流對文件進行直接讀寫:
注:
FileOutputStream(file, true);裏面true參數表示不覆蓋原文件,直接在文件後面追加添加內容。
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();
}
}
}
二、運用字符流對文件進行直接讀寫:
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封裝以後的運用:
一、
對字節流封裝後對文件進行讀寫:
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();
}
}
}
二、
對字符流封裝後對文件進行讀寫:
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();
}
}
}