Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import java.io.ByteArrayInputStream;

import java.io.File;

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

public class Main {

    public static void writeFile(File file, byte[] content) throws IOException {
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                throw new IOException("not crete file=" + file.getAbsolutePath());
            }
        }
        FileOutputStream fileOutputStream = null;
        ByteArrayInputStream bis = null;
        try {
            bis = new ByteArrayInputStream(content);
            fileOutputStream = new FileOutputStream(file, false);
            byte[] buffer = new byte[1024];
            int length = 0;
            while ((length = bis.read(buffer)) != -1) {
                fileOutputStream.write(buffer, 0, length);
            }
            fileOutputStream.flush();
        } finally {
            if (fileOutputStream != null) {
                fileOutputStream.close();
            }
            if (bis != null) {
                bis.close();
            }
        }
    }
}