2017-10-30
前些天面試的時候考了兩道題,都是有關 Java 基礎的:
- 利用 Java io 包進行讀寫文件;
- 使用 JDBC 獲取數據。
很可惜當時記不起來這些基礎知識,所以很有必要整理一下,鞏固一下這方面的知識。
在這篇總結里,我將通過代碼,文字解釋兩部分來總結 io 包的讀寫文件操作。
場景
例如在桌面上有一份 1.txt 文件,里面是一行以半角逗號分隔的亂序數字。請編寫 Java 程序讀取該文件,並降序排序,寫到 2.txt 文件中(若沒有就新建)。限用 io 包。
考察知識點
- 讀取文件
- 字符串與整型數組之間的轉換
- 排序算法
- 寫文件
代碼示例
我也將代碼托管在碼雲平台,可以前往參考:https://gitee.com/jinglun404/io-demo
1 import java.io.*; 2 3 public class Main { 4 5 public static void main(String[] args) { 6 String filePath = "C:\\Users\\ASUSS\\Desktop\\1.txt"; 7 8 readFile(filePath); 9 } 10 11 // 讀取文件方法 12 public static void readFile(String filePath) { 13 try { 14 String encoding = "UTF-8"; 15 // 1.獲得File對象 16 File file = new File(filePath); 17 18 if (file.isFile() && file.exists()) { // 判斷文件是否存在 19 // 2.File對象->FileInputStream對象->InputStreamReader對象 20 InputStreamReader read = new InputStreamReader(new FileInputStream(file), encoding); 21 System.out.println("read:" + read); //read:java.io.InputStreamReader@14ae5a5 22 23 // 3.通過InputStreamReader對象獲得BufferedReader對象 24 BufferedReader bufferedReader = new BufferedReader(read); 25 System.out.println("bufferedReader:" + bufferedReader); //bufferedReader:java.io.BufferedReader@7f31245a 26 27 // 4.通過BufferedReader對象將文件內容以字符串形式讀出 28 String lineText = null; 29 while ((lineText = bufferedReader.readLine()) != null) { 30 System.out.println("lineText:" + lineText); //lineText:1,1,4,5,23,12,13,35,345,34,67,5,3,90 31 32 // 將讀取的字符串根據“,”分割成字符串數組 33 String[] stringNums = lineText.split(","); 34 35 // 將字符串數組轉換成整型數組,用於排序 36 int[] intNums = new int[stringNums.length]; 37 for (int i = 0; i < stringNums.length; i++) { 38 intNums[i] = new Integer(stringNums[i]); 39 } 40 41 // 調用排序方法 42 mySort(intNums); 43 44 // 將整型數組轉換成字符串,用於寫入文件 45 String s = ""; 46 for (int i = 0; i < intNums.length; i++) { 47 s += intNums[i] + ","; 48 } 49 s = s.substring(0, s.length()-1);// 去掉最后一個“,” 50 51 // 調用寫文件方法 52 writeFile(s); 53 } 54 read.close(); 55 56 } else { 57 System.out.println("找不到指定文件!"); 58 } 59 } catch (Exception e) { 60 System.out.println("讀取文件出錯!"); 61 e.printStackTrace(); 62 } 63 } 64 65 // 寫入文件方法 66 private static void writeFile(String s) { 67 String writeFile = "C:\\Users\\ASUSS\\Desktop\\2.txt"; 68 69 // 1.獲得File對象 70 File newFile = new File(writeFile); 71 72 try { 73 if (!newFile.exists()) { 74 // 若文件不存在,則新建文件 75 newFile.createNewFile(); 76 } 77 78 // 2.通過File對象獲得FileOutputStream對象 79 FileOutputStream out = new FileOutputStream(newFile, true); 80 StringBuffer sb = new StringBuffer(); 81 sb.append(s); 82 83 // 3.通過FileOutputStream進行寫操作 84 out.write(s.toString().getBytes("UTF-8")); 85 out.close(); 86 } catch (IOException e) { 87 e.printStackTrace(); 88 } 89 } 90 91 // 冒泡排序,降序輸出 92 private static void mySort(int[] intNums) { 93 int temp = 0; 94 for (int i = 0; i < intNums.length; i++) { 95 for (int j = 0; j < intNums.length - i - 1; j++) { 96 if (intNums[j] < intNums[j + 1]) { 97 temp = intNums[j]; 98 intNums[j] = intNums[j + 1]; 99 intNums[j + 1] = temp; 100 } 101 } 102 } 103 } 104 }
說明
讀取文件
利用 Java 的 io 包讀取文件內容,分為幾個步驟:
- 傳入文件路徑生成 File 對象實例
- 通過 File 實例生成 FileInputStream 對象實例
- 通過 FileInputStream 對象實例生成 InputStreamReader 對象實例
- 通過 InputStreamReader 對象實例生成 BufferedReader 對象實例
- 通過 BufferedReader 對象實例的 .readLine() 方法讀取文件的一行數據
一般步驟如上,但為何要這樣做呢?我的理解如下:
- Java 的文件操作都是基於 File 對象的,因此讀取文件操作的第一步肯定是獲取一個 FIle 對象實例,那么獲取怎樣的實例呢?由此很容易聯想到需要獲取我們進行讀取的文件的 File 實例。所以這里便需要傳入文件路徑參數,來構造出一個 File 實例。
- 獲取了 File 的實例,我們的 Java 程序便與文件取得了聯系,接下來需要將文件讀入到內存中,方便我們的 Java 程序進行后續操作。
- 通過生成 FileInputStream 對象實例,我們將文件內容以字節形式寫入到內存。
- 但我們讀不懂字節內容,因此不能直接輸出存到內存中的字節內容。需要進行繼續轉換。
- 通過生成 InputStreamReader 對象實例,我們讀取了內存中的字節內容,然后進一步生成 BufferedReader 對象實例,將字節內容放在緩存區,加快操作的速度。
- 最后,利用 BufferedReader 的 .readLine() 方法,將字節轉換成我們能讀懂的字符串信息,進行輸出。
寫入文件
利用 Java io 包進行寫入文件,操作思路跟讀取文件類似,甚至簡單些,例如:
- 獲取 File 對象實例
- 通過 File 實例獲取 FileOutputStream 對象實例
- 利用 FileOutputStream 的 .write() 方法將字節數據寫入文件
當然這只是大概思路。具體實現過程可以參考我的代碼邏輯。
排序邏輯
對數字進行排序,實現的方法很多,我這里使用了常見的冒泡排序,具體不展開講了,網上也有很多資料。也可以參考我代碼的邏輯。
參考網站
http://www.cnblogs.com/manongxiaojiang/archive/2012/10/13/2722068.html
https://segmentfault.com/q/1010000000359840
https://www.ezloo.com/2008/03/jave_file_reader_writer.html