A simple example. The regularity is to cancel extra spaces or tab keys
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 according to the file path br = new BufferedReader(new FileReader(filePath));//In filePath is aaa.txt file String str = "";//Loop reading of each line of the file, modify the line that needs to be modified, and put it in the buffer object while ((line = br.readLine()) != null) { //Set the regular to convert all extra spaces into a space str=line+"/r/n"; String[] dictionary = str.split("//s{2,}|/t"); for(int i=0;i<dictionary.length;i++){str = "insert into tablename values("+ dictionary[0]+",'"+dictionary[1]+"',"+dictionary[2]+"')"; } 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 files 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 = 1; 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 java implementation of reading txt files and using spaces to get data on each line is all the content I share with you. I hope it can give you a reference and I hope you can support Wulin.com more.