java實(shí)現(xiàn)讀取txt文件并以在每行以空格取數(shù)據(jù)
簡(jiǎn)單一個(gè)例子。其中正則是取消多余空格或者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);
}
/**
* 讀取內(nèi)容
*/
public static String read(String filePath){
BufferedReader br = null;
String line =null;
//StringBuffer buf = new StringBuffer();
try {
//根據(jù)文件路徑創(chuàng)建緩沖輸入流
br = new BufferedReader(new FileReader(filePath));//filePath中是aaa.txt文件
String str = "";
//循環(huán)讀取文件的每一行,對(duì)需要修改的行進(jìn)行修改,放入緩沖對(duì)象中
while ((line = br.readLine()) != null) {
//設(shè)置正則將多余空格都轉(zhuǎn)為一個(gè)空格
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) {// 關(guān)閉流
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<String, Integer> map = new HashMap<String, Integer>();
/* 讀取數(shù)據(jù) */
try {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("D:/報(bào)銷.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);
}
/* 輸出數(shù)據(jù) */
try {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File("D:/結(jié)果.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);
}
}
}
以上這篇java實(shí)現(xiàn)讀取txt文件并以在每行以空格取數(shù)據(jù)就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- Java讀取txt文件中的數(shù)據(jù)賦給String變量方法
- java 實(shí)現(xiàn)讀取txt文本數(shù)據(jù)并以數(shù)組形式一行一行取值
- Java實(shí)現(xiàn)讀取鍵盤輸入保存到txt文件,再統(tǒng)計(jì)并輸出每個(gè)單詞出現(xiàn)次數(shù)的方法
- Java讀取TXT文件內(nèi)容的方法
- Java讀取txt文件和寫入txt文件的簡(jiǎn)單實(shí)例
- java實(shí)現(xiàn)讀取txt文件中的內(nèi)容
- java 逐行讀取txt文本如何解決中文亂碼
- Java(springboot) 讀取txt文本內(nèi)容代碼實(shí)例
相關(guān)文章
詳解springboot設(shè)置cors跨域請(qǐng)求的兩種方式
這篇文章主要介紹了詳解springboot設(shè)置cors跨域請(qǐng)求的兩種方式,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-11-11
Activiti如何啟動(dòng)流程并使流程前進(jìn)
這篇文章主要介紹了Activiti如何啟動(dòng)流程并使流程前進(jìn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03
基于springboot攔截器HandlerInterceptor的注入問題
這篇文章主要介紹了springboot攔截器HandlerInterceptor的注入問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09
本地安裝MinIO分布式對(duì)象存儲(chǔ)服務(wù)器的詳細(xì)步驟
本地安裝MinIO非常簡(jiǎn)單,MinIO提供了獨(dú)立的二進(jìn)制文件,無需額外的依賴,本文介紹如何在本地安裝MinIO分布式對(duì)象存儲(chǔ)服務(wù)器,感興趣的朋友一起看看吧2024-01-01
Spring?Validation參數(shù)效驗(yàn)的各種使用姿勢(shì)總結(jié)
在實(shí)際項(xiàng)目中經(jīng)常需要對(duì)前段傳來的數(shù)據(jù)進(jìn)行校驗(yàn),下面這篇文章主要給大家介紹了關(guān)于Spring?Validation參數(shù)效驗(yàn)的各種使用姿勢(shì),文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-04-04
微信公眾平臺(tái)開發(fā)實(shí)戰(zhàn)Java版之微信獲取用戶基本信息
這篇文章主要介紹了微信公眾平臺(tái)開發(fā)實(shí)戰(zhàn)Java版之微信獲取用戶基本信息 的相關(guān)資料,需要的朋友可以參考下2015-12-12
利用Java Apache POI 生成Word文檔示例代碼
本篇文章主要介紹了利用Java Apache POI 生成Word文檔示例代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05

