Java IO(1) --文件读写

Talk is cheap, show you the code.

一、 读文件

/**
 * 读取文本文件
 * 在文件编码与系统默认编码相同时,可使用FileReader,这里我们使用FileInputStream方便我们指定编码
 * @param filename 文件名
 * @param charset 文件编码
 * @return 文件内容
 * @throws IOException
 */
public static String readFile(String filename, String charset) throws IOException {
    File file = new File(filename);
    if (!file.exists() || file.isDirectory()) {
        throw new FileNotFoundException(String.format("文件%s不存在", filename));
    }
    try (FileInputStream inputStream = new FileInputStream(file)) {

        byte[] bytes = new byte[1024];
        StringBuilder builder = new StringBuilder();
        while (inputStream.read(bytes) > 0) {
            builder.append(new String(bytes, charset));
        }
        return builder.toString();
    }
}

二、写文件

/**
 * 将指定内容写入文件
 * @param filename 文件名
 * @param content 即将写入文件的内容
 * @param charset 字符编码
 * @return 
 * @throws IOException
 */
public static boolean writeFile(String filename, String content, String charset) throws IOException {
    try (FileOutputStream outputStream = new FileOutputStream(filename)) {
        byte[] bytes = content.getBytes(charset);
        for (Byte b : bytes) {
            outputStream.write(b);
        }
    }
    return true;
}

三、完整代码

import java.io.*;

public class FileTest {
    /**
     * 读取文本文件
     * @param filename 文件名
     * @param charset 文件编码
     * @return 文件内容
     * @throws IOException
     */
    public static String readFile(String filename, String charset) throws IOException {
        File file = new File(filename);
        if (!file.exists() || file.isDirectory()) {
            throw new FileNotFoundException(String.format("文件%s不存在", filename));
        }
        try (FileInputStream inputStream = new FileInputStream(file)) {

            byte[] bytes = new byte[1024];
            StringBuilder builder = new StringBuilder();
            while (inputStream.read(bytes) > 0) {
                builder.append(new String(bytes, charset));
            }
            return builder.toString();
        }
    }

    /**
     * 将指定内容写入文件
     * @param filename 文件名
     * @param content 即将写入文件的内容
     * @param charset 字符编码
     * @return
     * @throws IOException
     */
    public static boolean writeFile(String filename, String content, String charset) throws IOException {
        try (FileOutputStream outputStream = new FileOutputStream(filename)) {
            byte[] bytes = content.getBytes(charset);
            for (Byte b : bytes) {
                outputStream.write(b);
            }
        }
        return true;
    }


    public static void main(String[] args) throws IOException {
        String content = "锄禾日当午,汗滴禾下土。谁知盘中餐,粒粒皆辛苦。";
        System.out.println(String.format("写入文件:%s", writeFile("out.txt", content, "UTF-8")));
        content = readFile("out.txt", "UTF-8");
        System.out.println(content);
        // 测试文件编码转换
        System.out.println(writeFile("out.txt", content, "GBK"));
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值