Java Utililty Methods FileOutputStream Write

List of utility methods to do FileOutputStream Write

Description

The list of methods to do FileOutputStream Write are organized into topic(s).

Method

booleansaveStringIntoFile(String filePath, String contents)
save String Into File
File file = new File(filePath);
try {
    new FileOutputStream(file).write(contents.getBytes());
} catch (FileNotFoundException e) {
    e.printStackTrace();
    return false;
} catch (IOException e) {
    e.printStackTrace();
...
voidsaveStringsList(String file, Collection c)
save Strings List
saveStringsList(file, c, false);
voidsaveStringToFile(File file, String content, String charset)
Saves specified content to the file with specified charset.
FileOutputStream out = new FileOutputStream(file);
byte[] data = content.getBytes(charset);
out.write(data);
out.flush();
out.close();
FilesaveTemp(final byte[] data, final String suffix)
save Temp
final File tempFile = File.createTempFile("afirma", suffix); 
tempFile.deleteOnExit();
if (saveFile(tempFile, data)) {
    return tempFile;
return null;
voidsaveTempFile(String xmlSource, String fileName)
save Temp File
if (fileName.isEmpty()) {
    fileName = "MissingId";
File adhoc = new File(PATH + fileName + ".xml");
if (adhoc.exists()) {
    adhoc.delete();
adhoc.createNewFile();
...
voidsaveTextContentInFile(String content, File file)
save Text Content In File
FileOutputStream fos = null;
try {
    fos = new FileOutputStream(file);
    fos.write(content.getBytes("UTF-8"));
    fos.flush();
} catch (Exception e) {
    throw new RuntimeException("Cannot add the content into temp file " + file.getAbsolutePath(), e);
} finally {
...
voidsaveTextToFile(String fileName, String content)
save Text To File
if (content == null) {
    System.out.println("File not saved, content is null " + fileName);
    return;
FileOutputStream fop = null;
File file;
try {
    file = new File(fileName);
...
voidsaveTo(InputStream in, String fileName)
save To
saveTo(in, new File(fileName));
voidsaveTo(String path, InputStream in)
Saves content read from input stream into a file.
if (in == null)
    throw new IllegalArgumentException("input stream cannot be null");
if (path == null)
    throw new IllegalArgumentException("path cannot be null");
FileOutputStream out = new FileOutputStream(path);
try {
    byte[] bytes = new byte[1024];
    for (int x = in.read(bytes); x != -1; x = in.read(bytes))
...
voidsaveTo(String path, InputStream in)
Saves content read from input stream into a file.
if (in == null) {
    throw new IllegalArgumentException("input stream cannot be null");
if (path == null) {
    throw new IllegalArgumentException("path cannot be null");
FileOutputStream out = null;
try {
...