IO流工具类

16 篇文章 0 订阅
package com.lt.an20_utils;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;

/**
 * Created by 风情万种冷哥哥 on 2016.
 *
 */
public class IOHelper {

    /**
     * 读取文本文件,返回String
     * @param filePath
     * @return
     */
    public static String readTextFile(String filePath) {
        BufferedReader bReader = null;
        try {
            bReader = new BufferedReader(new FileReader(filePath));
            String line = "";
            StringBuilder sb = new StringBuilder();
            while ((line = bReader.readLine()) != null) {
                sb.append(line);
            }
            return sb.toString();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bReader != null) {
                try {
                    bReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

    /**
     * 读取二进制文件,返回字节数组
     * @param filePath
     * @return
     */
    public static byte[] readBinaryFile(String filePath) {
        FileInputStream fis = null;
        ByteArrayOutputStream baos = null;

        try {
            fis = new FileInputStream(filePath);
            baos = new ByteArrayOutputStream();
            int c = 0;
            byte[] buffer = new byte[1024 * 8];
            while ((c = fis.read(buffer)) != -1) {
                baos.write(buffer, 0, c);
                baos.flush();
            }
            return baos.toByteArray();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (baos != null) {
                try {
                    baos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

    /**
     * 读取文件输入流,返回字节数组
     * @param fis
     * @return
     */
    public static byte[] readFileInputStream(FileInputStream fis) {
        ByteArrayOutputStream baos = null;
        try {
            baos = new ByteArrayOutputStream();
            int c = 0;
            byte[] buffer = new byte[1024 * 8];
            while ((c = fis.read(buffer)) != -1) {
                baos.write(buffer, 0, c);
                baos.flush();
            }
            return baos.toByteArray();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (baos != null) {
                try {
                    baos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }


    /**
     * 将文本写入到文件,返回boolean
     * @param content
     * @param filePath
     * @return
     */
    public static boolean writeTextFile(String content, String filePath) {
        BufferedWriter bWriter = null;
        try {
            bWriter = new BufferedWriter(new FileWriter(filePath, true));
            bWriter.write(content);
            bWriter.flush();
            return true;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bWriter != null) {
                try {
                    bWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
        return false;
    }

    /**
     * 将文本写入到文件,返回boolean
     * @param content
     * @param filePath
     * @return
     */
    public static boolean writeTextFile2(String content, String filePath) {
        PrintWriter pWriter = null;
        try {
            pWriter = new PrintWriter(new FileWriter(filePath, true), true);
            pWriter.write(content);
            return true;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (pWriter != null) {
                pWriter.close();
            }
        }
        return false;
    }

    /**
     * 将字节数组写入到文件,返回boolean
     * @param data
     * @param filePath
     * @return
     */
    public static boolean writeBinaryFile(byte[] data, String filePath) {
        BufferedOutputStream bos = null;
        try {
            bos = new BufferedOutputStream(new FileOutputStream(filePath));
            bos.write(data, 0, data.length);
            bos.flush();
            return true;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return false;
    }

    /**
     * 将输出流写入到文件,返回boolean
     * @param data
     * @param fos
     * @return
     */
    public static boolean writeFileOutputStream(byte[] data, FileOutputStream fos) {
        BufferedOutputStream bos = null;
        try {
            bos = new BufferedOutputStream(fos);
            bos.write(data, 0, data.length);
            bos.flush();
            return true;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return false;
    }

    /**
     * 拷贝文本文件,返回boolean
     * @param filePath
     * @param destFilePath
     * @return
     */
    public static boolean copyTextFile(String filePath, String destFilePath) {
        BufferedReader bReader = null;
        BufferedWriter bWriter = null;
        try {
            bReader = new BufferedReader(new FileReader(filePath));
            bWriter = new BufferedWriter(new FileWriter(destFilePath, true));

            String line = "";
            while ((line = bReader.readLine()) != null) {
                bWriter.write(line);
                bWriter.flush();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bReader != null) {
                try {
                    bReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bWriter != null) {
                try {
                    bWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return false;
    }

    /**
     * 拷贝文本文件或非文本文件,返回boolean
     * @param filePath
     * @param destFilePath
     * @return
     */
    public static boolean copyBinaryFile(String filePath, String destFilePath) {
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;

        try {
            bis = new BufferedInputStream(new FileInputStream(filePath));
            bos = new BufferedOutputStream(new FileOutputStream(destFilePath));

            int c = 0;
            byte[] buffer = new byte[8 * 1024];
            while ((c = bis.read(buffer)) != -1) {
                bos.write(buffer, 0, c);
                bos.flush();
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bis != null) {
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return false;
    }

    /**
     * 删除文件,返回boolean
     * @param filePath
     * @return
     */
    public static boolean deleteFile(String filePath) {
        File file = new File(filePath);
        return file.delete();
    }

    /**
     * 判断文件是否存在,返回boolean
     * @param filePath
     * @return
     */
    public static boolean isFileExist(String filePath) {
        File file = new File(filePath);
        return file.exists();
    }

    /**
     * 获取文件后缀,返回String
     * @param filePath
     * @return
     */
    public static String getFileExtension(String filePath) {
        return filePath.substring(filePath.lastIndexOf("."), filePath.length());
    }

    /**
     * 输入流转字节数组
     * @param is
     * @return
     */
    public static byte[] streamToByteArray(InputStream is) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        int c = 0;
        byte[] buffer = new byte[8 * 1024];
        try {
            while ((c = is.read(buffer)) != -1) {
                baos.write(buffer, 0, c);
                baos.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (baos != null) {
                try {
                    baos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return baos.toByteArray();
    }

    /**
     * 输入流转String
     * @param is
     * @param charsetName
     * @return
     */
    public static String streamToString(InputStream is, String charsetName) {
        BufferedInputStream bis = new BufferedInputStream(is);
        StringBuilder sb = new StringBuilder();
        int c = 0;
        byte[] buffer = new byte[8 * 1024];
        try {
            while ((c = bis.read(buffer)) != -1) {
                sb.append(new String(buffer, charsetName));
            }
            return sb.toString();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bis != null) {
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

    /**
     * 字符串转输入流
     * @param str
     * @return
     */
    public static InputStream stringToInputStream(String str) {
        InputStream is = null;
        try {
            is = new ByteArrayInputStream(str.getBytes("UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return is;
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值