Android Open Source - BarrelRace File Util






From Project

Back to project page BarrelRace.

License

The source code is released under:

MIT License

If you think the Android project BarrelRace 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.app.ui.assignment.barrelrace.util;
//from   w  w  w . j  a v a  2  s  . co m
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

import android.os.Environment;

import com.app.ui.assignment.barrelrace.objects.Score;

/**
* @author Revanth Kumar
* @description Barrel Race Game for Android
* @module FileUtil
*/

/*FileUtil class to write and read for file*/
public class FileUtil {
    private FileOutputStream fileOutputStream;
    private BufferedReader bufferedReader;
    
    /*Write to file*/
    public boolean writeToFile(String stringToWrite) {
        boolean success = false;

        if(isWritableToStorage()) {
            File fileToWrite = new File(Environment.getExternalStorageDirectory(), "highscores.txt"); 
            try {
                if(!fileToWrite.exists()) {
                    fileToWrite.createNewFile();
                }
                fileOutputStream = new FileOutputStream(fileToWrite);
                fileOutputStream.write(stringToWrite.getBytes());
                fileOutputStream.close();
                success = true;
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        return success;
    }
    
    /*Read from File*/
    public ArrayList<Score> fetchHighScores() {
        String str;

        ArrayList<Score> resultArray = new ArrayList<Score>();
        if(isWritableToStorage()) {
            try {
                File file = new File(Environment.getExternalStorageDirectory(), "highscores.txt");
                if(!file.exists()){
                    file.createNewFile();
                }
                bufferedReader = new BufferedReader(new FileReader(file));
                while((str = bufferedReader.readLine()) != null){
                    String[] tempStr = str.split(":");
                    resultArray.add(new Score(tempStr[0], Long.parseLong(tempStr[1])));
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        return resultArray;
    }
    
    /*Check Writable to storage*/
    private boolean isWritableToStorage() {
        String storageState = Environment.getExternalStorageState();

        if(storageState.equals(Environment.MEDIA_MOUNTED)) {
            return true;
        }

        return false;
    }
}




Java Source Code List

com.app.ui.assignment.barrelrace.FailureActivity.java
com.app.ui.assignment.barrelrace.GameActivity.java
com.app.ui.assignment.barrelrace.MainActivity.java
com.app.ui.assignment.barrelrace.SettingsActivity.java
com.app.ui.assignment.barrelrace.SuccessActivity.java
com.app.ui.assignment.barrelrace.ViewScoresActivity.java
com.app.ui.assignment.barrelrace.objects.Barrel.java
com.app.ui.assignment.barrelrace.objects.Fence.java
com.app.ui.assignment.barrelrace.objects.Horse.java
com.app.ui.assignment.barrelrace.objects.Score.java
com.app.ui.assignment.barrelrace.util.CustomAdapter.java
com.app.ui.assignment.barrelrace.util.FileUtil.java
com.app.ui.assignment.barrelrace.util.ScoreComparator.java
com.app.ui.assignment.barrelrace.util.TimerUtil.java
com.app.ui.assignment.barrelrace.views.BarrelRaceView.java