Packagecom.loongtao.general.crawler.slave.utils;ImportJava.io.BufferedReader;ImportJava.io.File;ImportJava.io.FileInputStream;ImportJava.io.InputStreamReader;/*** Read TXT file tool class *@authorLilin*/ Public classtxtutil{PrivateString IP; PrivateString Port; PublicTxtutil () { This. Readtxtfile (); } /*** Function: Java read content of TXT file * Step: 1: Get file Handle First * 2: Get file handle as input a byte stream, need to read this input stream * 3: Read to input stream, need to read generated byte stream * 4: A The output of the row line. ReadLine (). * Note: It is unusual to consider the situation *@paramFilePath*/ Public voidReadtxtfile () {Try{String encoding= "UTF-8"; File File=NewFile (Txtutil.class. GetResource ("/"). GetPath () + "\ \" + "Listenner.txt"); if(File.isfile () && file.exists ()) {//determine if a file existsInputStreamReader Read =NewInputStreamReader (NewFileInputStream (file), encoding);//considering the encoding formatBufferedReader BufferedReader =NewBufferedReader (read); String Linetxt=NULL; while((Linetxt = Bufferedreader.readline ())! =NULL){ if(Linetxt.indexof ("IP") >-1) {IP=linetxt.substring (linetxt.indexof ("IP") +3). Trim (); }Else if(Linetxt.indexof ("Port") >-1) {Port=linetxt.substring (Linetxt.indexof ("Port") +5). Trim (); }} read.close (); }Else{System.out.println ("The specified file cannot be found"); } } Catch(Exception e) {System.out.println ("Error reading file contents"); E.printstacktrace (); } } Public Static voidMain (String argv[]) {Txtutil T=NewTxtutil (); //txtutil.readtxtfile ();System.out.println ("IP:" +t.ip+ "Port:" +t.port); } }
Java Read TXT file