ImportJava.io.BufferedInputStream;ImportJava.io.BufferedReader;ImportJava.io.File;ImportJava.io.FileInputStream;ImportJava.io.InputStreamReader;ImportJava.io.Reader;Importjava.util.ArrayList;Importjava.util.List;/** * @authorcode farmer Xiao Jiang * h20121012.java * 2012-10-12 pm 11:40:21*/ Public classIO {/*** 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: The output of a row of rows. ReadLine (). * Note: It is unusual to consider the situation *@paramFilePath*/ Public Static voidreadtxtfile (String filePath) {Try{String encoding= "GBK"; String[] as; String s1,s2; File File=NewFile (FilePath); 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) {System.out.println (linetxt); //S1 = Linetxt.replaceall ("\\s+", "|");S2= Linetxt.replaceall ("\\s{1,}", ""); //System.out.println (S1); //System.out.println (S2); //As = Linetxt.split (""); //System.out.println (as[2]);} 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[]) {string FilePath= "E:\\test.txt"; Readtxtfile (FilePath); } }
Read files in Java are separated by rules and then output