编程语言
首页 > 编程语言> > java文本文件读写操作

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();
    }
}

 

标签:java,String,读写操作,ze,filePath,文本文件,new,close,import
来源: https://blog.csdn.net/qq_37519791/article/details/99743859