Package se.runa.file

Source Code of se.runa.file.FileManager

package se.runa.file;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.swing.Icon;
import javax.swing.JFileChooser;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.Copy;
import org.apache.tools.ant.taskdefs.Delete;
import org.apache.tools.ant.taskdefs.Move;
import org.apache.tools.ant.types.FileSet;
import org.apache.tools.ant.types.resources.FileResource;

public class FileManager {

    private static JFileChooser fileChooser = new JFileChooser();

    public static long getSizeAsLong(File file) {
  return new FileResource(file).getSize();
    }

    public static long getSizeAsLong(FileResource fileResource) {
  return fileResource.getSize();
    }

    public static String getSizeAsString(FileResource fileResource) {
  long size = getSizeAsLong(fileResource);
  return getSizeAsString(size);
    }

    public static String getSizeAsString(File file) {
  return getSizeAsString(new FileResource(file));
    }

    public static String getSizeAsString(long size) {
  if (size < 1024l) {
      return String.valueOf(size) + " B";
  } else if (size < 1048576l) {
      return String.valueOf(size / 1024l) + " KB";
  } else if (size < 1073741824l) {
      return String.valueOf(size / 1048576l) + " MB";
  } else if (size < 1099511627776l) {
      return String.valueOf(size / 1073741824l) + " GB";
  } else {
      return String.valueOf("?");
  }
    }

    public static void copy(List<File> files, File destDir) throws IOException {
  try {
      Copy task = new Copy();
      task.setProject(new Project());
      task.setTodir(destDir);
      for (File file : files) {
    FileSet fileSet = new FileSet();
    if (file.isDirectory()) {
        fileSet.setDir(file.getParentFile());
        fileSet.setIncludes(file.getName() + "/**");
    } else {
        fileSet.setFile(file);
    }
    task.addFileset(fileSet);
      }
      task.execute();
  } catch (BuildException e) {
      throw new IOException(e);
  }
    }

    public static void move(List<File> files, File destDir) throws IOException {
  try {
      Move task = new Move();
      task.setProject(new Project());
      task.setTodir(destDir);
      for (File file : files) {
    FileSet fileSet = new FileSet();
    if (file.isDirectory()) {
        fileSet.setDir(file.getParentFile());
        fileSet.setIncludes(file.getName() + "/**");
    } else {
        fileSet.setFile(file);
    }
    task.addFileset(fileSet);
      }
      task.execute();
  } catch (BuildException e) {
      throw new IOException(e);
  }
    }

    public static void delete(List<File> files) throws IOException {
  for (File file : files) {
      delete(file);
  }
    }

    public static void delete(File file) throws IOException {
  try {
      Delete task = new Delete();
      if (file.isDirectory()) {
    task.setDir(file);
      } else {
    task.setFile(file);
      }
      task.execute();
  } catch (BuildException e) {
      throw new IOException(e);
  }
    }

    public static Icon getIcon(File file) {
  return fileChooser.getIcon(file);
    }

    public static String getKind(File file) {
  return file.isDirectory() ? "Folder" : "File";
    }

    public static Date getModifyDate(File file) {
  return getModifyDate(new FileResource(file));
    }

    public static Date getModifyDate(FileResource fileResource) {
  long lastModified = fileResource.getLastModified();
  return new Date(lastModified);
    }

}
TOP

Related Classes of se.runa.file.FileManager

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.