Java中字符流和字节流读写

Zeran

Java中读取文件中的内容的几种方式如下:

读取文件

字符流读写

public class CharacterIoUtil {
    private static final Logger log = Logger.getLogger("com.yzr.io.CharacterIoUtil");
    private static Integer len;
    private static final char[] BYTES = new char[1024];
    private static boolean result = false;
    private static String stringContent = null;

    /**
     * 将字符串写入文件
     *
     * @param character 字符串
     * @param filePath  文件绝对路径
     * @return 是否成功
     */
    public static boolean characterWrite(String character, String filePath) {
        try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(filePath))) {
            if (character.length() > 0) {
                bufferedWriter.write(character);
                result = true;
            }
        } catch (Exception e) {
            log.info("错误...");
        }
        return result;
    }

    /**
     * 从一个文件写入到另一个文件
     *
     * @param fromPath 原文件
     * @param toPath   目标文件
     */
    public static boolean fileWrite(String fromPath, String toPath) {
        try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(toPath));
             BufferedReader bufferedReader = new BufferedReader(new FileReader(fromPath))) {
            while ((len = bufferedReader.read(BYTES)) != -1) {
                bufferedWriter.write(BYTES);
                result = true;
            }
        } catch (Exception e) {
            log.info("错误...");
        }
        return result;
    }

    /**
     * 读取文件中内容,以字符串的形式返回
     *
     * @param filePath 文件路径
     * @return 读取的文件内容信息
     */
    public static String fileRead(String filePath) {
        try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
            while ((len = reader.read(BYTES)) != -1) {
                stringContent = new String(BYTES, 0, len);
            }
        } catch (Exception e) {
            log.info("错误...");
        }
        return stringContent;
    }

    /**
     * 读取文件中内容,以字符串的形式返回
     *
     * @param stream 流对象
     * @return 读取的文件内容信息
     */
    public static String readByStream(InputStream stream) {
        try (InputStreamReader reader = new InputStreamReader(stream, "GBK")) {
            while ((len = reader.read(BYTES)) != -1) {
                stringContent = new String(BYTES, 0, len);
            }
        } catch (Exception e) {
            log.info("错误...");
        }
        return stringContent;
    }

    /**
     * 将流中的字符写入文件
     *
     * @param output 写流对象
     * @param input  读流对象
     */
    public static boolean writeByStream(InputStream input, OutputStream output) {
        try (OutputStreamWriter writer = new OutputStreamWriter(output, "GBK");
             InputStreamReader reader = new InputStreamReader(input, "GBK")) {
            while ((len = reader.read(BYTES)) != -1) {
                writer.write(BYTES);
                result = true;
            }
        } catch (Exception e) {
            log.info("错误...");
        }
        return result;
    }

}

字节流读写

public class ByteStreamUtil {
    private static final Logger log = Logger.getLogger("com.yzr.io.CharacterIoUtil");
    private static int len;
    private static final byte[] BYTES = new byte[2048];
    private static boolean result = false;
    private static String stringResult = null;

    /**
     * 读取流
     *
     * @param file 文件对象
     * @return 字符串
     */
    public static String fileIn(File file) {
        try (BufferedInputStream stream = new BufferedInputStream(new FileInputStream(file))) {
            if (!file.exists()) {
                throw new IOException("此文件不存在");
            }
            while ((len = stream.read(BYTES)) != -1) {
                stringResult = new String(BYTES, 0, len, "GBK");
            }
        } catch (Exception e) {
            log.info("错误...");
        }
        return stringResult;
    }

    /**
     * 写入流
     *
     * @param file    文件对象
     * @param content 内容
     * @return 是否成功
     */
    public static boolean fileOut(File file, String content) {
        try (BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(file))) {
            if (!file.exists()) {
                throw new IOException("此文件不存在");
            }
            if (content.length() > 0) {
                stream.write(content.getBytes());
                result = true;
            }
        } catch (Exception e) {
            log.info("错误...");
        }
        return result;
    }

    /**
     * 写入流
     *
     * @param in  读取流对象
     * @param out 写入流对象
     * @return 是否成功
     */
    public static boolean fileCopy(InputStream in, OutputStream out) {
        try (BufferedOutputStream outputStream = new BufferedOutputStream(out);
             BufferedInputStream inputStream = new BufferedInputStream(in)) {
            while ((len = inputStream.read(BYTES)) != -1) {
                outputStream.write(BYTES, 0, len);
                result = true;
            }
        } catch (Exception e) {
            log.info("错误...");
        }
        return result;
    }

}

以上内容仅供参考

阅读 912

学而不思则罔,思而不学则殆。

13 声望
2 粉丝
0 条评论

学而不思则罔,思而不学则殆。

13 声望
2 粉丝
文章目录
宣传栏