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.FileWriter;
import java.io.InputStream;
import java.io.OutputStream;
import android.util.Log;

public class Main {

    public static boolean writeFile(String filePath, String fileName, String content, boolean append) {
        FileWriter fileWriter = null;
        boolean result = false;
        if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) {
            try {

                File file = new File(filePath);
                if (!file.exists()) {
                    file.mkdirs();
                }
                Log.i("file", filePath);
                fileWriter = new FileWriter(filePath + fileName, append);
                fileWriter.write(content);
                result = true;
            } catch (Exception e) {
                e.printStackTrace();
                Log.i("file", e.toString());
            } finally {
                if (fileWriter != null) {
                    try {
                        fileWriter.close();
                    } catch (Exception e1) {
                        e1.printStackTrace();
                    }
                }
            }
        }
        return result;
    }

    public static boolean writeFile(String filePath, InputStream inputStream, boolean append) {
        OutputStream o = null;
        boolean result = false;
        try {
            o = new FileOutputStream(filePath);
            byte data[] = new byte[1024];
            int length = -1;
            while ((length = inputStream.read(data)) != -1) {
                o.write(data, 0, length);
            }
            o.flush();
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (o != null) {
                try {
                    o.close();
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            }
        }
        return result;
    }
}