欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java基礎(chǔ)之查找文本特定內(nèi)容后進(jìn)行修改

 更新時間:2021年04月20日 14:42:09   作者:沒譜的曲  
這篇文章主要介紹了Java基礎(chǔ)之查找文本特定內(nèi)容后進(jìn)行修改,文中有非常詳細(xì)的代碼示例,對正在學(xué)習(xí)java基礎(chǔ)的小伙伴們有非常好的幫助,需要的朋友可以參考下

一、問題的產(chǎn)生

基于I/O流編寫的圖書館管理系統(tǒng)

在最近使用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)前用戶信息:

當(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)文章

最新評論