java中的接口是类吗
318
2023-01-26
java实现读取txt文件并以在每行以空格取数据
简RLisZsvwwj单一个RLisZsvwwj例子。其中正则是取消多余空格或者tab键
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"; // 文件路径
read(filePath);
}
/**
* 读取内容
*/
public static String read(String filePath){
BufferedReader br = nullhttp://;
String line =null;
//StringBuffer buf = new StringBuffer();
try {
//根据文件路径创建缓冲输入流
br = new BufferedReader(new FileReader(filePath));//filePath中是aaa.txt文件
String str = "";
//循环读取文件的每一行,对需要修改的行进行修改,放入缓冲对象中
while ((line = br.readLine()) != null) {
//设置正则将多余空格都转为一个空格
str=line+"\r\n";
String[] dictionary = str.split("\\s{2,}|\t");
for(int i=0;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) {// 关闭流 try { br.close(); } catch (IOException e) { br = null; } } } return null; } } java逐行读写txt文件 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 /* 读取数据 */ try { BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("D:/报销.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); } /* 输出数据 */ try { BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File("D:/结果.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); } } }
str = "insert into tablename values("+ dictionary[0]+",'"+dictionary[1]+"',"+dictionary[2]+"')";
}
System.out.println(str);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
if (br != null) {// 关闭流
try {
br.close();
} catch (IOException e) {
br = null;
}
}
}
return null;
}
}
java逐行读写txt文件
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
/* 读取数据 */
try {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("D:/报销.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);
}
/* 输出数据 */
try {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File("D:/结果.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);
}
}
}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~