Java FileOutputStream Write saveTextToFile(String fileName, String content)

Here you can find the source of saveTextToFile(String fileName, String content)

Description

save Text To File

License

Apache License

Declaration

public static void saveTextToFile(String fileName, String content) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

public class Main {
    public static void saveTextToFile(String fileName, String content) {
        if (content == null) {
            System.out.println("File not saved, content is null " + fileName);
            return;
        }/*  w w  w. j ava  2  s.  com*/
        FileOutputStream fop = null;
        File file;
        try {

            file = new File(fileName);
            fop = new FileOutputStream(file);

            // if file doesnt exists, then create it
            if (!file.exists()) {
                file.createNewFile();
            }

            // get the content in bytes
            byte[] contentInBytes = content.getBytes();

            fop.write(contentInBytes);
            fop.flush();
            fop.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fop != null) {
                    fop.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

Related

  1. saveStringsList(String file, Collection c)
  2. saveStringToFile(File file, String content, String charset)
  3. saveTemp(final byte[] data, final String suffix)
  4. saveTempFile(String xmlSource, String fileName)
  5. saveTextContentInFile(String content, File file)
  6. saveTo(InputStream in, String fileName)
  7. saveTo(String path, InputStream in)
  8. saveTo(String path, InputStream in)
  9. saveToBinaryFile(File dest, byte[] data)