zoukankan      html  css  js  c++  java
  • Java(springboot) 读取txt文本内容代码实例

    这篇文章主要介绍了Java(springboot) 读取txt文本内容代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

    代码如下

    public class TxtTest {
      private static final Logger logger = LoggerFactory.getLogger(TxtTest.class);
      public static String readTxt(File file) throws IOException {
        String s = "";
        InputStreamReader in = new InputStreamReader(new FileInputStream(file),"UTF-8");
        BufferedReader br = new BufferedReader(in);
        StringBuffer content = new StringBuffer();
        while ((s=br.readLine())!=null){
          content = content.append(s);
        }
        return content.toString();
      }
     
      public static void main(String[] args) {
        try {
            //通过绝对路径获取文件
          String s1 = TxtTest.readTxt(new File("C:\Users\....\du.txt"));
          logger.info(s1);
     
            //spring boot中文件直接放在resources目录下
          String s2 = TxtTest.readTxt(ResourceUtils.getFile("classpath:du.txt"));
          logger.info(s2);
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }

     以上代码如果部署在linux服务器上,就找不到SpringBoot项目resources根目录下的du.txt文件。所以需要修改成如下方式:

    import com.huixiaoer.joy.api.common.config.EnvConfig;
    import com.huixiaoer.joy.api.service.FileService;
    import lombok.extern.slf4j.Slf4j;
    import org.apache.commons.io.IOUtils;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.core.io.ClassPathResource;
    import org.springframework.stereotype.Service;
    import org.springframework.util.ResourceUtils;
    
    import java.io.*;
    
    
    @Slf4j
    @Service
    public class FileServiceImpl implements FileService {
    
        @Override
        public String readTxtFile() {
            try {
                byte[] bytes;
    
                ClassPathResource classPathResource = new ClassPathResource("file/du.txt");
                //获取文件流
                InputStream keyStream = classPathResource.getInputStream();
                bytes = IOUtils.toByteArray(keyStream);
                keyStream.read(bytes);
                keyStream.close();
    
                ByteArrayInputStream certBis = new ByteArrayInputStream(bytes);
                InputStreamReader input = new InputStreamReader(certBis);
                BufferedReader bf = new BufferedReader(input);
                String line = null;
                StringBuilder sb = new StringBuilder();
                while((line=bf.readLine()) != null){
                    sb.append(line);
                }
    
                return sb.toString();
            } catch (IOException e) {
                log.error("读取文件错误",e);
            }
            return "";
        }
    
    }

    转载于:https://www.jb51.net/article/180411.htm

  • 相关阅读:
    New Audio Codec (3) : Design of a Scalable Parametric Audio Coder(可分级正弦模型)
    英国旅游庄园酒店
    圣塔芭芭拉加州大学 信号压缩实验室
    mptkcodec工程(二):VS2008+Win7 编译 mptkcodec(下)
    SPIHT 编码原理,代码,应用,专利问题
    Audio Bandwidth Extension 技术主页
    【quote】free HRTF Databases available online
    New Audio Codec (4) : Daryl Ning 的 Warped LPC and Wavelet Audio Coding 方案
    mptkcodec工程(二):VS2008+Win7 编译 mptkcodec(上)
    mptkcodec工程(一):Cygwin+Win7 编译 mptkcodec
  • 原文地址:https://www.cnblogs.com/it-deepinmind/p/13571764.html
Copyright © 2011-2022 走看看