Java创建文件并向文件写入内容:I/0流简单操作

2 篇文章 0 订阅
1 篇文章 0 订阅
package com.lijy.util;


import org.junit.Test;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileUtilTest {

    /** logger */
     protected static Logger log = Logger.getLogger(FileUtilTest.class);

    @Test
    public void testWriteFile() throws IOException {
        String path = "D:\\settledown";
        String fileName = "test.txt";
        String content = "Hello World! 你好,世界";
        String charset = "GBK";
        String filePath = writeFile(path, fileName, content, charset);
        System.out.println(filePath);
    }

    @Test
    public void testRead(){
        File file = new File("D:\\settledown\\test.txt");
        String str = read(file);
        System.out.println(str);
    }

    /**
     * 读文件
     *
     * @param file
     * @return
     */
    public static String read(File file) {
        StringBuffer contents = new StringBuffer();
        BufferedReader br = null;
        try {
            br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
        } catch (FileNotFoundException e) {
            log.info("下载文件失败");
            e.printStackTrace();
        }
        try {
            log.info("文件路径" + file.getAbsolutePath());
            String line = null;
            while ((line = br.readLine()) != null) {
                contents.append(line).append("\r\n");
            }
            br.close();
            log.info("文件内容" + contents.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return contents.toString();
    }

    /**
     *
     * @param path 自定义文件路径
     * @param fileName 自定义文件名
     * @param content 组装文件内容
     * @param charset 文件编码方式
     * @return
     */
    public static String writeFile(String path, String fileName, String content, String charset) throws IOException {
        mkDir(path);
        String filePath = path + File.separator + fileName;//D:\settledown\test.txt
        File file = new File(filePath);

        if (file.exists()) {//如果文件存在,那么删除
            file.delete();
        }
        file.createNewFile();//创建文件

        FileOutputStream fops = new FileOutputStream(file);

        fops.write((content).getBytes(charset));

        fops.flush();
        fops.close();
        return filePath;
    }

    /**
     * @param path 路径
     */
    public static void mkDir(String path) {
        File file = new File(path);
        if (!file.exists()) {//如果目录不存在
            file.mkdirs();
        }
    }
    /**性能上没有read(File file)方法好*/
    public static String readFile(String filePath) throws IOException
    {
        File file = new File(filePath);

        FileInputStream fin = new FileInputStream(file);

        ByteArrayOutputStream bs = new ByteArrayOutputStream();

        while (true)
        {
            int read = fin.read();
            if (read == -1)
            {
                break;
            }
            else
            {
                bs.write(read);
            }
        }

        String content = new String(bs.toByteArray(), "UTF-8");

        fin.close();
        bs.close();
        return content;
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值