久久久久久久视色,久久电影免费精品,中文亚洲欧美乱码在线观看,在线免费播放AV片

<center id="vfaef"><input id="vfaef"><table id="vfaef"></table></input></center>

    <p id="vfaef"><kbd id="vfaef"></kbd></p>

    
    
    <pre id="vfaef"><u id="vfaef"></u></pre>

      <thead id="vfaef"><input id="vfaef"></input></thead>

    1. 站長資訊網(wǎng)
      最全最豐富的資訊網(wǎng)站

      Java對文件的讀寫操作(圖文詳解)

      Java對文件的讀寫操作(圖文詳解)

      像我們經(jīng)常會遇到這樣的事情,例如一個txt文件中有姓名和電話,這個時候很經(jīng)常就需要將名字和電話號碼進行提取操作,這個時候就可以利用Java中io來實現(xiàn)了。

      這里我就不具體介紹io中的字節(jié)流和字符流的異同點了,有興趣的同學(xué)可以自己百度百度。

      今天主要是介紹一下如何實現(xiàn)對文件內(nèi)容的獲取還有就是對獲取的文件內(nèi)容進行修改操作。下面看具體案例介紹。

      Java對文件的讀寫操作(圖文詳解)

      這個是案例最終要實現(xiàn)的效果,在姓名和電話號碼直接添加分割符號。

      這里有一點需要主要的是,這個案例并不是直接在原先的txt文檔上面進行修改的,而是新建一個新的txt文件重新寫入新的內(nèi)容。

      好了廢話不多說,看看這個案例具體是怎么具體實現(xiàn)的。

      這個案例分為三個模塊:1.文件讀取模塊,2.姓名電話分離模塊,3.文件寫入模塊

      1.文件讀取模塊:

       /**      * 功能:Java讀取txt文件的內(nèi)容      * 步驟:1:先獲得文件句柄      * 2:獲得文件句柄當(dāng)做是輸入一個字節(jié)碼流,需要對這個輸入流進行讀取      * 3:讀取到輸入流后,需要讀取生成字節(jié)流      * 4:一行一行的輸出。readline()。      * 備注:需要考慮的是異常情況      * @param filePath      */ 	public static String readTxtFile(String filePath) { 		StringBuilder content = new StringBuilder(""); 		try { 			String encoding = "UTF-8"; 			File file = new File(filePath); 			if (file.isFile() && file.exists()) { 				InputStreamReader read = new InputStreamReader(new FileInputStream(file), encoding); 				BufferedReader bufferedReader = new BufferedReader(read); 				String lineTxt = null; 				while ((lineTxt = bufferedReader.readLine()) != null) { 					String[] result = getNamePhone(lineTxt); 					System.out.println(lineTxt); 					content.append(result[0] + "----" + result[1]); 					content.append("rn");// txt換行 				} 				read.close(); 			} else { 				System.out.println("找不到指定的文件"); 			} 		} catch (Exception e) { 			System.out.println("讀取文件內(nèi)容出錯"); 			e.printStackTrace(); 		} 		return content.toString(); 	}

      2.姓名電話分離模塊:

      public static String[] getNamePhone(String str) { 		String[] result = new String[2]; 		int index = 0; 		for (int i = 0; i < str.length(); i++) { 			if (str.charAt(i) >= '0' && str.charAt(i) <= '9') { 				index = i; 				break; 			} 		} 		result[0] = str.substring(0, index); 		result[1] = str.substring(index); 		return result; 	}

      3.文件寫入模塊:

      public static void printFile(String content) { 		BufferedWriter bw = null; 		try { 			File file = new File("D:/filename.txt"); 			if (!file.exists()) { 				file.createNewFile(); 			} 			FileWriter fw = new FileWriter(file.getAbsoluteFile()); 			bw = new BufferedWriter(fw); 			bw.write(content); 			bw.close(); 		} catch (IOException e) { 			e.printStackTrace(); 		} 	}

      通過這三個模塊就可以實現(xiàn)對文件的讀取操作了,然后對信息進行處理,最后將處理好的信息添加到新的文件中去。

      這里需要注意的是:項目的編碼格式要寫成utf-8,否則會出現(xiàn)亂碼的情況。

      Java對文件的讀寫操作(圖文詳解)

      到這里文件的讀寫操作就完結(jié)了,是不是特別簡單方便。

      感謝大家的閱讀,希望大家收益多多。

      本文轉(zhuǎn)自: https://blog.csdn.net/linzhiqiang0316/article/details/71744340

      推薦教程:《java教程》

      贊(0)
      分享到: 更多 (0)
      網(wǎng)站地圖   滬ICP備18035694號-2    滬公網(wǎng)安備31011702889846號