Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.io.BufferedReader;
import java.io.File;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

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

public class Main {
    @SuppressWarnings("resource")
    public static void writeInfoTophone(Context context, InputStream scriptData, String filePath)
            throws IOException {
        String temp = null;
        Log.d("Tag", "Starting write file");
        FileOutputStream fos = null;
        if (isSdcardAvailable()) {
            removeExistFile(filePath);
            fos = new FileOutputStream(filePath, true);
        } else {
            removeExistFile(filePath);
            fos = context.openFileOutput(filePath, Context.MODE_APPEND);
        }
        BufferedReader mBufferedReader = new BufferedReader(new InputStreamReader(scriptData));
        while ((temp = mBufferedReader.readLine()) != null) {
            fos.write((temp + "\n").getBytes());
        }
        fos.close();
    }

    /**
     * if sdcard available return true ,else return false
     * */
    public static boolean isSdcardAvailable() {
        String sdcardStatus = Environment.getExternalStorageState();
        return sdcardStatus.equals(Environment.MEDIA_MOUNTED) ? true : false;
    }

    private static void removeExistFile(String path) {
        if (path != null) {
            File file = new File(path);
            if (file.exists()) {
                file.delete();
            }
        }
    }
}