拿到文本的BufferedReader,而後使用BufferedReader的readLine()方法,就能夠得到一行文本字符串,沒有內容的時候會返回null,遍歷執行就能夠輸出文本的全部內容。code
1·拿到BufferedReader字符串
public static BufferedReader openHosts(){ BufferedReader br = null; try{ br = new BufferedReader(new InputStreamReader( new FileInputStream(hostsPath)));//hostsPath是文本路徑 }catch(IOException e){ System.out.println("獲取" + hostsPath + "的BufferedReader 時出現異常"); } return br; }
2·循環讀取就能夠遍歷這個文本的內容io
for ( String hostsFileTextLine = hostsBufferedReader.readLine(); hostsFileTextLine != null; hostsFileTextLine = hostsBufferedReader.readLine())