Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import android.util.Log;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

public class Main {
    public static boolean WriteFileFromUrl(String url, String filename) {
        try {
            URL fileUrl = new URL(url);
            URLConnection connection = fileUrl.openConnection();
            InputStream inputStream = new BufferedInputStream(fileUrl.openStream(), 10240);
            File cacheFile = new File(filename);
            FileOutputStream outputStream = new FileOutputStream(cacheFile);

            byte buffer[] = new byte[1024];
            int dataSize;
            int loadedSize = 0;
            while ((dataSize = inputStream.read(buffer)) != -1) {
                loadedSize += dataSize;
                outputStream.write(buffer, 0, dataSize);
            }

            outputStream.close();
            return true;
        } catch (Exception e) {
            Log.d("#StorageHelper Error:", e.toString());
            return false;
        }
    }
}