<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    文件處理

    package nl.enovation.ems.transferlink.client.util;

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.RandomAccessFile;
    import java.nio.ByteBuffer;
    import java.nio.MappedByteBuffer;
    import java.nio.channels.Channels;
    import java.nio.channels.FileChannel;
    import java.nio.channels.ReadableByteChannel;
    import java.nio.channels.WritableByteChannel;
    import java.util.ArrayList;
    import java.util.Collection;
    import java.util.Enumeration;
    import java.util.zip.ZipEntry;
    import java.util.zip.ZipFile;
    import java.util.zip.ZipOutputStream;

    public class FileUtil {


     public static void moveFile(File source, String path) throws IOException {
      if(!source.renameTo(new File(path, source.getName()))){
       throw new IOException(
         "Can't move file "
         + source.getName()
         + " to "
         + path);
      }
     }
     
     public static File moveFile(File source, File path) throws IOException {
      File newFile = new File(path, source.getName());
      if(!source.renameTo(newFile)){
       throw new IOException(
         "Can't move file "
         + source.getName()
         + " to "
         + path);
      }
      return newFile;
     }
     
     public static void moveFile(File source, File path, boolean uniqueFileName) throws IOException {
      String fileName = source.getName();
      File file = new File(path, fileName);
      int count = 1;
      while(true) {
       if(file.exists()) {
        String newFileName = null;
        int idx = fileName.lastIndexOf(".");
        if(idx > 0) {
         newFileName = fileName.substring(0, idx) + "." + count + fileName.substring(idx); 
        } else {
         newFileName = fileName + "." + count;
        }
        file = new File(path, newFileName);
        count++;
       } else {
        break;
       }
      }
      if(!source.renameTo(file)){
       //try {
       // copyFile(source, file);
       // deleteFile(source);
       //}
       //catch (IOException e)
       //{
        throw new IOException(
          "Can't move file "
          + source.getName() //+ " (" + source.getAbsolutePath() + ")"
          + " to "
          + path);
         //+"("+e.getMessage()+")");
         // + " (" + file.getAbsolutePath() + ")");
       //}
      }
     }
     
     public static void moveFiles(File sourceDir, File destDir) throws IOException {
      java.io.File[] list=sourceDir.listFiles();
      for (int i = 0; i < list.length; i++) {
       if (!list[i].isDirectory()) {
        if(!list[i].renameTo(new File(destDir, list[i].getName()))) {
         throw new IOException(
           "Can't move file "
           + list[i].getName()
           + " to "
           + destDir.getAbsolutePath());
        }
       }
      }
     }
     
     /**
      * Copy a File (source) to a File (destination)
      * @param source
      * @param dest
      * @throws IOException
      */
     public static void copyFile(File source, File dest) throws IOException {
          FileChannel in = null, out = null;
          try {         
               in = new FileInputStream(source).getChannel();
               out = new FileOutputStream(dest).getChannel();

               long size = in.size();
               MappedByteBuffer buf = in.map(FileChannel.MapMode.READ_ONLY, 0, size);
               out.write(buf);

          } finally {
               if (in != null)     
                in.close();
               if (out != null)    
                out.close();
          }
     }
     
     /**
      * Write an input stream to a file.
      * @param source
      * @param dest
      * @param size
      * @throws IOException
      */
     public static int writeFile(InputStream source, File dest, long buffer) throws IOException {
      int bytesWrote = 0;
      FileChannel out = null;
      ReadableByteChannel in = null;
      ByteBuffer data = ByteBuffer.allocate((int) buffer);
      try {
       in = Channels.newChannel(source);
       out = new FileOutputStream(dest).getChannel();
       
       int read = in.read(data);
       while(read > 0) {
        bytesWrote += read;
        data.flip();
        out.write(data);
        data.clear();
        read = in.read(data);
       }

      } finally {
       if(out != null)
        out.close();
       if(in != null)
        in.close();
      }
      return bytesWrote;
     }
     
     public static void delete(java.io.File path) throws IOException {
      if (path.isDirectory()) {
       deleteDirectory(path);
      } else {
       deleteFile(path);
      }
     }
     
     protected static void deleteDirectory(java.io.File path) throws IOException {
      java.io.File[] list = path.listFiles();
      for (int i = 0; i < list.length; i++) {
       if (list[i].isDirectory()) {
        deleteDirectory(list[i]);
        if (!list[i].delete()) {
         throw new IOException("Can't delete directory '"+list[i]+"'.");
        }
       } else {
        deleteFile(list[i]);
       }
      }
     }
     
     protected static void deleteFile(java.io.File path) throws IOException {
      if (!path.delete()) {
       throw new IOException("Can't delete file '"+path+"'.");
      }
     }
     
     // TODO optimize ... read and write with buffers?!
     //@SuppressWarnings("unchecked")
     public static Collection splitFile(File file, int splitlen, File tmpDir) throws IOException
     {
      long leng = 0;
      int count = 1, read = 0;
      byte [] buffer = new byte[8196];
      
      String fileName  = file.getName();
      Collection files = new ArrayList();  
      RandomAccessFile infile = new RandomAccessFile(file, "r");
      read = infile.read(buffer);
      while(read != -1)
      {
       file = new File(tmpDir, fileName + ".lms.sp" + count);
       RandomAccessFile outfile = new RandomAccessFile(file, "rw");
       while( read != -1 && leng < splitlen)
       {
        outfile.write(buffer, 0, read);
        leng += read;
        read = infile.read(buffer);
       }
       leng = 0;
       outfile.close();
       file.deleteOnExit();
       files.add(file);
       count++;
      }
      return files;
     }
     
     public static File joinFile(String fileName, File tmpDir) throws IOException
     {
      int count = 1, read = 0;
      byte [] buffer = new byte[8196];
      
      File destFile = new File(tmpDir, fileName);
      RandomAccessFile outfile = new RandomAccessFile(destFile, "rw");
      while(true)
      {
       File readFile = new File(tmpDir, fileName + ".lms.sp" + count);
       if (readFile.exists())
       {
        RandomAccessFile infile = new RandomAccessFile(readFile, "r");
        read = infile.read(buffer);
        while(read != -1)
        {
         outfile.write(buffer, 0, read);
         read = infile.read(buffer);
        }
        infile.close();
        readFile.delete();
        count++;
       }
       else
       {
        break;
       }
      }
      outfile.close();
      return destFile;
     }
     
     public static boolean isComplete(String fileName, File dir, int parts) {
      int counter = 1;
      int matched = 0;
      File[] list = dir.listFiles();
      for (int i = 0; i < list.length; i++) {
       if (!list[i].isDirectory()) {
        String f = list[i].getName();
        String f2 = fileName + ".lms.sp";
        if(f.startsWith(f2)) {
         matched++;
        }
        counter++;
       }
      }
      if(matched == parts) {
       return true;
      } else {
       return false;
      }
      
     }
     
     public static File zipFile(File file, File dir) throws IOException {
      WritableByteChannel out = null;
      ReadableByteChannel in = null;
      FileInputStream source = new FileInputStream(file);
      File dest = new File(dir, file.getName() + ".zip");
      ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(dest));
      dest.deleteOnExit();
      int buffer = 4096;
      ByteBuffer data = ByteBuffer.allocate(buffer);
      try {
       zos.putNextEntry(new ZipEntry(file.getName()));

       in = Channels.newChannel(source);
       out = Channels.newChannel(zos);
       
       int read = in.read(data);
       while(read > 0) {
        data.flip();
        out.write(data);
        data.clear();
        read = in.read(data);
       }
       
       zos.closeEntry();
      } finally {
       if(zos != null) {
        zos.finish();
       }
       if(out != null)
        out.close();
       if(in != null)
        in.close();
      }
      return dest;
     }
     
     public static File unzipFile(File file, File dir) throws IOException {
      WritableByteChannel out = null;
      ReadableByteChannel in = null;
      ZipEntry entry = null;
      ZipFile zipFile = null;
      int buffer = 4096;
      ByteBuffer data = ByteBuffer.allocate(buffer);
      File unzippedFile = null;
      try {
       zipFile = new ZipFile(file, ZipFile.OPEN_READ);
       Enumeration zipFileEntries = zipFile.entries();
          
       // Process each entry
       if (zipFileEntries.hasMoreElements()) {
        // grab a zip file entry
        entry = (ZipEntry) zipFileEntries.nextElement();
        String currentEntry = entry.getName();
        InputStream input = zipFile.getInputStream(entry);
        unzippedFile = new File(dir, currentEntry);
        FileOutputStream output = new FileOutputStream(unzippedFile);
        
        in = Channels.newChannel(input);
        out = Channels.newChannel(output);

        int read = in.read(data);
        while(read > 0) {
         data.flip();
         out.write(data);
         data.clear();
         read = in.read(data);
        }
       }
      } finally {
       if(file != null)
        file.delete();
       if(zipFile != null)
        zipFile.close();
       if(out != null)
        out.close();
       if(in != null)
        in.close();
      }
      return unzippedFile;
     }
    }


    不過最好使用Apache Common 的IO組件。
    里面有個(gè)FileUtils,提供了很多很好的方法。
    可以參考:
     

    http://commons.apache.org/io/apidocs/index.html

    posted on 2008-01-21 17:44 劉錚 閱讀(335) 評(píng)論(0)  編輯  收藏 所屬分類: JAVA General

    <2025年5月>
    27282930123
    45678910
    11121314151617
    18192021222324
    25262728293031
    1234567

    導(dǎo)航

    統(tǒng)計(jì)

    留言簿(1)

    文章分類(141)

    文章檔案(147)

    搜索

    最新評(píng)論

    主站蜘蛛池模板: 久久久99精品免费观看| 中文字幕av免费专区| 日日麻批免费40分钟日本的| 亚洲VA中文字幕无码毛片| 男女拍拍拍免费视频网站| 国产亚洲自拍一区| 国产无遮挡又黄又爽免费网站| av无码东京热亚洲男人的天堂| 国产偷国产偷亚洲清高APP| 国产美女精品视频免费观看| 久久亚洲精品成人无码| 免费看小12萝裸体视频国产| 黄色网址大全免费| 一本色道久久综合亚洲精品高清| 久久国产福利免费| 亚洲AV无码AV男人的天堂| 91精品手机国产免费| 亚洲AV无码成人专区| 成年美女黄网站色大免费视频| 亚洲国产成人无码AV在线| 亚洲成人影院在线观看| 怡红院免费全部视频在线视频| 亚洲免费精彩视频在线观看| 免费专区丝袜脚调教视频| 亚洲女女女同性video| 亚洲国产成人精品女人久久久 | 亚洲精品无码久久不卡| 男女猛烈无遮掩视频免费软件 | 亚洲天堂福利视频| 在线观看免费a∨网站| 免费看一级高潮毛片| 国产精品亚洲а∨无码播放| 美丽的姑娘免费观看在线播放| 亚洲成AV人影片在线观看| 亚洲一级特黄大片在线观看| 日韩人妻无码精品久久免费一| 亚洲不卡在线观看| 国产精品亚洲视频| 国产男女爽爽爽爽爽免费视频| 国产亚洲精品精品精品| 亚洲AV无码成人精品区在线观看 |