欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

文件操作工具类---使用IO流向磁盘写.txt文件和读.txt文件

程序员文章站 2024-01-20 13:40:34
...

文件操作工具类FileUtil

package cn.lf.day0901;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;

/**
 * @author lf
 * @date 2017年9月1日
 * @discribe 文件操作工具类
 *
 */

public class FileUtil {
    /**
     * @function 该方法用文件字符内容写入(不征对文字以外的文件)
     * @param file 操作的文件
     * @param content 要写入的内容
     * @param charset 设置编码
     * @param return true写入成功,false写入失败
     * @throws UnsupportedEncodingException,FileNotFoundException,IOException
     * 
     */
    //往文件内写数据
    public static boolean writeFile(File file,String content,String charset) throws UnsupportedEncodingException{
        //判断file是否为一个文件类型,并且file存在
        if (file.isFile() && file.exists()) {
            PrintWriter out =null;
            try {
                out = new PrintWriter(file,charset);
                //根据“\n”切割字符串str
                String[] str = content.split("\n");
                for(String s: str){ //遍历输出被切割的字符串
                    out.println(s); 
                }
            } catch (UnsupportedEncodingException e) {
                System.out.println("在文件中写入内容,不支持编码集!");
                return false;
            } catch (FileNotFoundException e) {
                System.out.println("在文件中写入内容,文件不存在!");
                return false;
            }finally{
                out.flush(); 
                out.close();
            }
        }else {
            return false;
        }
        return true;
    }


    /**
     * @function 读取文件中的内容,仅用于文字读取
     * @param file 被读取的文件
     * @param charset 设置编码
     * @return String,读取到的内容,若读取失败返回null
     * @throws UnsupportedEncodingException,FileNotFoundException,IOException 
     */
    public static String readFile(File file,String charset) {
        String contents ="";
        if (file.isFile() && file.exists()) {
            BufferedReader in = null;
            try {
                in = new BufferedReader(
                        new InputStreamReader(
                                new FileInputStream(file), charset));
                String content = null;
                //整行读取数据,当不为空时,追加
                while((content = in.readLine()) != null){
                    contents += content+"\n";
                }
            } catch (UnsupportedEncodingException e) {
                System.out.println("在文件中读取内容,不支持编码集!");
                contents = null;
            } catch (FileNotFoundException e) {
                System.out.println("在文件中读取内容,文件不存在!");
                contents = null;
            }catch (IOException e) {
                System.out.println("读取文件的过程中IO异常!");
            }
            finally {
                try {
                    in.close();
                } catch (IOException e) {
                    System.out.println("读取完毕,关闭流的过程中出现异常!");
                    return contents;
                }
            }
        }else {
            contents = null;
        }
        return contents;
    }
}

使用FileUtil类,实现文件的复制

package cn.lf.day0901;

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

import org.junit.Test;

public class FileUtilTest {
    /**
     * 文件复制
     * @throws IOException 
     */

    @Test
    public void test() throws IOException{
        //源文件目录
        File file_resource = new File("./test/a.txt");
        //
        File file_copy = new File("./test/c_copy.txt");
        //判断file_copy文件是否存在,不存在则创建
        if (!file_copy.exists()) {
            file_copy.createNewFile();
        }
        //源文件是什么编码格式,复制文件就是什么编码格式(否则会出现乱码)
        String content = FileUtil.readFile(file_resource,"GB2312");
        boolean bool = FileUtil.writeFile(file_copy, content,"GB2312");
        if (bool == true) {
            System.out.println("复制成功!");
        }else {
            System.out.println("复制失败!");
        }

    }
}

运行结果:
文件操作工具类---使用IO流向磁盘写.txt文件和读.txt文件
文件操作工具类---使用IO流向磁盘写.txt文件和读.txt文件
文件操作工具类---使用IO流向磁盘写.txt文件和读.txt文件

相关标签: 文件读写