Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

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

import java.io.BufferedOutputStream;

import java.io.File;

import java.io.FileOutputStream;

import java.io.UnsupportedEncodingException;

public class Main {
    public static File writeStringToTextFile(String outputFile, String string) {

        try {
            return getFileFromBytes(string.getBytes("UTF-8"), outputFile);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        return null;
    }

    public static File getFileFromBytes(byte[] b, String outputFile) {
        BufferedOutputStream stream = null;
        File file = null;
        FileOutputStream fstream = null;
        try {
            file = new File(outputFile);
            if (!file.exists()) {
                file.createNewFile();
            }
            fstream = new FileOutputStream(file);
            stream = new BufferedOutputStream(fstream);
            stream.write(b);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (stream != null) {
            try {
                stream.close();
                fstream.close();

            } catch (Exception e1) {
                e1.printStackTrace();
            }

        }

        System.gc();

        return file;
    }
}