Java reads the contents of the txt file. It can be understood as follows:
package com.campu; import java.io.BufferedInputStream;import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.InputStreamReader;import java.io.Reader; /** * @author coder Xiaojiang* H20121012.java * 2012-10-12 11:40:21 pm */public class H20121012 { /** * Function: Java reads the content of the txt file* Steps: 1: Get the file handle first* 2: Obtaining the file handle is used to input a bytecode stream, and the input stream needs to be read * 3: After reading the input stream, the generated byte stream needs to be read * 4: Output of one line by one. readline(). * Note: What needs to be considered is exceptions* @param filePath */ public static void readTxtFile(String filePath){ try { String encoding="GBK"; File file=new File(filePath); if(file.isFile() && file.exists()){ //Judge whether the file exists InputStreamReader read = new InputStreamReader( new FileInputStream(file),encoding);//Consider the encoding format BufferedReader bufferedReader = new BufferedReader(read); String lineTxt = null; while((lineTxt = bufferedReader.readLine()) != null){ System.out.println(lineTxt); } read.close(); }else{ System.out.println("Specified file cannot be found"); } } } catch (Exception e) { System.out.println("Error reading file content"); e.printStackTrace(); } } public static void main(String argv[]){ String filePath = "L://Apache//htdocs//res//20121012.txt";// "res/"; readTxtFile(filePath); } }The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.