java文本文件读写操作

FileOpUtil.java
package com.qiaojun;

import com.jmt.alipay.property.SystemProps;
import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;

/**
 * 文件读写工具类
 * @author qiaojun
 * @date 2019/4/3
 */
@Slf4j
public class FileOpUtil {

    /**
     * 下载文件到指定位置
     * @param downLoadUrl 下载链接
     * @param saveUrl   保证地址
     */
    public static void downLoadBillData(String downLoadUrl,String saveUrl){
        try {
            InputStream in = new URL(downLoadUrl).openConnection().getInputStream();
            OutputStream ou = new FileOutputStream(saveUrl);

            int c = 0;
            int k = 0;
            float m = 0;
            int bread = 0;
            byte[] byt = new byte[2048];
            while ((bread = in.read(byt, 0, 2048)) != -1) {
                ou.write(byt, 0, bread);
                c = c + bread;
                k = c / 1024;
                m = k / 1024;
                System.out.println("己下载: " + k + "k " + m + "M");
            }
            in.close();
            ou.close();
        } catch (Exception e) {
            e.printStackTrace();
            log.info("------------下载文件出错------------------");
            //发邮件告警
            MailUtl.sendSimpleMail("下载文件出错-"+ SystemProps.CURRENT_ENV,"异常信息:\n"+e.getMessage(),null,null);
        }
    }

    /**
     * 读取压缩包里面的文本文件
     */
    public static String readZipBillFile(String file) throws IOException {
        ZipFile zf = new ZipFile(file,Charset.forName("GBK"));
        InputStream in = new BufferedInputStream(new FileInputStream(file));
        ZipInputStream zin = new ZipInputStream(in,Charset.forName("GBK"));
        ZipEntry ze;
        StringBuilder resultStr = new StringBuilder();
        while ((ze = zin.getNextEntry()) != null) {
            //如果不是目录才处理
            if (!ze.isDirectory()) {
//                System.out.println("file - " + ze.getName() + " : "+ ze.getSize() + " bytes");
               
                System.out.println("文件名: " +ze.getName());
                long size = ze.getSize();
                if (size > 0) {
                    BufferedReader br = new BufferedReader(
                            new InputStreamReader(zf.getInputStream(ze),Charset.forName("GBK")));

                    String line;
                    boolean flag = false;
                    while ((line = br.readLine()) != null) {
                        resultStr.append(line).append("\n");
                    }
                    br.close();
                }
            }
        }
        zin.closeEntry();
        in.close();
        zf.close();
        return resultStr.toString();
    }

    /**
     * 写入文本文件内容
     * @param filePath 写入地址
     * @param content 写入内容
     */
    public static void writeJSONToFileTxt(String filePath,String content){
        FileOutputStream out;
        PrintStream p;
        try {
            out = new FileOutputStream(filePath);
            p = new PrintStream(out);
            p.println(content);
            p.close();
            out.close();
        } catch (Exception e) {
            log.error("-----写入文本文件错误filePath:"+filePath+";异常信息:"+e.getMessage(),e);
        }
    }

    /**
     * 读取文件内容
     * @param filePath 文件地址
     * @return 内容
     */
    public static String readTxtFile(String filePath) {
        StringBuilder line = new StringBuilder();
        try {
            InputStream in = new BufferedInputStream(new FileInputStream(filePath));
            BufferedReader br = new BufferedReader(
                    new InputStreamReader(in, Charset.forName("UTF-8")));
            String str;
            while ((str = br.readLine()) != null) {
                line.append(str);
            }
            in.close();
            br.close();
        } catch (Exception e) {
            log.error("-----读取文本文件错误---:"+e.getMessage(),e);
        }
        return line.toString();
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值