久久久久久久视色,久久电影免费精品,中文亚洲欧美乱码在线观看,在线免费播放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對(duì)文件的讀寫操作(圖文詳解)

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

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

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

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

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

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

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

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

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

      1.文件讀取模塊:

       /**      * 功能:Java讀取txt文件的內(nèi)容      * 步驟:1:先獲得文件句柄      * 2:獲得文件句柄當(dāng)做是輸入一個(gè)字節(jié)碼流,需要對(duì)這個(gè)輸入流進(jìn)行讀取      * 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)容出錯(cuò)"); 			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(); 		} 	}

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

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

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

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

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

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

      推薦教程:《java教程》

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