JAVA按行读取txt文件

    /**
     * 读取文件内容
     * 按行读取
     * @param fileName
     * @return
     */
    public static List<String> readLineFile(String fileName){
        List<String> list=new ArrayList<>();

        File file=new File(fileName);
        if(!file.exists()){
            log.error("文件不存在,文件全路径[{}]",fileName);
            return null;
        }
        if(!getFileCharsetName(file).equals("GBK")){
         log.error("文件编码格式不正确");
          return null;
        }
        InputStreamReader read=null;
        BufferedReader bufferedReader=null;
        try{
            FileInputStream inputStream=new FileInputStream(file);
            read=new InputStreamReader(inputStream,"GBK");
            bufferedReader=new BufferedReader(read);
            String lineTxt=null;
            while ((lineTxt=bufferedReader.readLine())!=null){
                list.add(lineTxt);
            }
        }catch (Exception e){
            log.error("读取文件内容异常:",e);
              return null;
        }finally {
            try{
                if(bufferedReader!=null){
                    bufferedReader.close();
                }
                if(read!=null){
                    read.close();
                }
            }catch (Exception e){
                log.error("读取文件流关闭异常:",e);
                  return null;
            }
        }
        return list;

    }

获取文件字符集

    /**
     * 获取文件字符集
     * @param file
     * @return
     */
    public static String getFileCharsetName(File file){
        CodepageDetectorProxy detector = CodepageDetectorProxy.getInstance();
        detector.add(new ParsingDetector(false));
        detector.add(JChardetFacade.getInstance());
        detector.add(ASCIIDetector.getInstance());
        detector.add(UnicodeDetector.getInstance());
        Charset charset = null;
        try{
            charset = detector.detectCodepage(file.toURI().toURL());
        }catch (Exception e){
            log.error("获取文件字符集失败。",e);
             return null;
        }
        String charsetName = "GBK";
        if(charset.name().equals("US-ASCII")){
            charsetName="ISO-8859-1";
        }else if(charset.name().equals("GB2312")){
            return charsetName;
        }else{
            charsetName =  charset.name();
        }
        return charsetName;
    }

获取文件字符集需引入jar包

        <dependency>
            <groupId>cpdetector</groupId>
            <artifactId>cpdetector</artifactId>
            <version>1.0.7</version>
        </dependency>
        <dependency>
            <groupId>net.sourceforge.jchardet</groupId>
            <artifactId>jchardet</artifactId>
            <version>1.0</version>
        </dependency>

移动文件

 /**
     * 移动文件
     * @param oldPath 移动制文件全路径
     * @param newPath  目标文件全路径。
     * @return
     */
    public static boolean  fileMove(String oldPath,String newPath){
        File sourceFile = new File(oldPath);
        File targetFile = new File(newPath);
        if(targetFile.exists()){
            targetFile.delete();
        }
        Path source = sourceFile.toPath();
        Path target = targetFile.toPath();
        try {
            Files.move(source,target);
        }catch (Exception e){
            log.error("文件移动失败",e);
            return  false;
        }
        return true;
    }

复制文件

 /**
     * 复制文件
     * @param oldPath 需复制文件全路径
     * @param newPath  目标文件全路径。
     * @return
     */
    public static boolean  fileCopy(String oldPath,String newPath){
       File sourceFile = new File(oldPath);
       File targetFile = new File(newPath);
       if(targetFile.exists()){
           targetFile.delete();
       }
        Path source = sourceFile.toPath();
        Path target = targetFile.toPath();
        try {
            Files.copy(source,target);
        }catch (Exception e){
            log.error("文件复制失败",e);
            return  false;
        }
        return true;
    }
  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值