spring 读写本地文件

package cn.java.receive.service;

import org.springframework.beans.factory.annotation.Value;

import org.springframework.core.io.FileSystemResource;
import org.springframework.stereotype.Component;

import java.io.*;

@Component
public class FileOperater {

    @Value("${token.path}")
    private String tokenPath;

    public String getfileinfo() {
        String rstr = "";

        try {
            FileSystemResource resource = new FileSystemResource(tokenPath);
            BufferedReader br = new BufferedReader(new FileReader(resource.getFile()));
            String str = null;
            while ((str = br.readLine()) != null) {
                rstr += str;
            }
            br.close();
        } catch (IOException e) {
            //todo loginfo
        }
        return rstr;
    }

    public Boolean writefileinfo(String t) {
        FileSystemResource resource = new FileSystemResource(tokenPath);
        try {
            FileWriter fileWriter = (new FileWriter(resource.getFile()));
            fileWriter.write(t);
            fileWriter.close();
        } catch (IOException e) {
            //todo loginfo
            return false;
        }
        return true;
    }
}

创建配置文件application.properties
token.path=/n/t/tke.txt

猜你喜欢

转载自blog.csdn.net/qq_38572383/article/details/81977974