Java-文件操作|read|write

package com.bi.mr_example;

import java.io.*;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Scanner;

/**
 * Created by gongzi on 2016/1/12.
 */
public class JavaReadFile {

    private static void readUsingFileReader(String fileName) throws IOException {
        File file = new File(fileName);
        FileReader fr = new FileReader(file);
        BufferedReader br = new BufferedReader(fr);
        String line;
        while ((line = br.readLine()) != null) {
            System.out.println(line);
        }
        br.close();
        fr.close();
    }

    private static void readUsingBufferedReader(String fileName, Charset cs) throws IOException {
        File file = new File(fileName);
        FileInputStream fis = new FileInputStream(file);
        InputStreamReader isr = new InputStreamReader(fis, cs);
        BufferedReader br = new BufferedReader(isr);
        String line;
        while ((line = br.readLine()) != null) {
            System.out.println(line);
        }
        br.close();
    }

    private static void readUsingBufferedReaderJava7(String fileName, Charset cs) throws IOException {
        Path path = Paths.get(fileName);
        BufferedReader br = Files.newBufferedReader(path, cs);
        String line;
        while ((line = br.readLine()) != null) {
            System.out.println(line);
        }
        br.close();
    }

    private static void readUsingBufferedReader(String fileName) throws IOException {
        File file = new File(fileName);
        FileReader fr = new FileReader(file);
        BufferedReader br = new BufferedReader(fr);
        String line;
        while ((line = br.readLine()) != null) {
            System.out.println(line);
        }
        br.close();
        fr.close();
    }

    private static void readUsingScanner(String fileName) throws IOException {
        Path path = Paths.get(fileName);
        Scanner scanner = new Scanner(path);
        while (scanner.hasNextLine()) {
            String line = scanner.nextLine();
            System.out.println(line);
        }
    }

    /**
     * read  file to String list
     * @param fileName
     * @throws IOException
     */
    private static void readUsingFiles(String fileName) throws IOException {
        Path path = Paths.get(fileName);
        //read  file to byte array
        byte[] bytes = Files.readAllBytes(path);
        List allLines = Files.readAllLines(path, StandardCharsets.UTF_8);
    }

    public static void main(String[] args) throws IOException {
        String fileName = "E:\\gongzi_test.txt";
        //using Java 7 Files class to process small files, get complete file data
        System.out.println("-------- readUsingFiles ----------");
        readUsingFiles(fileName);
        //using Scanner class for large files, to read  line by line
        System.out.println("-------- readUsingScanner ----------");
        readUsingScanner(fileName);
        //read  using BufferedReader , to read  line by line
        System.out.println("-------- readUsingBufferedReader ----------");
        readUsingBufferedReader(fileName);

        System.out.println("-------- readUsingBufferedReaderJava7 ----------");
        readUsingBufferedReaderJava7(fileName, StandardCharsets.UTF_8);

        System.out.println("-------- readUsingBufferedReader ----------");
        readUsingBufferedReader(fileName, StandardCharsets.UTF_8);
        //read  using FileReader , no encoding support, not efficient

        System.out.println("-------- readUsingFileReader ----------");
        readUsingFileReader(fileName);
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值