记一次JAVA-SPRINGBOOT读写Jar包外的txt文件

项目部署在windows-service平台,直接Jar包部署
为一个web项目。服务端有个python脚本需要调用web项目中设置的txt的文件内容。为了方便python调用,此文件在Jar包外。

由于本项目是java+python的项目。python需要读取一部分txt文件中的内容。为了方便文件修改,在java后端增加了对此文件的操作。这是我项目中第一次碰到的对Jar包外文件进行读写。故此记录,方便以后翻阅。


txt文件内容:

lxj 10000 v

lxj2 n

双十一 20000 n

618 10000 n

购物节 20000 n

柔软的 10000 a

合适 10000 a

实惠 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
主要代码片段(省去无关代码)

WritePhraseWordsUtil

public class WritePhraseWordsUtil {
    /**
     *
     * @param file file需要传入文件的全路径
     */
    public static List<PhraseWordListEntity> readTxt(File file) throws IOException {
        String s;
        List<PhraseWordListEntity> listEntities = new ArrayList<>();
        /*
       以文件流的形式逐行读取txt文件(文件存储编码需要utf-8格式,不然会乱码)
        */
        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(调用方法)

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设置的字段

CustomCorePropertiesConfig

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

}

CustomCoreProperties

@ConfigurationProperties(prefix = "custom")
public class CustomCoreProperties {
    /**
     * 分词相关
     */
    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(转换后的实体类)

public class PhraseWordListEntity {

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

    private String word;

    private Integer weight;
    /**
     * 词性:n, a……
     */
    @Column(name = "charactertype")
    private String characterType;
    /**
     * 修改时间
     */
    @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;
    }
}


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容