Java基礎(chǔ)之查找文本特定內(nèi)容后進(jìn)行修改
一、問題的產(chǎn)生
在最近使用I/O流寫圖書館管理系統(tǒng)中有修改圖書信息的部分,以及借書和還書等多個部分內(nèi)容都需要對文本中的特定位置的內(nèi)容進(jìn)行精確查找并修改,在查閱資料和同學(xué)討論后最終將該問題解決了。
二、問題的解決
下面通過修改密碼為例,演示一下該方法的實(shí)現(xiàn):
具體代碼:
String filePath = "用戶信息.txt";
User modifyFile = new User();
System.out.println("請輸入原密碼:");
String oldString = sc.next();
//如果輸入的原密碼不正確,無法進(jìn)行修改,如果正確,才能進(jìn)行修改
if (oldString.equals(userinf[1])) {
System.out.println("請輸入新密碼:");
String newString = sc.next();
boolean result = modifyFile.writeFile(filePath, modifyFile.readFileContent(userinf, filePath, userinf[1], newString));// 修改文件中密碼
// 如果修改結(jié)果為true,進(jìn)行修改成功提示,否則提示修改失敗
if (result == true) {
System.out.println("修改成功,請重新登錄!");
return;
} else {
System.out.println("修改錯誤,請檢查后重新修改!");
}
} else {
System.out.println("輸入錯誤,請檢查后重新進(jìn)行修改!");
}
其中userinf為其他類中傳入的參數(shù),主要作用是為了進(jìn)行判斷密碼是否正確,具體代碼可以查看基于I/O流編寫的圖書館管理系統(tǒng)
我們注意到上述代碼中有一行為:
modifyFile.writeFile(filePath, modifyFile.readFileContent(userinf, filePath, userinf[1], newString));// 修改文件中密碼
該行代碼調(diào)用的兩個方法才是解決本文內(nèi)容的關(guān)鍵方法
具體實(shí)現(xiàn)如下:
// 讀文件
public String readFileContent(String[] userinf, String filePath, String oldString, String newString) {
Scanner sc = new Scanner(System.in);
BufferedReader br = null;
String line = null;
StringBuffer bufAll = new StringBuffer();// 保存修改過后的所有內(nèi)容
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream(filePath), "UTF-8"));
while ((line = br.readLine()) != null) {
StringBuffer buf = new StringBuffer();
// 修改內(nèi)容核心代碼
String[] userinf2 = line.split(",");
if (userinf2[0].equals(userinf[0])) {//判斷條件根據(jù)自己的要求修改
buf.append(line);
int indexOf = line.indexOf(oldString);
buf.replace(indexOf, indexOf + oldString.length(), newString);// 修改內(nèi)容
buf.append(System.getProperty("line.separator"));// 添加換行
bufAll.append(buf);
} else {
buf.append(line);
buf.append(System.getProperty("line.separator"));
bufAll.append(buf);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (Exception e) {
br = null;
}
}
}
return bufAll.toString();
}
// 寫文件
public boolean writeFile(String filePath, String content) {
BufferedWriter bw = null;
try {
bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filePath), "UTF-8"));
bw.write(content);
bw.flush();
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
if (bw != null) {
try {
bw.close();
} catch (IOException e) {
bw = null;
}
}
}
return true;
}
通過上述方法可以找到文本中對應(yīng)行中當(dāng)前信息所對應(yīng)的位置,然后將newString替換為oldString
如下所示:
以下為當(dāng)前用戶信息:


在操作中就行了修改密碼,此時我們再查看文件中張三所對應(yīng)的密碼

此時密碼已經(jīng)被修改為1234
到此這篇關(guān)于Java基礎(chǔ)之查找文本特定內(nèi)容后進(jìn)行修改的文章就介紹到這了,更多相關(guān)java查找文本內(nèi)容進(jìn)行修改內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot項(xiàng)目Maven下載依賴速度慢問題的解決方法
在使用Maven構(gòu)建項(xiàng)目時,有時會遇到下載依賴包速度慢的問題,為了提高下載速度,我們可以將默認(rèn)的倉庫地址替換為國內(nèi)鏡像源,所以本文介紹了SpringBoot項(xiàng)目Maven下載依賴速度慢問題的解決方法,需要的朋友可以參考下2024-08-08
SpringBoot中調(diào)用通用URL的實(shí)現(xiàn)
在 Spring Boot 應(yīng)用程序中,有時候我們需要調(diào)用一些通用的 URL 接口,本文主要介紹了SpringBoot中調(diào)用通用URL的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07
Java中使用RediSearch實(shí)現(xiàn)高效的數(shù)據(jù)檢索功能
RediSearch是一款構(gòu)建在Redis上的搜索引擎,它為Redis數(shù)據(jù)庫提供了全文搜索、排序、過濾和聚合等高級查詢功能,本文將介紹如何在Java應(yīng)用中集成并使用RediSearch,以實(shí)現(xiàn)高效的數(shù)據(jù)檢索功能,感興趣的朋友跟著小編一起來看看吧2024-05-05
Spring AOP實(shí)現(xiàn)復(fù)雜的日志記錄操作(自定義注解)
Spring AOP實(shí)現(xiàn)復(fù)雜的日志記錄操作(自定義注解),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09

