Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.OutputStreamWriter;

public class Main {
    public static synchronized void writeToFile(String rootPath, String filename, String data) {

        File file = new File(rootPath);
        if (!file.exists()) {
            file.mkdirs();

        }

        FileOutputStream fOut = null;
        try {

            File savedfile = new File(rootPath + filename);
            savedfile.createNewFile();//create file if not exists
            fOut = new FileOutputStream(savedfile, true); //append content to the end of file
            OutputStreamWriter outWriter = new OutputStreamWriter(fOut);

            outWriter.write(data);

            fOut.flush();
            outWriter.flush();

            fOut.close();
            outWriter.close();
        } catch (IOException e) {

            e.printStackTrace();
        }

    }
}