Java Write String to File writeFile(final File file, final String content)

Here you can find the source of writeFile(final File file, final String content)

Description

write File

License

Open Source License

Declaration

static File writeFile(final File file, final String content) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class Main {
    static File writeFile(final File file, final String content) {
        try {/* w w  w . j ava2s.c  o m*/
            if (file.exists()) {
                file.delete();
            }

            final File parent = file.getParentFile();
            if (!parent.exists()) {
                parent.mkdirs();
            }

            final FileWriter fw = new FileWriter(file);
            fw.write(content);
            fw.close();
            return file;
        } catch (final IOException e) {
            throw new RuntimeException("Error in tests!", e);
        }
    }
}

Related

  1. writeFile(File file, String text)
  2. writeFile(File file, String... lines)
  3. writeFile(File file, String... lines)
  4. writeFile(File file, StringBuffer buffer)
  5. writeFile(File filename, ArrayList lines)
  6. writeFile(final File file, final String contents)
  7. writeFile(final File file, final String contents)
  8. writeFile(final File iFile, final String iContent)
  9. writeFile(String absoluteFileName, String content, boolean append)