Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import android.os.Environment;

import java.io.*;

import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.channels.FileChannel;

public class Main {
    static public String downloadFile(String downloadUrl, String fileName, String dir) throws IOException {
        URL url = new URL(downloadUrl);
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.setRequestMethod("GET");
        urlConnection.setDoOutput(true);
        urlConnection.connect();
        int code = urlConnection.getResponseCode();
        if (code > 300 || code == -1) {
            throw new IOException("Cannot read url: " + downloadUrl);
        }
        String filePath = prepareFilePath(fileName, dir);
        String tmpFilePath = prepareTmpFilePath(fileName, dir);
        FileOutputStream fileOutput = new FileOutputStream(tmpFilePath);
        BufferedInputStream inputStream = new BufferedInputStream(urlConnection.getInputStream());
        byte[] buffer = new byte[1024];
        int bufferLength;
        while ((bufferLength = inputStream.read(buffer)) > 0) {
            fileOutput.write(buffer, 0, bufferLength);
        }
        fileOutput.close();
        // move tmp to destination
        copyFile(new File(tmpFilePath), new File(filePath));
        return filePath;
    }

    static public String prepareFilePath(String fileName, String dir) throws IOException {
        File dictionaryRoot = new File(Environment.getExternalStorageDirectory(), dir);
        File dictionaryDirFile = new File(dictionaryRoot, fileName.substring(0, 1));
        if (!dictionaryDirFile.exists()) {
            if (!dictionaryDirFile.mkdirs()) {
                throw new IOException("Cannot create directory: " + dictionaryDirFile);
            }
        }
        return new File(dictionaryDirFile, fileName + ".mp3").getCanonicalPath();
    }

    static public String prepareTmpFilePath(String fileName, String dir) throws IOException {
        File dictionaryRoot = new File(Environment.getExternalStorageDirectory(), dir);
        File dictionaryDirFile = new File(dictionaryRoot, "tmp");
        if (!dictionaryDirFile.exists()) {
            if (!dictionaryDirFile.mkdirs()) {
                throw new IOException("Cannot create directory: " + dictionaryDirFile);
            }
        }
        return new File(dictionaryDirFile, fileName + ".mp3").getCanonicalPath();
    }

    public static void copyFile(File sourceFile, File destFile) throws IOException {
        if (!destFile.exists()) {
            if (!destFile.createNewFile()) {
                throw new IOException("Cannot create file: " + destFile.getCanonicalFile());
            }
        }
        FileChannel source = null;
        FileChannel destination = null;
        try {
            source = new FileInputStream(sourceFile).getChannel();
            destination = new FileOutputStream(destFile).getChannel();
            long count = 0;
            long size = source.size();
            while ((count += destination.transferFrom(source, count, size - count)) < size)
                ;
        } finally {
            if (source != null) {
                source.close();
            }
            if (destination != null) {
                destination.close();
            }
        }
    }
}