每日一考和複習
每日一考
- 如何遍歷Map的key集,value集,key-value集,使用上泛型
Map<Integer, String> map = new HashMap<>();
map.put(1, "aa");
map.put(2, "ab");
map.put(3, "af");
map.put(4, "ag");
map.put(5, "art");
map.put(6, "ah");
Set<Integer> set = map.keySet();
Iterator<Integer> iterSet = set.iterator();
while (iterSet.hasNext()) {
System.out.println(iterSet.next());
}
Collection<String> listValue = map.values();
Iterator<String> iterValue = listValue.iterator();
while (iterValue.hasNext()) {
System.out.println(iterValue.next());
}
Set<Map.Entry<Integer, String>> entry = map.entrySet();
for (Map.Entry<Integer, String> e : entry) {
System.out.println(e.getKey() + "--->" + e.getValue());
}
- 寫出使用Iterator和增強for循環遍歷List的代碼,使用上泛型
List<String> list = new ArrayList<>();
list.add("a");
list.add("b");
list.add("c");
list.add("d");
Iterator<String> iterator = list.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
for (String str:list){
System.out.println(str);
}
- 提供一個方法,用於遍歷獲取HashMap<String,String>中的所有value,並存放在List中返回。考慮上集合中泛型的使用
public List<String> getValueList(Map<String, String> map) {
Collection<String> values = map.values();
List<String> list = new ArrayList<>();
list.addAll(values);
return list;
}
- 創建一個與a.txt文件同目錄下的另外一個文件b.txt
File file1 = new File("d:\\test\\a.txt");
File file2 = new File(file1.getParent(),"b.txt");
- Map接口中的常用方法有哪些
增:put(K k,V v)
刪:V remove(K k)
改:put(K k,V v)
查:get(K k)
長度:size
遍歷:keySet values entrySet
複習
day25的學習內容
IO流
IO流原理及流的分類
Java IO原理
-
I/O是Input/Output的縮寫, I/O技術是非常實用的技術,用於處理設備之間的數據傳輸。如讀/寫文件,網絡通訊等
-
Java程序中,對於數據的輸入/輸出操作以“流(stream)” 的方式進行
-
java.io包下提供了各種“流”類和接口,用以獲取不同種類的數據,並通過標準的方法輸入或輸出數據
-
輸入input:讀取外部數據(磁盤、光盤等存儲設備的數據)到程序(內存)中
-
輸出output:將程序(內存)數據輸出到磁盤、光盤等存儲設備中
流的分類
- 按操作數據單位不同分爲:字節流(8 bit),字符流(16 bit)
- 按數據流的流向不同分爲:輸入流,輸出流
- 按流的角色的不同分爲:節點流,處理流
IO流體系
節點流(文件流)
流程
- 實例化File類的對象,指明要操作的文件
- 提供具體的流
- 數據的讀入
- 流的關閉操作
這部分類很多,但是步驟就這四步
FileReader fr = null;
try {
//1.實例化File類的對象,指明要操作的文件
File file = new File("hello.txt");//相較於當前Module
//2.提供具體的流
fr = new FileReader(file);
//3.數據的讀入
//read():返回讀入的一個字符。如果達到文件末尾,返回-1
//方式一:
int data = fr.read();
while (data != -1) {
System.out.print((char) data);
data = fr.read();
}
//方式二:語法上針對於方式一的修改
int data;
while ((data = fr.read()) != -1) {
System.out.print((char) data);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//4.流的關閉操作
try {
if (fr != null) {
fr.close();
}
} catch (IOException e) {
e.printStackTrace();
}
//或
if (fr != null) {
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
對read()操作升級:使用read的重載方法
FileReader fr = null;
try {
//1.File類的實例化
File file = new File("hello.txt");
//2.FileReader流的實例化
fr = new FileReader(file);
//3.讀入的操作
//read(char[] cbuf):返回每次讀入cbuf數組中的字符的個數。如果達到文件末尾,返回-1
char[] cbuf = new char[5];
int len;
while ((len = fr.read(cbuf)) != -1) {
//方式一:
//錯誤的寫法,倒數第二次數組會填滿,最後一次對其進行覆蓋,數組長度還是5,而真正字符個數只有3
for (int i = 0; i < cbuf.length; i++) {
System.out.print(cbuf[i]);
}
//正確的寫法
for (int i = 0; i < len; i++) {
System.out.print(cbuf[i]);
}
//方式二:
//錯誤的寫法,對應着方式一的錯誤的寫法
String str = new String(cbuf);
System.out.print(str);
//正確的寫法
String str = new String(cbuf, 0, len);
System.out.print(str);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fr != null) {
//4.資源的關閉
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
- File對應的硬盤中的文件如果不存在,在輸出的過程中,會自動創建此文件
- File對應的硬盤中的文件如果存在:
- 如果流使用的構造器是:FileWriter(file,false) / FileWriter(file):對原有文件的覆蓋
- 如果流使用的構造器是:FileWriter(file,true):不會對原有文件覆蓋,而是在原有文件基礎上追加內容
FileReader fr = null;
FileWriter fw = null;
try {
//1.創建File類的對象,指明讀入和寫出的文件
File srcFile = new File("hello.txt");
File destFile = new File("hello2.txt");
//不能使用字符流來處理圖片等字節數據
File srcFile = new File("a.jpg");
File destFile = new File("b.jpg");
//2.創建輸入流和輸出流的對象
fr = new FileReader(srcFile);
fw = new FileWriter(destFile);
//3.數據的讀入和寫出操作
char[] cbuf = new char[5];
int len;//記錄每次讀入到cbuf數組中的字符的個數
while ((len = fr.read(cbuf)) != -1) {
//每次寫出len個字符
fw.write(cbuf, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//4.關閉流資源
//方式一:
try {
if (fw != null) {
fw.close();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fr != null) {
fr.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
//方式二:
try {
if (fw != null) {
fw.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (fr != null) {
fr.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
緩衝流
BufferedInputStream
BufferedOutputStream
BufferedReader
BufferedWriter
- 作用:
- 提供流的讀取、寫入的速度
- 提高讀寫速度的原因:內部提供了一個緩衝區
實現非文本文件的複製
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
//1.造文件
File srcFile = new File("a.jpg");
File destFile = new File("a3.jpg");
//2.造流
//2.1 造節點流
FileInputStream fis = new FileInputStream((srcFile));
FileOutputStream fos = new FileOutputStream(destFile);
//2.2 造緩衝流
bis = new BufferedInputStream(fis);
bos = new BufferedOutputStream(fos);
//3.複製的細節:讀取、寫入
byte[] buffer = new byte[10];
int len;
while ((len = bis.read(buffer)) != -1) {
bos.write(buffer, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//4.資源關閉
//要求:先關閉外層的流,再關閉內層的流
//說明:關閉外層流的同時,內層流也會自動的進行關閉。關於內層流的關閉,我們可以省略
if (bos != null) {
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
使用BufferedReader和BufferedWriter實現文本文件的複製
BufferedReader br = null;
BufferedWriter bw = null;
try {
//創建文件和相應的流
br = new BufferedReader(new FileReader(new File("p.txt")));
bw = new BufferedWriter(new FileWriter(new File("p1.txt")));
//讀寫操作
//方式一:使用char[]數組
char[] cbuf = new char[1024];
int len;
while ((len = br.read(cbuf)) != -1) {
bw.write(cbuf, 0, len);
}
//方式二:使用String
String data;
while ((data = br.readLine()) != null) {
//方法一:
bw.write(data + "\n");//data中不包含換行符
//方法二:
bw.write(data);//data中不包含換行符
bw.newLine();//提供換行的操作
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//關閉資源
if (bw != null) {
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
轉換流
InputStreamReader
:將一個字節的輸入流轉換爲字符的輸入流
OutputStreamWriter
:將一個字符的輸出流轉換爲字節的輸出流
-
作用:提供字節流與字符流之間的轉換
-
解碼:字節、字節數組 —>字符數組、字符串
編碼:字符數組、字符串 —> 字節、字節數組 -
字符集
ASCII:美國標準信息交換碼
用一個字節的7位可以表示
ISO8859-1:拉丁碼錶。歐洲碼錶
用一個字節的8位表示
GB2312:中國的中文編碼表
最多兩個字節編碼所有字符
GBK:中國的中文編碼表升級,融合了更多的中文文字符號
最多兩個字節編碼
Unicode:國際標準碼,融合了目前人類使用的所有字符。爲每個字符分配唯一的字符碼
所有的文字都用兩個字節來表示
UTF-8:變長的編碼方式
可用1-4個字節來表示一個字符
綜合使用InputStreamReader和OutputStreamWriter
InputStreamReader isr = null;
OutputStreamWriter osw = null;
try {
//1.造文件、造流
File file1 = new File("dbcp.txt");
File file2 = new File("dbcp_gbk.txt");
FileInputStream fis = new FileInputStream(file1);
FileOutputStream fos = new FileOutputStream(file2);
isr = new InputStreamReader(fis, "utf-8");
osw = new OutputStreamWriter(fos, "gbk");
//2.讀寫過程
char[] cbuf = new char[20];
int len;
while ((len = isr.read(cbuf)) != -1) {
osw.write(cbuf, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//3.關閉資源
if (isr != null) {
try {
isr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (osw != null) {
try {
osw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
標椎輸入、輸出(瞭解)
System.in
:標準的輸入流,默認從鍵盤輸入
System.out
:標準的輸出流,默認從控制檯輸出
-
System類的
setIn(InputStream is) / setOut(PrintStream ps)
方式重新指定輸入和輸出的流。 -
練習:
從鍵盤輸入字符串,要求將讀取到的整行字符串轉成大寫輸出。然後繼續進行輸入操作,直至當輸入“e”或者“exit”時,退出程序方法一:使用Scanner實現,調用next()返回一個字符串
方法二:使用System.in實現。System.in —> 轉換流 —> BufferedReader的readLine()流
BufferedReader br = null;
try {
InputStreamReader isr = new InputStreamReader(System.in);
br = new BufferedReader(isr);
while (true) {
System.out.println("請輸入字符串:");
String data = br.readLine();
if ("e".equalsIgnoreCase(data) || "exit".equalsIgnoreCase(data)) {
System.out.println("程序結束");
break;
}
String upperCase = data.toUpperCase();
System.out.println(upperCase);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
打印流(瞭解)
PrintStream
PrintWriter
提供了一系列重載的print() 和 println()
PrintStream ps = null;
try {
FileOutputStream fos = new FileOutputStream(new File("D:\\IO\\text.txt"));
// 創建打印輸出流,設置爲自動刷新模式(寫入換行符或字節 '\n' 時都會刷新輸出緩衝區)
ps = new PrintStream(fos, true);
if (ps != null) {// 把標準輸出流(控制檯輸出)改成文件
System.setOut(ps);
}
for (int i = 0; i <= 255; i++) { // 輸出ASCII字符
System.out.print((char) i);
if (i % 50 == 0) {
System.out.println();
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (ps != null) {
ps.close();
}
}
數據流(瞭解)
DataInputStream
DataOutputStream
作用:用於讀取或寫出基本數據類型的變量或字符串
練習:將內存中的字符串、基本數據類型的變量寫出到文件中
DataOutputStream dos = null;
try {
dos = new DataOutputStream(new FileOutputStream("data.txt"));
dos.writeUTF("abc");
dos.flush();//刷新操作,將內存中的數據寫入文件
dos.writeInt(23);
dos.flush();
dos.writeBoolean(true);
dos.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (dos != null) {
try {
dos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
將文件中存儲的基本數據類型變量和字符串讀取到內存中,保存在變量中
讀取不同類型的數據的順序要與當初寫入文件時,保存的數據的順序一致!
DataInputStream dis = null;
try {
dis = new DataInputStream(new FileInputStream("data.txt"));
String name = dis.readUTF();
int age = dis.readInt();
boolean isMale = dis.readBoolean();
System.out.println("name = " + name);
System.out.println("age = " + age);
System.out.println("isMale = " + isMale);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (dis != null) {
try {
dis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}