Java Utililty Methods Text File Save

List of utility methods to do Text File Save

Description

The list of methods to do Text File Save are organized into topic(s).

Method

FilewriteFile(File path, String fileName, String fileContent)
write File
if (fileContent != null && fileContent.trim().length() > 0) {
    path.mkdirs();
    File out = new File(path, fileName);
    BufferedWriter writer = new BufferedWriter(new FileWriter(out));
    writer.write(fileContent);
    writer.close();
    return out;
return null;
voidwriteFile(File target, String content)
write File
PrintWriter p = null;
try {
    p = new PrintWriter(new FileWriter(target));
    p.write(content);
} catch (Exception e) {
    throw new RuntimeException("Problems writing text to file: " + target);
} finally {
    close(p);
...
voidwriteFile(final byte[] content, final String path)
write File
FileOutputStream stream = new FileOutputStream(path);
try {
    stream.write(content);
} finally {
    stream.close();
voidwriteFile(final File destination, final List contents)
Write the strings to the file, one per line.
final BufferedWriter bw = new BufferedWriter(new FileWriter(destination));
try {
    for (String line : contents) {
        bw.write(line);
        bw.newLine();
    bw.flush();
} finally {
...
voidwriteFile(final File f, final String content)
Write the content to the file.
final FileOutputStream o = new FileOutputStream(f);
o.write(content.getBytes());
o.close();
voidwriteFile(final File outputFile, final String data, final String encoding)
Writes the given String data to the given output file, encoded using the given encoding.
final FileOutputStream outStream = new FileOutputStream(outputFile);
OutputStreamWriter writer = null;
try {
    writer = new OutputStreamWriter(outStream, encoding);
    writer.write(data);
} finally {
    if (writer != null) {
        writer.close();
...
voidwriteFile(final String aText, final File aFile, final String aEncoding)
For tests only.
Writer writer = null;
try {
    aFile.getParentFile().mkdirs();
    writer = new OutputStreamWriter(new FileOutputStream(aFile), aEncoding);
    writer.write(aText);
} finally {
    close(writer);
voidwriteFile(final String file, final String text)
Method declaration
try {
    final FileWriter write = new FileWriter(file);
    write.write(text.toCharArray());
    write.close();
} catch (IOException e) {
    e.printStackTrace();
voidwriteFile(final String name, final String s)
Writes to the file with the given name
writeFile(name, s, false);
voidwriteFile(final String nameFile, final List listMsg)
Imprime a lista em uma arquivo.
try {
    Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(nameFile), "utf-8"));
    for (String msg : listMsg) {
        writer.write(msg + "\n");
    writer.close();
} catch (IOException e) {
    System.err.println("Erro ao escrever resultado no arquivo de saida. " + e);
...