Android Open Source - GPSDataCollector File Utils






From Project

Back to project page GPSDataCollector.

License

The source code is released under:

GNU General Public License

If you think the Android project GPSDataCollector listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.ymelo.gpsdatacollector.app.utils;
/*from  w  ww.j a va  2s.  c o m*/
import android.content.Context;
import android.os.Environment;
import android.util.Log;

import java.io.*;
import java.util.ArrayList;

/**
 * Created by yohann on 04/01/15.
 */
public class FileUtils {
    private static String DIR = "gps_data";
    static public FileOutputStream getFileWriter(Context context, String fileName) throws IOException {
        if(context == null)
            return null;
        FileOutputStream fos = context.openFileOutput(fileName, Context.MODE_PRIVATE | Context.MODE_APPEND);
        return fos;
    }

    public static FileInputStream getFileInputStream(Context context, String filename) throws FileNotFoundException {
        return context.openFileInput(filename);
    }

    public static String getFileContent(Context context, String filename) throws IOException {
        FileInputStream fis = getFileInputStream(context, filename);
        StringBuffer fileContent = new StringBuffer("");
        byte[] buffer = new byte[1024];
        int n;
        while ((n = fis.read(buffer)) != -1)
        {
            fileContent.append(new String(buffer, 0, n));
        }
        return fileContent.toString();
    }

    public static File[] getFileList(Context context) {
        return context.getFilesDir().listFiles(new FilenameFilter() {
            @Override
            public boolean accept(File dir, String filename) {
                if(filename.contains("gps_")) {
                    return true;
                }
                return false;
            }
        });
    }

    public static void copyFile(String inputPath, String inputFile, String outputPath) {
        InputStream in = null;
        OutputStream out = null;
        try {

            //create output directory if it doesn't exist
            File dir = new File (outputPath);
            if (!dir.exists())
            {
                dir.mkdirs();
            }


            in = new FileInputStream(inputPath + inputFile);
            out = new FileOutputStream(outputPath + inputFile);

            byte[] buffer = new byte[1024];
            int read;
            while ((read = in.read(buffer)) != -1) {
                out.write(buffer, 0, read);
            }
            in.close();
            in = null;

            // write the output file (You have now copied the file)
            out.flush();
            out.close();
            out = null;

        }  catch (FileNotFoundException fnfe1) {
            Log.e("tag", fnfe1.getMessage());
        }
        catch (Exception e) {
            Log.e("tag", e.getMessage());
        }

    }
}




Java Source Code List

com.ymelo.gpsdatacollector.app.ApplicationTest.java
com.ymelo.gpsdatacollector.app.Config.java
com.ymelo.gpsdatacollector.app.DisplayFragment.java
com.ymelo.gpsdatacollector.app.GetAddressTask.java
com.ymelo.gpsdatacollector.app.LocationServiceErrorMessages.java
com.ymelo.gpsdatacollector.app.MainActivity.java
com.ymelo.gpsdatacollector.app.MapFragment.java
com.ymelo.gpsdatacollector.app.NavigationDrawerFragment.java
com.ymelo.gpsdatacollector.app.RecordFragment.java
com.ymelo.gpsdatacollector.app.TaskUpdater.java
com.ymelo.gpsdatacollector.app.TripListFragment.java
com.ymelo.gpsdatacollector.app.utils.FileUtils.java
com.ymelo.gpsdatacollector.app.utils.FragmentFix.java
com.ymelo.gpsdatacollector.app.utils.LocationUtils.java