Java File Object Create toFile(File dstPath, String fullClassName)

Here you can find the source of toFile(File dstPath, String fullClassName)

Description

Converts file reference from file and ensures that the path exists

License

Open Source License

Parameter

Parameter Description
dstPath src folder
fullClassName full class name

Return

java file path

Declaration

public static File toFile(File dstPath, String fullClassName) 

Method Source Code


//package com.java2s;
/* ========================================================================
* Copyright (c) 2005-2015 The OPC Foundation, Inc. All rights reserved.
*
* OPC Foundation MIT License 1.00/*from  w ww  .ja va2 s.c o m*/
* 
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
* 
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* The complete license agreement can be found here:
* http://opcfoundation.org/License/MIT/1.00/
* ======================================================================*/

import java.io.File;

public class Main {
    /**
     * Converts file reference from file and ensures that the path exists
     * 
     * @param dstPath src folder 
     * @param fullClassName full class name
     * @return java file path
     */
    public static File toFile(File dstPath, String fullClassName) {
        String packageName = getPackageName(fullClassName);
        String packagePath = packageName.replaceAll("\\.", "/");
        File path = new File(dstPath, packagePath);
        path.mkdirs();
        if (!path.exists())
            throw new RuntimeException("Could not create " + path);
        String fileName = fullClassName.replaceAll("\\.", "/");
        File file = new File(dstPath, fileName + ".java");
        return file;
    }

    public static String getPackageName(String fullClassName) {
        int i = fullClassName.lastIndexOf(".");
        if (i < 0)
            return "";
        return fullClassName.substring(0, i);
    }
}

Related

  1. toFile(byte[] bFile)
  2. toFile(Class cls)
  3. toFile(File baseDir, String path)
  4. toFile(File currentDirectory, String file)
  5. toFile(File dir, String[] path)
  6. toFile(File file, byte[] data)
  7. toFile(IFile file)
  8. toFile(InputStream in, File file)
  9. toFile(InputStream inputStream)