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 pathname, String fileName, String contents)
save File
File filePath = new File(pathname);
if (!filePath.exists())
    filePath.mkdirs();
File file = new File(filePath, fileName);
if (file.exists())
    file.createNewFile();
OutputStream os = null;
try {
...
voidsaveFile(String pathRoot, String subPath, boolean isFoler, InputStream in)
save File
FileOutputStream fos = null;
File file = new File(pathRoot + File.separator + subPath);
if (file.getParentFile().exists() == false) {
    file.getParentFile().mkdirs();
if (isFoler) {
    if (file.exists() == false) {
        file.mkdirs();
...
booleansaveFile(String text, File saveFile)
save File
if (text == null || saveFile == null) {
    return false;
OutputStream outputStream = null;
try {
    outputStream = new FileOutputStream(saveFile);
    outputStream.write(text.getBytes("UTF-8"));
    return true;
...
voidsaveFileBinary(final File file, final byte[] data)
Saves a data in a file.
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file));
try {
    out.write(data);
} finally {
    out.close();
voidSaveFileFromInputStream(InputStream in, String fileName, String path)
Save File From Input Stream
try {
    FileOutputStream fs = new FileOutputStream(path + fileName);
    byte[] buffer = new byte[1024 * 1024];
    int bytesum = 0;
    int byteread = 0;
    while ((byteread = in.read(buffer)) != -1) {
        bytesum += byteread;
        fs.write(buffer, 0, byteread);
...
voidSaveFileFromInputStream(InputStream stream, String path, String filename)
Save File From Input Stream
FileOutputStream fs = new FileOutputStream(path + "/" + filename);
byte[] buffer = new byte[1024 * 1024];
int byteread = 0;
while ((byteread = stream.read(buffer)) != -1) {
    fs.write(buffer, 0, byteread);
    fs.flush();
fs.close();
...
FilesaveInFile(String filename, String content, String charsetName)
Save in a file the content of a string.
InputStream is = new ByteArrayInputStream(content.getBytes(charsetName));
byte[] b = new byte[2048];
int length;
OutputStream os = new FileOutputStream(filename);
while ((length = is.read(b)) != -1) {
    os.write(b, 0, length);
os.close();
...
StringsaveInputStream(InputStream _input_stream, File _file)
Save an Object to a given filename.
BufferedInputStream input_stream = new BufferedInputStream(_input_stream);
BufferedOutputStream output_stream = new BufferedOutputStream(new FileOutputStream(_file));
int stream_byte;
while ((stream_byte = input_stream.read()) != -1)
    output_stream.write(stream_byte);
output_stream.flush();
input_stream.close();
return _file.getAbsolutePath();
...
voidsaveInputStream(InputStream is, String filePath)
save Input Stream
FileOutputStream fos = null;
try {
    fos = new FileOutputStream(filePath);
    byte[] buf = new byte[1024];
    int len;
    while ((len = is.read(buf)) != -1) {
        fos.write(buf, 0, len);
} catch (IOException e) {
    e.printStackTrace();
} finally {
    if (is != null) {
        try {
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
    if (fos != null) {
        try {
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
voidsaveInt16bit(String filename, int[] intData)
save Intbit
saveBytes(filename, convertIntArrayToByteArray(intData, 2));