java读写文件的几种方式,方便日后使用

1、递归获取某文件夹下所有文件路径

/**
     * 获取文件夹下所有文件的路径
     * @param folder
     * @return
     */
    public static void getFilePath(String folder){

        File files = new File(folder);
        if(files.isFile()){
            String parent = files.getParent();
            int i = parent.lastIndexOf("\\");
            if("url_worlds".equals(parent.substring(i+1))) {
                pathList.add(folder);
            }
        }else {
            File[] files1 = files.listFiles();
            for(File file : files1){
                getFilePath(file.getPath());
            }
        }
    }

2、读文件的几种方式

2.1、读取小文件

 

public static List<String> readFiles(String filePath){
        List<String> textList = new ArrayList<String>();
        try {
            String encoding="utf-8";
            File file=new File(filePath);
            if(file.isFile() && file.exists()){
                InputStreamReader read = new InputStreamReader(
                        new FileInputStream(file),encoding);
                BufferedReader bufferedReader = new BufferedReader(read);
                String lineTxt = null;
                while((lineTxt = bufferedReader.readLine()) != null){
                    textList.add(lineTxt);
                }
                read.close();
            }else{
                System.out.println("文件不存在");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return textList;
    }

 

 

2.2 读取大文件(G级以上)

使用 RandomAccessFile
public static void largeRead(String inFile, String outFile){
        RandomAccessFile read = null;
        try {
            read = new RandomAccessFile(inFile,"r");
            RandomAccessFile writer = new RandomAccessFile(outFile,"rw");
            byte[] b = new byte[200*1024*1024];
            while(read.read(b)!=-1){

                writer.write(b);
            }
            writer.close();
            read.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }catch (IOException e){
            e.printStackTrace();
        }
    }


 使用LineIterator

public static void largeRead1(String inFile, String outFile){

        try {
            LineIterator li = FileUtils.lineIterator(new File(inFile), "utf-8");
            FileWriter fw = new FileWriter(outFile,true);
            BufferedWriter bw = new BufferedWriter(fw);
            while (li.hasNext()){
                String line = li.nextLine();
                String[] split = line.split("\t");
                if(split.length > 1){
                    //写入outFile
                    bw.write(line);
                    bw.newLine();
                    bw.flush();
                    count++;
                }
            }
            bw.close();
            fw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
        }
    }


二、写文件

常用方法:

 

public static void writeFile(String destinationPath, String line) throws Exception {
        FileWriter fileWriter = new FileWriter(destinationPath,true);
        BufferedWriter writer = new BufferedWriter(fileWriter);
        writer.write(line);
        writer.newLine();
        writer.flush();

        writer.close();
        fileWriter.close();
    }

 

 

三、多个文件合并为一个文件

3.1 普通方式(小文件)

先读出文件的内容,然后以追加的方式写入新的文件中,结合以上的读和写即可实现。
 

3.2 快速合并

 

public static void fastMerge(String outFile){
        FileChannel outChannel = null;
        try {
            outChannel = new FileOutputStream(outFile).getChannel();
            for (String f : pathList){
                System.out.println("Merge " + f + " into " + outFile);
                FileChannel fc = new FileInputStream(f).getChannel();
                ByteBuffer bb = ByteBuffer.allocate(BUFSIZE);
                while (fc.read(bb) != -1){
                    bb.flip();
                    outChannel.write(bb);
                    bb.clear();
                    count++;
                }
            }
        } catch (IOException e){
            e.printStackTrace();
        }finally {
            try {
                if(outChannel != null){
                    outChannel.close();
                }
            }catch (IOException e){
                e.printStackTrace();
            }
        }
    }

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值