Cheap VPS host selection
Provide server host evaluation information

Java reads every line of the txt file and fetches data with spaces in each line

Let's share how to use java to read txt files and get data in each line with spaces,

A simple example. Regular is to cancel extra space or tab key

 package test4;   import  java.io.BufferedReader; import  java.io.FileReader; import  java.io.IOException;   public   class  ExplaceSql { public   static   void  main( String [] args) {   String  filePath = ExplaceSql.class.getResource( "" ).getPath()+ "aaa.txt" ;   //File Path  read(filePath); } /** *Read content  */
	 public   static   String  read( String  filePath){ BufferedReader br =  null ; String  line = null ; //StringBuffer buf = new StringBuffer();
		 try  { //Create buffered input stream based on file path br =  new  BufferedReader( new  FileReader(filePath)); //Aaa.txt file in filePath
			 String  str =  "" ; //Read each line of the file circularly, modify the line to be modified, and put it into the buffer object   while  ((line = br.readLine()) !=   null ) {   //Set regular to convert all extra spaces into one space  str=line+ "\r\n" ;   String [] dictionary = str.split( "\\s{2,}|\t" );   for (int i= zero ; i<dictionary.length;i++){ str =  "insert into tablename values(" + dictionary[ zero ]+ ",'" +dictionary[ one ]+ "'," +dictionary[ two ]+ "')" ;  }  System.out.println(str);    }  catch  (Exception e) { e.printStackTrace(); } finally  {     if  (br !=  null ) { //Close Stream      try  {      br.close();     }  catch  (IOException e) {       br =  null ;   }   } } return   null ; } }

Java reads and writes txt file line by line

 package help;   import  java.io.BufferedReader; import  java.io.BufferedWriter; import  java.io.File; import  java.io.FileInputStream; import  java.io.FileOutputStream; import  java.io.InputStreamReader; import  java.io.OutputStreamWriter; import  java.util. HashMap; import  java.util. Map;   public   class   TXTParseUtils   {     private   static   final   Integer   ONE  =  one ;     public   static  void main( String [] args) {    Map < String  Integer  map  = new  HashMap < String  Integer >();      /*Read data*/    try  {     BufferedReader  br = new  BufferedReader (new  InputStreamReader (new  FileInputStream (new  File ( "D:/Reimbursement. txt" )),                     "UTF-8" ));     String  lineTxt = null;     while  ((lineTxt = br. readLine ()) !=  null) {      String [] names = lineTxt. split ( "," );      for  ( String  name : names) {       if  ( map .keySet(). contains (name)) {        map .put(name, ( map . get (name) +  ONE ));      }  else  {        map .put(name,  ONE );      }     }    }    br.close();   }  catch  ( Exception  e) {     System .err. println ( "read errors :"  + e);   }      /*Output Data*/    try  {     BufferedWriter  bw = new  BufferedWriter (new  OutputStreamWriter (new  FileOutputStream (new  File ( "D:/result. txt" )),                     "UTF-8" ));       for  ( String  name :  map .keySet()) {     bw.write(name +  " "  +  map . get (name));     bw.newLine();    }    bw.close();   }  catch  ( Exception  e) {     System .err. println ( "write errors :"  + e);   }  } }

The above is the detailed code that java reads every line of the txt file and fetches data in every line with spaces.

Do not reprint without permission: Cheap VPS evaluation » Java reads every line of the txt file and fetches data with spaces in each line