Android File Move moveSubFiles(String sourceFileName, String destPath)

Here you can find the source of moveSubFiles(String sourceFileName, String destPath)

Description

move Sub Files

License

Sun Public License Notice

Declaration

public static void moveSubFiles(String sourceFileName, String destPath) 

Method Source Code

/*****************************************************************************
 *                                                                           *
 *  This file is part of the tna framework distribution.                     *
 *  Documentation and updates may be get from  biaoping.yin the author of    *
 *  this framework                          *
 *                                                                           *
 *  Sun Public License Notice:                                               *
 *                                                                           *
 *  The contents of this file are subject to the Sun Public License Version  *
 *  1.0 (the "License"); you may not use this file except in compliance with *
 *  the License. A copy of the License is available at http://www.sun.com    *
 *                                                                           *
 *  The Original Code is tag. The Initial Developer of the Original          *
 *  Code is biaoping yin. Portions created by biaoping yin are Copyright     *
 *  (C) 2000.  All Rights Reserved.                                          *
 *                                                                           *
 *  GNU Public License Notice:                                               *
 *                                                                           *
 *  Alternatively, the contents of this file may be used under the terms of  *
 *  the GNU Lesser General Public License (the "LGPL"), in which case the    *
 *  provisions of LGPL are applicable instead of those above. If you wish to *
 *  allow use of your version of this file only under the  terms of the LGPL *
 *  and not to allow others to use your version of this file under the SPL,  *
 *  indicate your decision by deleting the provisions above and replace      *
 *  them with the notice and other provisions required by the LGPL.  If you  *
 *  do not delete the provisions above, a recipient may use your version of  *
 *  this file under either the SPL or the LGPL.                              *
 *                                                                           *
 *  biaoping.yin (yin-bp@163.com)                                            *
 *  Author of Learning Java                                        *
 *                                                                           *
 *****************************************************************************/

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.net.URL;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.ListResourceBundle;
import java.util.Properties;
import java.util.Vector;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;

public class Main{
    public static void moveSubFiles(String sourceFileName, String destPath) {
        File src = new File(sourceFileName);
        File dest = new File(destPath);
        if (!dest.exists())
            dest.mkdirs();/*w  w  w  .j a v  a2  s.c o  m*/
        if (src.isFile())
            return;
        else {
            File[] files = src.listFiles();
            String destFile = null;
            for (int i = 0; files != null && i < files.length; i++) {
                if (files[i].isDirectory()) {
                    String temp_name = files[i].getName();
                    try {
                        moveSubFiles(files[i].getAbsolutePath(), destPath
                                + "/" + temp_name);
                    } catch (Exception e) {
                        e.printStackTrace();
                        continue;
                    }
                } else {

                    destFile = destPath + "/" + files[i].getName();
                    try {
                        moveFile(files[i].getAbsolutePath(), destFile);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
    
    public static void moveFile(String sourceFileName, String destPath)
            throws Exception {
        File src = new File(sourceFileName);
        // File dest = new File(destPath );
        if (!src.exists()) {
            throw new Exception("save file[" + sourceFileName
                    + "] to file[" + destPath + "] failed:"
                    + sourceFileName + " not exist.");
        }
        // if (dest.exists()) {
        // if (!dest.delete())
        // {
        // System.out.println("delete dest file failed:" +
        // dest.getAbsolutePath()
        // + " the file is read= " + dest.canRead()
        // + " the file is write= " + dest.canWrite());
        // }
        // // throw new FileMoveException(
        // // "Dest file already exists,delete fail!");
        // }
        // src = null;
        // dest = null;
        try {
            FileUtil.fileCopy(sourceFileName, destPath);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            System.out.println("save file[" + sourceFileName + "] to file["
                    + destPath + "]" + e.getMessage());
            e.printStackTrace();
        }

        // if (!src.renameTo(dest))
        // {
        // try {
        // System.setOut(new java.io.PrintStream(new
        // java.io.FileOutputStream(new File("d:/test.log"))));
        // } catch (FileNotFoundException e) {
        // // TODO Auto-generated catch block
        // e.printStackTrace();
        // }
        //                  
        // System.out.println("src.getAbsolutePath():" + src.getAbsolutePath());
        // System.out.println("src.exist():" + src.exists());
        // System.out.println("dest.getAbsolutePath():" +
        // dest.getAbsolutePath());
        // System.out.println("dest.exist():" + dest.exists());
        // // Runtime.getRuntime().halt(1);
        //                  
        // throw new FileMoveException("Move file fail!");
        // }
    }
    public static void fileCopy(String sourcefile, String destinctionFile)
            throws IOException {
        fileCopy(new File(sourcefile), destinctionFile);
    }
    public static void fileCopy(File sourcefile, String destinctionFile)
            throws IOException {

        FileInputStream stFileInputStream = null;

        FileOutputStream stFileOutputStream = null;

        try {
            makeFile(destinctionFile);

            stFileInputStream = new FileInputStream(sourcefile);

            stFileOutputStream = new FileOutputStream(destinctionFile);

            int arraySize = 1024;
            byte buffer[] = new byte[arraySize];
            int bytesRead;
            while ((bytesRead = stFileInputStream.read(buffer)) != -1) {
                stFileOutputStream.write(buffer, 0, bytesRead);
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (stFileInputStream != null)
                try {
                    stFileInputStream.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            if (stFileOutputStream != null)
                try {
                    stFileOutputStream.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
        }

    }
    public static void fileCopy(File sourcefile, File destinctionFile)
            throws IOException {

        FileInputStream stFileInputStream = null;

        FileOutputStream stFileOutputStream = null;

        try {
            //            makeFile(destinctionFile);

            stFileInputStream = new FileInputStream(sourcefile);

            stFileOutputStream = new FileOutputStream(destinctionFile);

            int arraySize = 1024;
            byte buffer[] = new byte[arraySize];
            int bytesRead;
            while ((bytesRead = stFileInputStream.read(buffer)) != -1) {
                stFileOutputStream.write(buffer, 0, bytesRead);
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (stFileInputStream != null)
                try {
                    stFileInputStream.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            if (stFileOutputStream != null)
                try {
                    stFileOutputStream.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
        }

    }
    public static void makeFile(String destinctionFile) {
        File f = new File(destinctionFile);
        File pf = f.getParentFile();
        if (f.exists())
            return;
        if (!pf.exists()) {
            pf.mkdirs();
        }

        try {
            f.createNewFile();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
    
    public static StringBuffer read(String file) throws Exception {
        BufferedReader in = null;
        StringBuffer sb = new StringBuffer();
        String s = null;
        StringBuffer stringbuffer;
        try {
            in = new BufferedReader(new FileReader(file));
            while ((s = in.readLine()) != null)
                sb.append(s).append('\n');
            stringbuffer = sb;
        } finally {
            if (in != null)
                in.close();
        }
        return stringbuffer;
    }
}

Related

  1. move(File file, File directory)
  2. move(File source, File destination)
  3. move(String sourceFileName, String destinationFileName)
  4. moveFile(String sourceFileName, String destPath)
  5. move(String input, String output)
  6. moveFile(File source, File target)
  7. moveFile(File src, File dest)
  8. moveFile(String in, String out)