(一)IO流——字節流,FileInputStream、FileOutputStream、Properties集合

IO:實際就是Java源程序與操作系統上的文件進行的操作

 

根據數據的流向分爲:輸入流和輸出流。

輸入流 :把數據從其他設備上讀取到內存中的流。

輸出流 :把數據從內存 中寫出到其他設備上的流。

 

根據數據的類型分爲:字節流和字符流。

字節流 (Stream):以字節爲單位,讀寫數據的流。能對任意文件進行操作

字符流 (Writer、Reader):以字符爲單位,讀寫數據的流。只能對文本文件進行操作

 

 

字節流常用類

字節輸入流(InputStream) 字節輸出流(OutputStream)
FileInputStream FileOutputStream
BufferedInputStream BufferedOutputStream
ObjectInputStream ObjectOutputStream
  PrintStream

 

 

 

 

 

 

字符流常用類

字符輸入流(Reader) 字符輸出流(Writer)
FileReader FileWriter
BufferedReader BufferedWriter
InputStreamReader OutputStreamWriter
  PrintWriter

 

 

FileInputStream類:

FileInputStream構造方法:

public FileInputStream(File file)                                        創建字節輸入流以寫入由指定的 File對象表示的文件

File f = new File("\\b.txt");
FileInputStream fis = new FileInputStream(f);

public FileInputStream(String pathname)                      創建字節輸出流以寫入由指定的 String路徑表示的文件

FileInputStream fis = new FileInputStream("\\b.txt");

創建InputStream的對象File文件或String路徑必須是實際存在的文件路徑,不然會報FileNotFoundException異常

 

 

以下方法File均使用根目錄下的b.txt文件,內容如圖

public void close()                                                 關閉此輸入流並釋放與此流相關聯的任何系統資源

FileInputStream fis = new FileInputStream(new File("\\b.txt"));
fis.close();

完成流的操作之後都要關閉流,無論是什麼流

public int read()                                                       從輸入流讀取數據的下一個字節

File f = new File("\\b.txt");
FileInputStream fis = new FileInputStream(f);
int i = fis.read();
System.out.println(i);                        //輸出:97
System.out.println((char) i);                 //輸出:a

調用一次read()方法,就等於光標下移會返回下一個字節的整數

若讀到文件內容末尾時會返回-1

public int read(byte[] b)                                         從輸入流中讀取一些字節數,並將它們存儲到字節數組 b中

File f = new File("\\b.txt");
FileInputStream fis = new FileInputStream(f);
byte[] b = new byte[5];
int i = fis.read(b);
System.out.println(i);                      //輸出:5
System.out.println(Arrays.toString(b));     //輸出:[97, 98, 99, 100, 101]

使用字節數組來存放讀取數組時,會將讀取到的字節全部存入數組中,然後返回int型,代表存儲了多少個元素

數組保存,和直接讀取文件一樣,讀取完所有內容則會返回整數-1

 

 

根據以上的方法想要獲取整個文件的字節可以利用字節讀到末尾會返回-1的特性作結束標記

使用read()方法獲取文件所有字節:

File f = new File("\\b.txt");
FileInputStream fis = new FileInputStream(f);
int i = 0;
while ((i = fis.read()) != -1) {
    System.out.println((char) i);            //輸出:a  b  c  d  e  f  g  
}

使用read(byte[] b )方法獲取文件所有字節:

 File f = new File("\\b.txt");
 FileInputStream fis = new FileInputStream(f);
 int i = 0;
 byte [] b = new byte[1024];                 //作IO流存放字節的數組常用1024的整數倍大小數組
 while ((i = fis.read(b)) != -1) {
     System.out.print(new String(b,0,i));    //輸出:abcdefg
 }

 

 

 

FileOutputStream類:

FileOutputStream構造方法:

public FileOutputStream(File file)                                                     由指定的File創建字節輸出流對象

File f = new File("\\b.txt");
FileOutputStream fos = new FileOutputStream(f);

每次調用該輸出流對象,若已存在該文件就會清空文件內容

若不存在該文件就會自動創建一個

public FileOutputStream(String name)                                            由指定的路徑字符串創建字節輸出流對象

FileOutputStream fos = new FileOutputStream("\\b.txt");

public FileOutputStream(File file, boolean append)                      由指定的File創建可續寫的字節輸出流對象

