Java FileOutputStream Create writeFile(String filePath, Object obj)

Here you can find the source of writeFile(String filePath, Object obj)

Description

write File

License

Open Source License

Declaration

public static void writeFile(String filePath, Object obj) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.File;
import java.io.IOException;
import java.io.FileOutputStream;

public class Main {
    public static void writeFile(String filePath, Object obj) {
        FileOutputStream fop = null;
        File file;//from  www .j  av a 2 s.  c  om
        String content = obj.toString();
        try {

            file = new File(filePath);
            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();

            System.out.println("Data added to file " + filePath);

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

Related

  1. writeFile(String file, byte[]... data)
  2. writeFile(String file, byte[]... data)
  3. writeFile(String fileName)
  4. writeFile(String fileOutput, byte[] data)
  5. writeFile(String filePath, byte[] bytes)
  6. writeFile(String filePathName, Object obj)
  7. writeFile(String p_filename, String p_buffer)
  8. writeFile(String path, byte[] contents)
  9. writeFile(String path, byte[] data)