zoukankan      html  css  js  c++  java
  • 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());
                }
            }
        }
    }
  • 相关阅读:
    张维迎:你必须知道的10个经济学原理
    艾德莱斯绸:“千年时尚”托起新产业
    Sending forms through JavaScript[form提交 form data]
    Sending form data
    Your first HTML form
    form submission
    <input type="file">
    web storm查看文件结构
    jQuery-File-Upload
    IHttpHandler
  • 原文地址:https://www.cnblogs.com/ai211234/p/5944900.html
Copyright © 2011-2022 走看看