File f = new File(\\b.txt");
FileOutputStream fos = new FileOutputStream(f,true);

創建FileOutputStream對象時加了true的參數,則不會清空源文件

輸出的文件字節都會在原有基礎上新增

public FileOutputStream(String name, boolean append)             由指定的路徑字符串創建可續寫的字節輸出流對象

FileOutputStream fos = new FileOutputStream("\\b.txt",true);

 

 

常用方法:

public  void  write(int b)                                                 輸出一個字節到文件中

File f = new File("c.txt");
FileOutputStream fos = new FileOutputStream(f);
fos.write(98);               //寫入了一個98字節,對應ASCII碼是一個 b

public  void  write(byte[] b)                                           將字節數組的元素輸出到文件中

File f = new File("c.txt");
FileOutputStream fos = new FileOutputStream(f);
byte [] b = {85,56,98,68};
fos.write(b);

將字節數組中的所有字節都對應ASCII碼錶,輸出到c.txt文件中

public  void  write(byte[] b, int off, int len)               將字節數組的從off開始len個元素輸出到文件中

File f = new File("c.txt");
FileOutputStream fos = new FileOutputStream(f);
byte [] b = {85,56,98,68};
fos.write(b,0,3);

將字節數組中從索引0開始的3個元素,也就是85,56,98 輸出到c.txt文件中

若想用字節流輸出一個字符串到文件中,可以使用String.getByte[]獲取數組再用write()方法輸出文件

 

 

文件複製:

利用InputStream和OutputStream的方法,可以實現複製文件到另一個地方

FileOutputStream fos = new FileOutputStream("c.txt");
FileInputStream fis = new FileInputStream("b.txt");
int i = 0;
while ((i = fis.read()) != -1) {
    fos.write(i);
}
fos.close();
fis.close();

先利用read()方法讀文件度到末尾會返回-1位終止循環標誌

然後每讀c.txt文件一個字節,就會在b.txt上輸出一個字節

由此循環就可以完成文件的複製

Stream字節流不止能複製txt文件還能複製視頻圖片等

 

 

Properties類:

項目中多數配置文件均使用Properties類作爲,配置信息保存

其實Properties類就是一個Map<String,String> 鍵和值都是String類型的雙列集合

可Properties類可以直接將集合中的數據保存到文件中,還可以將文件保存的信息直接讀取使用

 

Properties類文件後綴是:  xxx.properties

每個鍵值對佔一行,若想在文本中加入註釋需以#開頭寫註釋

 

Properties集合常用方法:

public Object setProperty(String key, String value)                       添加鍵值對

Properties prop = new Properties();            //直接new一個Properties()創建對象
prop.setProperty("name", "jack");
prop.setProperty("sex", "man");

若添加的鍵值對已存在,則使用新值替換舊值,並返回舊值

若添加的鍵值對不存在,則直接添加新的鍵和新的值,並返回null

public String getProperty(String key)                                                使用此屬性列表中指定的鍵搜索屬性值

Properties prop = new Properties();
prop.setProperty("name", "jack");
prop.setProperty("sex", "man");
System.out.println(prop.getProperty("sex"));        //輸出:man

public Set<String> stringPropertyNames()                                      Set集合保存所有鍵的名稱並返回

Properties prop = new Properties();
prop.setProperty("name", "jack");
prop.setProperty("sex", "man");
Set<String> s = prop.stringPropertyNames();
System.out.println(s);                //輸出:[sex, name]

相當於HashMap的keySet()方法,可用作遍歷

 

Properties處理文件方法:

public  void  load (InputStream  inStream)                                                             將流相關聯文件中的數據加載到集合中

Properties prop = new Properties();
FileInputStream fis = new FileInputStream("a.properties");
prop.load(fis);
System.out.println(prop);
fis.close();

這樣就將a.properites文件中的鍵值對都保存到prop集合中

 

public  void  store  (OutputStream  out, String  comments)                             將集合中的數據保存到流相關聯的目標文件中 

Properties prop = new Properties();
prop.setProperty("name", "jack");
prop.setProperty("sex", "boy");
prop.setProperty("age", "23");
prop.setProperty("work", "driver");
FileOutputStream fos = new FileOutputStream("b.properties");
prop.store(fos, "hi,this is b.properties");
fos.close();

comments是創建該文件時,第一行的註釋語句,不想寫可以傳null

 

輸出結果文件如圖:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章