首页 > 其他 > 详细

文件处理

时间:2019-10-21 18:50:48      阅读:92      评论:0      收藏:0      [点我收藏+]
package com.along.gps.util;

import com.alibaba.fastjson.JSONObject;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class fileUtil {

    public static void write(){
        Writer w = null;
        BufferedWriter bw = null;
        long startTime = System.currentTimeMillis();
        try {
            // 写入文本
            File f = new File("E:\\gpsData\\demo1-json.txt");

            if (!f.exists()) {
                f.createNewFile();
            }



            w = new FileWriter(f, true);
            bw = new BufferedWriter(w);
            for (int i = 0; i <1000000 ; i++) {
                bw.write(" -------------------demo------------------------------");
            }

            long endTime = System.currentTimeMillis();
            System.out.println("运行时间:" + (endTime - startTime) + "ms");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                bw.close();
                w.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    }

    public static List<String> read(String url){
        BufferedInputStream fis =null;
        BufferedReader reader = null;
        List<String> list=new ArrayList<>();
        try {
            File file = new File(url);
            fis = new BufferedInputStream(new FileInputStream(file));
            reader = new BufferedReader(new InputStreamReader(fis,"utf-8"),5*1024*1024);// 用5M的缓冲读取文本文件

            String line = "";
            int i=0;
            long startTime = System.currentTimeMillis();
        //    while((line = reader.readLine()) != null ){
            while( reader.ready()){
                //TODO: write your business
                //    JSONObject.toJSONString(line);

                JSONObject jsonObject = JSONObject.parseObject(line);

                i++;
                //System.out.println(i++);
                list.add(line);
            }
            long endTime = System.currentTimeMillis();
            System.out.println(i/10000.0+" w条数据   readTxt1方法,使用内存="+(Runtime.getRuntime().totalMemory()-Runtime.getRuntime().freeMemory())/ 1024 / 1024 + "M"+",使用时间毫秒="+(endTime-startTime));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                fis.close();
                reader.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
        return list;
    }
    public static void main(String args[]) {

        read("E:\\gpsData\\demo.txt");


    }

    /**
     * 按行分割文件
     * @param sourceFilePath 为源文件路径
     * @param targetDirectoryPath 文件分割后存放的目标目录
     * @param rows 为多少行一个文件
     */
    public static int splitFileByLine(String sourceFilePath, String targetDirectoryPath, int rows) {
        long startTime = System.currentTimeMillis();
        String sourceFileName = sourceFilePath.substring(sourceFilePath.lastIndexOf(File.separator) + 1, sourceFilePath.lastIndexOf("."));//源文件名
        String splitFileName = targetDirectoryPath + File.separator + sourceFileName + "-demo-%s.txt";//切割后的文件名
        File targetDirectory = new File(targetDirectoryPath);
        if (!targetDirectory.exists()) {
            targetDirectory.mkdirs();
        }

        PrintWriter pw = null;//字符输出流
        String tempLine;
        int lineNum = 0;//本次行数累计 , 达到rows开辟新文件
        int splitFileIndex = 1;//当前文件索引

        try (BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(sourceFilePath)))) {
            pw = new PrintWriter(String.format(splitFileName, splitFileIndex));
            while ((tempLine = br.readLine()) != null) {
                if (lineNum > 0 && lineNum % rows == 0) {//需要换新文件
                    pw.flush();
                    pw.close();
                    pw = new PrintWriter(String.format(splitFileName , ++splitFileIndex));
                }
                pw.write(tempLine + "\n");
                lineNum++;
            }
            long endTime = System.currentTimeMillis();
            System.out.println(splitFileIndex+"运行时间:" + (endTime - startTime) + "ms");
            return splitFileIndex;
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }finally {
            if (null != pw) {
                pw.flush();
                pw.close();
            }
        }

    }

}

 

文件处理

原文:https://www.cnblogs.com/why5125/p/11715100.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!