記一次JAVA-SPRINGBOOT讀寫(xiě)Jar包外的txt文件

項(xiàng)目部署在windows-service平臺(tái),直接Jar包部署
為一個(gè)web項(xiàng)目。服務(wù)端有個(gè)python腳本需要調(diào)用web項(xiàng)目中設(shè)置的txt的文件內(nèi)容。為了方便python調(diào)用,此文件在Jar包外。

由于本項(xiàng)目是java+python的項(xiàng)目。python需要讀取一部分txt文件中的內(nèi)容。為了方便文件修改,在java后端增加了對(duì)此文件的操作。這是我項(xiàng)目中第一次碰到的對(duì)Jar包外文件進(jìn)行讀寫(xiě)。故此記錄,方便以后翻閱。


txt文件內(nèi)容:

lxj 10000 v

lxj2 n

雙十一 20000 n

618 10000 n

購(gòu)物節(jié) 20000 n

柔軟的 10000 a

合適 10000 a

實(shí)惠 10000 d

大小 10000 a

喜歡 10000 a

棒棒噠 10000 d

雙十二 20000 m

褶皺 10000 d

美好 20000 a

耐磨 10000 a

yml配置文件路徑
wordChar:

custom:
    # txt 的文件路徑
    txtPath: D:/abc/
    # txt 的文件名稱
    txtName: userdict.txt
主要代碼片段(省去無(wú)關(guān)代碼)

WritePhraseWordsUtil

public class WritePhraseWordsUtil {
    /**
     *
     * @param file file需要傳入文件的全路徑
     */
    public static List<PhraseWordListEntity> readTxt(File file) throws IOException {
        String s;
        List<PhraseWordListEntity> listEntities = new ArrayList<>();
        /*
       以文件流的形式逐行讀取txt文件(文件存儲(chǔ)編碼需要utf-8格式,不然會(huì)亂碼)
        */
        InputStreamReader in = new InputStreamReader(new FileInputStream(file),"UTF-8");
        BufferedReader br = new BufferedReader(in);
        while ((s=br.readLine())!=null){
            String[] arr = s.split(" ");
            PhraseWordListEntity phraseWordListEntity = new PhraseWordListEntity(arr[0], DateUtil.getDateTimeDateNow());
            if(arr.length > 2){
                phraseWordListEntity.setCharacterType(arr[2]);
                if("20000".equals(arr[1])){
                    phraseWordListEntity.setWeight(100);
                }
                if("10000".equals(arr[1])){
                    phraseWordListEntity.setWeight(10);
                }
            }else{
                phraseWordListEntity.setWeight(1);
                phraseWordListEntity.setCharacterType(arr[1]);
            }
            listEntities.add(phraseWordListEntity);
        }
        return listEntities;
    }
}

CommentWordServiceImpl(調(diào)用方法)

public class CommentWordServiceImpl implements CommentsWordService {
    private final CustomCoreProperties customCoreProperties;
    
    public CommentWordServiceImpl( CustomCoreProperties customCoreProperties) {
        this.customCoreProperties = customCoreProperties;
    }

    /**
     * 初始化txt
     *
     * @throws IOException
     */
    @Override
    public void readText() throws IOException {
        /*
        fileName需要給全路徑
         */
        String fileName = customCoreProperties.getWordChar().getTxtName();
        String path = customCoreProperties.getWordChar().getTxtPath();
        List<PhraseWordListEntity> listEntities = WritePhraseWordsUtil.readTxtpath+fileName);
        for (PhraseWordListEntity wordListEntity : listEntities) {
            wordListEntity.setId(String.valueOf(idWorkerUtils.nextId()));
            phraseWordListMapper.insert(wordListEntity);
        }
    }
}
  • 以properties方式讀取yml設(shè)置的字段

CustomCorePropertiesConfig

@Configuration
@EnableConfigurationProperties(CustomCoreProperties.class)
public class CustomCorePropertiesConfig {

}

CustomCoreProperties

@ConfigurationProperties(prefix = "custom")
public class CustomCoreProperties {
    /**
     * 分詞相關(guān)
     */
    private WordCharProperties wordChar = new WordCharProperties();

    public AuthProperties getAuth() {
        return auth;
    }
}

WordCharProperties

public class WordCharProperties {

    private String jsonPath;

    private String txtPath;

    private String txtName;

    private String exeProgram;

    private String exePath;

    public WordCharProperties() {
    }

    public String getJsonPath() {
        return jsonPath;
    }

    public void setJsonPath(String jsonPath) {
        this.jsonPath = jsonPath;
    }

    public String getTxtPath() {
        return txtPath;
    }

    public void setTxtPath(String txtPath) {
        this.txtPath = txtPath;
    }

    public String getTxtName() {
        return txtName;
    }

    public void setTxtName(String txtName) {
        this.txtName = txtName;
    }

    public String getExeProgram() {
        return exeProgram;
    }

    public void setExeProgram(String exeProgram) {
        this.exeProgram = exeProgram;
    }

    public String getExePath() {
        return exePath;
    }

    public void setExePath(String exePath) {
        this.exePath = exePath;
    }
}

PhraseWordListEntity(轉(zhuǎn)換后的實(shí)體類)

public class PhraseWordListEntity {

    @Id
    @Column(name = "id")
    private String id;

    private String word;

    private Integer weight;
    /**
     * 詞性:n, a……
     */
    @Column(name = "charactertype")
    private String characterType;
    /**
     * 修改時(shí)間
     */
    @Column(name = "lstupdtime")
    private Date lstupdtime;

    public PhraseWordListEntity() {
    }

    public PhraseWordListEntity(String word, String characterType, Date lstupdtime) {
        this.word = word;
        this.characterType = characterType;
        this.lstupdtime = lstupdtime;
    }

    public PhraseWordListEntity(String word, Date lstupdtime) {
        this.word = word;
        this.lstupdtime = lstupdtime;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getWord() {
        return word;
    }

    public void setWord(String word) {
        this.word = word;
    }

    public Integer getWeight() {
        return weight;
    }

    public void setWeight(Integer weight) {
        this.weight = weight;
    }

    public String getCharacterType() {
        return characterType;
    }

    public void setCharacterType(String characterType) {
        this.characterType = characterType;
    }

    public Date getLstupdtime() {
        return lstupdtime;
    }

    public void setLstupdtime(Date lstupdtime) {
        this.lstupdtime = lstupdtime;
    }
}


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容