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

voidsaveFile(String file, String text)
save File
try {
    File f = new File(file);
    if (!f.exists())
        f.createNewFile();
    FileOutputStream fout = new FileOutputStream(f);
    fout.write(text.getBytes());
    fout.flush();
    fout.close();
...
voidsaveFile(String fileName, byte[] bytes)
save File
try {
    FileOutputStream fos = new FileOutputStream(fileName, true);
    fos.write(bytes);
    fos.flush();
    fos.close();
} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
...
voidsaveFile(String filename, byte[] content)
save File
saveFile(filename, new ByteArrayInputStream(content));
voidsaveFile(String fileName, byte[] data)
Convenience method to save a file
try {
    OutputStream out = new FileOutputStream(fileName);
    out.write(data);
    out.close();
} catch (Exception e) {
    System.out.println("Cannot save file. " + e.getMessage());
System.out.println("Have save file of: " + data.length + " bytes.");
...
FilesaveFile(String fileName, InputStream stream)
save File
File file = new File(fileName);
new File(fileName.substring(0, fileName.lastIndexOf('/'))).mkdirs();
file.createNewFile();
OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(file));
byte[] buf = new byte[127];
int read;
while ((read = stream.read(buf)) != -1) {
    outputStream.write(buf, 0, read);
...
voidsaveFile(String filename, String content)
Saves the content to the file.
FileOutputStream fos = null;
BufferedOutputStream bos = null;
try {
    fos = new FileOutputStream(filename);
    bos = new BufferedOutputStream(fos);
    bos.write(content.getBytes(), 0, content.length());
} finally {
    if (bos != null)
...
voidsaveFile(String fileName, String contents)
save File
if (contents == null) {
    return;
contents = contents.trim();
FileOutputStream out = null;
try {
    out = new FileOutputStream(new File(fileName), false);
    out.write(contents.getBytes());
...
voidsaveFile(String name, byte[] data)
Save data to a file.
saveFile(new File(name), data);
voidsaveFile(String path, byte[] content)
save File
try {
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(content);
    try (OutputStream outputStream = new FileOutputStream(path)) {
        int data;
        while ((data = byteArrayInputStream.read()) != -1) {
            outputStream.write(data);
} catch (IOException e) {
    throw e;
voidsaveFile(String pathFile, String data)
save File
FileOutputStream out = null;
try {
    File f = new File(pathFile);
    f.delete();
    out = new FileOutputStream(pathFile);
    out.write(data.getBytes());
} finally {
    if (out != null) {
...