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

FiletoFile(JavaFileObject javaFileObject)
Gets the file from a Java file object.
return new File(javaFileObject.getName());
ListtoFile(List filenames)
to File
List<File> files = new ArrayList<File>();
for (String filename : filenames) {
    files.add(new File(filename));
return files;
voidtoFile(Map res, String output)
to File
int c = 0;
try (PrintWriter writer = new PrintWriter(output, "UTF-8")) {
    for (Map.Entry<K, V> e : res.entrySet()) {
        writer.println(c + "\t" + e.getKey().toString() + "\t" + e.getValue().toString());
        c++;
voidtoFile(String base64Code, String targetPath)
to File
byte[] buffer = base64Code.getBytes();
FileOutputStream out = new FileOutputStream(targetPath);
out.write(buffer);
out.close();
voidtoFile(String content, File output)
to File
try {
    BufferedWriter writer = new BufferedWriter(new FileWriter(output));
    writer.write(content);
    writer.flush();
    writer.close();
} catch (IOException e) {
    throw new RuntimeException(e);
voidtoFile(String context, String fileName, boolean append)
Writes desired string data to appropriate file.
PrintStream ps = new PrintStream(new FileOutputStream(fileName, append));
ps.println(context);
ps.flush();
ps.close();
FiletoFile(String fileName)
to File
try {
    return new File(fileName).getCanonicalFile();
} catch (IOException e) {
    return new File(fileName).getAbsoluteFile();
voidtoFile(String fileName, String content)
to File
File file = new File(fileName);
try (BufferedWriter bw = new BufferedWriter(new FileWriter(file))) {
    bw.write(content);
    bw.flush();
FiletoFile(String fileName, String cwd)
to File
File file = new File(fileName);
if (!file.isAbsolute()) {
    file = new File(cwd + File.separator + fileName);
return file;
voidtoFile(String fileName, String txt)
Write string to file
PrintWriter pw = new PrintWriter(new FileWriter(fileName));
pw.print(txt);
pw.close();