将字符串附加到目标以创建新目录

时间:2016-06-21 10:56:06

标签: java file directory

您好我试图将一些变量附加到目录中以在我所在的目录中创建一个新的子目录但是我不知道该怎么做,我知道.mkdir()允许我创建一个新目录但我不知道如何附加变量然后创建新目录,这是我到目前为止的尝试: package movefile;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class MoveFile
{
static String newfile;


public static void main(String[] args)
{   
    File srcFolder = new File("/Users/francis/Desktop/folder1");
    File destFolder = new File("/Users/francis/Desktop/folder2");
    //make sure source exists
    if(!srcFolder.exists()){

       System.out.println("Directory does not exist.");
       //just exit
       System.exit(0);
    }else{      
       try{
        copyFolder (srcFolder,destFolder);
       }catch(IOException e){
        e.printStackTrace();
        //error, just exit
            System.exit(0);
       }
    }       
    System.out.println("Done");
}

 public static void copyFolder(File src, File dest)
    throws IOException{

        if(src.isDirectory()){

        //if directory not exists, create it
        if(!dest.exists()){
           dest.mkdir();
           System.out.println("Directory copied from " 
                          + src + "  to " + dest);
        }
        //list all the directory contents
        String files[] = src.list();
            FilenameFilter filter = new FilenameFilter()
    {
        @Override public boolean accept(File dir, String name)
    {
    return name.endsWith(".pdf");
    }
    };
        for (String file : files) {
           //construct the src and dest file structure
           File srcFile = new File(src, file); //needed for moving

              //third attempt  
                File f = null;
                File f1 = null;
                String vendor;
                String orderno;
                String desc;
                String v, v1, p;
                boolean bool = false;

                try {
                    f = new File("/Users/francis/Desktop/folder1/1234567898.pdf");
                    f1 = new File("/Users/francis/Desktop/folder2/");

                    v = f.getName();
                    v1 = f1.getName();

                    v = v.length() > 9 ? v.substring(0,8) : v;

                    p = "c3269";

                    vendor = "VendorName";

                    v = "file_" + p + " - " + v + ".pdf";

                    File destFile = new File(dest, v);    //get dest and insert (append) project number so it creates new folder
                    copyFolder(srcFile,destFile);

                   File appendProject = new File("/Users/francis/Desktop/folder2/");

                   boolean successfull = appendProject.mkdir();

                    if (successfull)
                    {
                        System.out.println("New directory was created:");
                    }
                    else
                        System.out.println("Failed to create new directory");

                    bool = f.exists();

                    if(bool)
                    {
                        System.out.println("File name:" + v);
                    }
                    bool = f1.exists();
                    if (bool)
                    {
                        System.out.println("Folder name:" + v1);
                    }    
                }catch(Exception e){
                    e.printStackTrace();
                }



            }
    }else{
        //if file, then copy it
        InputStream in = new FileInputStream(src);
            OutputStream out = new FileOutputStream(dest); 

            byte[] buffer = new byte[1024];

            int length;
            //copy the file content in bytes 
            while ((length = in.read(buffer)) > 0){
               out.write(buffer, 0, length);
            }
            in.close();
            out.close();
            System.out.println("File copied from " + src + " to " + dest);
    }
   }   
  }

目前这是检查文件夹中的.pdf文件,将P和V值附加到名称,将字符串修剪为8个字符并将文件移动到新文件夹但我希望它们被移动并创建新目录基于项目编号,该项目编号位于变量p。

非常感谢任何帮助,非常感谢!

1 个答案:

答案 0 :(得分:0)

假设您要将文件File srcFile复制到文件夹File destDir,您可以执行以下操作:

if( !destDir.exists() ) {
  //create destDir and all missing parent folders
  destDir.mkdirs(); //For simplicity I'll leave out the success checks, don't forget them in your code
}

String destFilename = srcFile.getName(); //adapt according to your needs
File destFile = new File( destDir, destFilename );

//You'd need to either implement that or use a library like Apache Commons IO
copy( srcFile, destFile );

如果要首先从文件名创建目录,请执行以下操作:

String destSubDirName = makeDirName( destFilename ); //whatever you need to do here
File destSubDir = new File( destDir, destSubDirName );
destSubDir.mkdirs(); //again don't forget the checks

File destFile = new File (destSubDir, destFilename );

copy( srcFile, destFile );