Java Utililty Methods File Object Create

List of utility methods to do File Object Create

Description

The list of methods to do File Object Create are organized into topic(s).

Method

voidtoFile(byte[] bFile)
to File
try {
    FileOutputStream fileOuputStream = new FileOutputStream("C:\\testing2.png");
    fileOuputStream.write(bFile);
    fileOuputStream.close();
    System.out.println("Done");
} catch (Exception e) {
    e.printStackTrace();
FiletoFile(Class cls)
to File
return new File(cls.getName().replace('.', '/') + ".class");
FiletoFile(File baseDir, String path)
to File
File result = baseDir;
for (String part : path.split("/")) { 
    result = new File(result, part);
return result;
FiletoFile(File currentDirectory, String file)
to File
String f = file == null ? "" : file;
if (f.startsWith("/")) {
    return new File(f);
return new File(currentDirectory, f);
FiletoFile(File dir, String[] path)
Build a full path of file with given directory, and relative path.
String dirPath = dir.getCanonicalPath();
StringBuilder sb = new StringBuilder(dirPath);
for (int i = 0; i < path.length; i++) {
    sb.append(File.separator);
    sb.append(path[i]);
return new File(sb.toString());
FiletoFile(File dstPath, String fullClassName)
Converts file reference from file and ensures that the path exists
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");
...
voidtoFile(File file, byte[] data)
Outputs some arbitrary data to file on disk
System.out.println("Attempt create file " + file.getAbsolutePath());
File parent = file.getParentFile();
parent.mkdirs();
BufferedOutputStream outputStream = null;
try {
    outputStream = new BufferedOutputStream(new FileOutputStream(file));
    outputStream.write(data);
} catch (FileNotFoundException e) {
...
FiletoFile(IFile file)
to File
File f = file.getRawLocation() != null ? file.getRawLocation().toFile() : null;
if (f == null)
    f = file.getLocation() != null ? file.getLocation().toFile() : null;
if (f == null)
    f = file.getFullPath() != null ? file.getFullPath().toFile() : null;
return f;
inttoFile(InputStream in, File file)
to File
try (FileOutputStream out = new FileOutputStream(file)) {
    return copyStream(in, out);
FiletoFile(InputStream inputStream)
to File
File result = null;
OutputStream outputStream = null;
try {
    result = File.createTempFile(".tmp", ".tmp");
    outputStream = new FileOutputStream(result);
    int read = 0;
    byte[] bytes = new byte[1024];
    while ((read = inputStream.read(bytes)) != -1) {
...