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.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.os.Environment;
import android.util.Log;

public class Main {
    /**
     * get the FileOutputStream
     * @param filePath the filePath must contain head "/"
     * @return
     */
    public static FileOutputStream getFileOutputStream(String filePath) {
        FileOutputStream fouts = null;
        File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + filePath.trim());

        if (file.exists() && file.isFile()) {
            try {
                fouts = new FileOutputStream(file);
                Log.d("Ragnarok", "get the fouts path = " + file.getAbsolutePath());
                return fouts;
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } else {
            try {
                File fileDirs = new File(file.getParent());
                fileDirs.mkdirs();
                //Log.d(LOG_TAG, "make the fileDirs " + fileDirs.getPath());
                //file.createNewFile();   
                //Log.d("Ragnarok", "create a new file name " + file.getName());
                Log.d("Ragnarok", "file path " + file.getAbsolutePath());
                synchronized (file) {
                    file.createNewFile();
                    fouts = new FileOutputStream(file);
                    return fouts;
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
        return null;
    }
}