• saveByRandomAccessFile(文件保存工具类)


    import java.io.BufferedInputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.RandomAccessFile;
    
    
    public class FileUtil {
    
        /**
         * @Title: saveByRandomAccessFile 
         * @Description: 使用RandomAccessFile写入文件
         * @param outPath 存入的目标地址,包括文件名称扩展名
         * @param tempFile 源文件
         * @throws IOException
         */
        public static void saveByRandomAccessFile(String outPath, File tempFile)
                throws IOException {
            RandomAccessFile raFile = null;
            BufferedInputStream inputStream = null;
            try {
                File dirFile = new File(outPath);
                if (!dirFile.getParentFile().exists()) {
                    dirFile.getParentFile().mkdirs();
                }
                // 以读写的方式打开目标文件
                raFile = new RandomAccessFile(dirFile, "rw");
                raFile.seek(raFile.length());
                inputStream = new BufferedInputStream(new FileInputStream(tempFile));
                byte[] buf = new byte[1024];
                int length = 0;
                while ((length = inputStream.read(buf)) != -1) {
                    raFile.write(buf, 0, length);
                }
            } catch (Exception e) {
                throw new IOException(e.getMessage());
            } finally {
                try {
                    if (inputStream != null) {
                        inputStream.close();
                    }
                    if (raFile != null) {
                        raFile.close();
                    }
                } catch (Exception e) {
                    throw new IOException(e.getMessage());
                }
            }
        }
    }
  • 相关阅读:
    hdu 5833 Zhu and 772002 (高斯消元)
    1203事件对象
    作用域面试题
    1130 JS高级 面向对象
    1122JS中级复习
    1120浏览器对象模型 函数分析
    1119动画和复习
    1114面试题作用域
    1113Js操作CSS样式
    1112函数封装和元素的属性
  • 原文地址:https://www.cnblogs.com/ai211234/p/5944900.html
Copyright © 2020-2023  润新知