字符流
以字符為單位讀寫資料,專門用于處理文本檔案
1、字符輸入流Reader
? java.io.Reader抽象類是表示用于讀取字符流的所有類的超類,可以讀取字符資訊到記憶體中,它定義了字符輸入流的基本共性功能方法
public void close() :關閉此流并釋放與此流相關聯的任何系統資源,
public int read(): 從輸入流讀取一個字符,
public int read(char[] cbuf): 從輸入流中讀取一些字符,并將它們存盤到字符陣列 cbuf中 ,
1、FileReader類
? 構造時使用系統默認的字符編碼和默認位元組緩沖區
? 當你創建一個流物件時,必須傳入一個檔案路徑,
FileReader(File file): 創建一個新的 FileReader ,給定要讀取的File物件,
FileReader(String fileName): 創建一個新的 FileReader ,給定要讀取的檔案的名稱,
2、讀取字符資料
public class Av {
public static void main(String[] args) throws IOException {
FileReader fr = new FileReader("D:\\av.txt");
int a;
while ((a = fr.read())!=-1){
System.out.println((char)a);
}
fr.close();
}
輸出結果
a
s
d
f
g
3、使用字符陣列
? read(char[] cbuf)每次讀取b的長度個字符到陣列中,回傳讀取到的有效字符個數,讀取到末尾時,回傳-1
public class FISRead {
public static void main(String[] args) throws IOException {
// 使用檔案名稱創建流物件
FileReader fr = new FileReader("D:\\av.txt");
// 定義變數,保存有效字符個數
int len ;
// 定義字符陣列,作為裝字符資料的容器
char[] cbuf = new char[2];
// 回圈讀取
while ((len = fr.read(cbuf))!=-1) {
System.out.println(new String(cbuf,0,len));
}
// 關閉資源
fr.close();
}
}
2 、字符輸出流Writer
? 將指定的字符資訊寫出到目的地
public void write(int c) 寫入單個字符,
public void write(char[] cbuf) 寫入字符陣列,
public abstract void write(char[] cbuf, int off, int len) 寫入字符陣列的某一部分,off陣列的開始索引,len寫的字符個數,
public void write(String str) 寫入字串,
public void write(String str, int off, int len)` 寫入字串的某一部分,off字串的開始索引,len寫的字符個數,
public void flush() 重繪該流的緩沖,
public void close() 關閉此流,但要先重繪它,
1 寫出字符
? write(int a)每次寫出一個字符
public class FWWrite {
public static void main(String[] args) throws IOException {
// 使用檔案名稱創建流物件
FileWriter fw = new FileWriter("fw.txt");
// 寫出資料
fw.write(97); // 寫出第1個字符
fw.write('ad'); // 寫出第2個字符
fw.write('C'); // 寫出第3個字符
fw.write(30000); // 寫出第4個字符,中文編碼表中30000對應一個漢字,
/*
關閉資源時,與FileOutputStream不同,
如果不關閉,資料只是保存到緩沖區,并未保存到檔案,
*/
// fw.close();
//未呼叫close方法,資料只是保存到了緩沖區,并未寫出到檔案中
}
}
2 寫出字符陣列
? write(char[] cbuf) 和 write(char[] cbuf, int off, int len)
public class FWWrite {
public static void main(String[] args) throws IOException {
// 使用檔案名稱創建流物件
FileWriter fw = new FileWriter("av.txt");
// 字串
String msg = "這不是一句話";
// 寫出字符陣列
fw.write(msg);
//第一個2是索引 第二個2是長度
fw.write(msg,2,2);
// 關閉資源
fos.close();
}
}
3、Properties類
`java.util.Properties ` 繼承于` Hashtable` ,來表示一個持久的屬性集,它使用鍵值結構存盤資料,每個鍵及其對應值都是一個字串,該類也被許多Java類使用,比如獲取系統屬性時,`System.getProperties` 方法就是回傳一個`Properties`物件,
`public Properties()` :創建一個空的屬性串列,
- `public Object setProperty(String key, String value)` : 保存一對屬性,
- `public String getProperty(String key) ` :使用此屬性串列中指定的鍵搜索屬性值,
- `public Set<String> stringPropertyNames() ` :所有鍵的名稱的集合,
?
public class ProDemo {
public static void main(String[] args) throws FileNotFoundException {
// 創建屬性集物件
Properties properties = new Properties();
// 添加鍵值對元素
properties.setProperty("filename", "a.txt");
properties.setProperty("length", "209385038");
properties.setProperty("location", "D:\\av.txt");
// 列印屬性集物件
System.out.println(properties);
// 通過鍵,獲取屬性值
System.out.println(properties.getProperty("filename"));
System.out.println(properties.getProperty("length"));
System.out.println(properties.getProperty("location"));
// 遍歷屬性集,獲取所有鍵的集合
Set<String> strings = properties.stringPropertyNames();
// 列印鍵值對
for (String key : strings ) {
System.out.println(key+" -- "+properties.getProperty(key));
}
}
}
輸出結果:
{filename=a.txt, length=209385038, location=D:\a.txt}
a.txt
209385038
D:\av.txt
filename -- a.txt
length -- 209385038
location -- D:\av.txt
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/229837.html
標籤:java
上一篇:CoProcessFunction實戰三部曲之二:狀態處理
下一篇:使用一維陣列,模擬堆疊資料結構
