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

import java.io.UnsupportedEncodingException;

import android.os.Environment;

public class Main {
    public static String mergeFlockedFiles(String FilePath) {

        String result = null;
        try {
            result = java.net.URLDecoder.decode(FilePath, "UTF-8");
        } catch (UnsupportedEncodingException e2) {
            // TODO Auto-generated catch block
            e2.printStackTrace();
        }
        String fileName = result.substring(result.lastIndexOf('/') + 1);
        if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) {

            File flockedFilesFolder = new File(
                    Environment.getExternalStorageDirectory() + File.separator + "FlockLoad");
            System.out.println("FlockedFileDir: " + flockedFilesFolder);
            long leninfile = 0, leng = 0;
            int count = 1, data = 0;
            int bytesRead = 0;
            try {
                File filename = new File(flockedFilesFolder.toString() + "/" + fileName);
                if (filename.exists())
                    filename.delete();
                //BUFFER_SIZE = (int) filename.length();
                System.out.println("filename: " + filename);
                FileOutputStream fos = new FileOutputStream(filename, true);

                while (true) {
                    File filepart = new File(filename + ".part" + count);
                    System.out.println("part filename: " + filepart);
                    if (filepart.exists()) {
                        FileInputStream fis = new FileInputStream(filepart);
                        byte fileBytes[] = new byte[(int) filepart.length()];
                        bytesRead = fis.read(fileBytes, 0, (int) filepart.length());
                        assert (bytesRead == fileBytes.length);
                        assert (bytesRead == (int) filepart.length());
                        fos.write(fileBytes);
                        fos.flush();
                        fileBytes = null;
                        fis.close();
                        fis = null;
                        count++;
                        filepart.delete();
                    } else
                        break;
                }
                fos.close();
                fos = null;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return fileName;
    }
}