Java:文件写入读取操作和工具类

文件读写示例

package com.demo;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class FileUtil {

    public static void main(String[] args) throws IOException {
        // 覆盖写入
        PrintWriter writer = new PrintWriter(new FileWriter("data.txt"));
        writer.println("hello");
        writer.println("world");
        writer.close();

        // 追加写入
        PrintWriter appendWriter = new PrintWriter(new FileWriter("data.txt", true));
        appendWriter.println("hello");
        appendWriter.println("world");
        appendWriter.close();

        // 按行读取
        List<String> list = new ArrayList<>();

        Scanner scanner = new Scanner(new FileReader("data.txt"));
        while (scanner.hasNextLine()) {
            String line = scanner.nextLine();
            list.add(line);
        }
        scanner.close();

        System.out.println(list);
        // [hello, world, hello, world]

    }


}

工具类封装

写入

package com.demo.fileutil;

import java.io.IOException;
import java.io.PrintWriter;

public class FileWriter {

    private PrintWriter writer;

    public FileWriter(String fileName) throws IOException {
        this(fileName, false);
    }

    public FileWriter(String fileName, boolean append) throws IOException {
        this.writer = new PrintWriter(new java.io.FileWriter(fileName, append));
    }

    public void println(String content){
        this.writer.println(content);
    }

    public void print(String content){
        this.writer.print(content);
    }

    public void close(){
        this.writer.close();
    }
}

读取

package com.demo.fileutil;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class FileReader {
    private String fileName;

    public FileReader(String fileName) {
        this.fileName = fileName;
    }

    public List<String> readLines() throws FileNotFoundException {

        List<String> list = new ArrayList<>();

        Scanner scanner = new Scanner(new java.io.FileReader(this.fileName));
        while (scanner.hasNextLine()) {
            String line = scanner.nextLine();
            list.add(line);
        }
        scanner.close();

        return list;
    }

    public String readAll() throws IOException {

        StringBuilder sb = new StringBuilder();

        java.io.FileReader reader = new java.io.FileReader(this.fileName);

        int len = 0;
        char[] data = new char[1024];
        while ((len = reader.read(data)) != -1) {
            sb.append(data, 0, len);
        }

        reader.close();

        return sb.toString();
    }
}

测试

package com.demo;

import com.demo.fileutil.FileReader;
import com.demo.fileutil.FileWriter;

import java.io.IOException;
import java.util.List;

public class TestFileUtil {
    public static void main(String[] args) throws IOException {
        FileWriter writer = new FileWriter("data.txt", true);
        writer.println("hello");
        writer.println("world");
        writer.close();

        FileReader reader = new FileReader("data.txt");
        String content = reader.readAll();
        System.out.print(content);
        // hello
        // world

        List<String> list = reader.readLines();
        System.out.println(list);
        // [hello, world]


    }
}

©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页