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;

public class Main {
    public static final String XML_HEADER = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";

    public static void writeFile(String directoryPath, String fileName, String content) throws IOException {

        if (notEmptyStr(directoryPath) && notEmptyStr(fileName)) {

            FileOutputStream fos = null;

            try {
                File directory = new File(directoryPath);

                if (!directory.exists()) {
                    directory.mkdirs();
                }
                File file = new File(directoryPath + File.separator + fileName);
                fos = new FileOutputStream(file);
                byte[] contentBytes = content.getBytes("UTF-8");
                fos.write(XML_HEADER.getBytes("UTF-8"));
                fos.write(contentBytes);

            } finally {
                if (fos != null) {
                    fos.close();
                }
            }
        }
    }

    public static boolean notEmptyStr(String value) {

        if (value != null && !"".equals(value)) {
            return true;
        }
        return false;
    }
}