Android Open Source - BusTicketer File Handler






From Project

Back to project page BusTicketer.

License

The source code is released under:

Copyright (c) 2013, Nelspike All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: Red...

If you think the Android project BusTicketer 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 bus.ticketer.utils;
//  w  w  w  .  j a  v a2s .  c om
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

import bus.ticketer.objects.Ticket;

import android.os.Environment;
import android.util.SparseArray;

public class FileHandler {

  private String filename;
  private String toWrite;

  public FileHandler(String filename, String toWrite) {
    this.filename = filename;
    this.toWrite = toWrite;
  }

  public FileHandler() {
  }

  public File getAlbumStorageDir(String filename) {
    File file = new File(
        Environment
            .getExternalStoragePublicDirectory(Environment.DIRECTORY_NOTIFICATIONS),
        filename);
    if (!file.mkdirs()) {
    }
    return file;
  }

  public void writeToFile() {
    File directory = getAlbumStorageDir("BusTicketer");
    File file = new File(directory, filename);

    FileOutputStream outputStream = null;
    try {
      outputStream = new FileOutputStream(file);
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    }

    try {
      outputStream.write(toWrite.getBytes());
      outputStream.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  public ArrayList<String> readFromFile() {
    String scan;
    ArrayList<String> ret = new ArrayList<String>();
    File directory = getAlbumStorageDir("BusTicketer");
    File file = new File(directory, filename);

    if(!file.exists()) {
      try {
        file.createNewFile();
      } catch (IOException e) {
      }
    }
    
    FileReader fileReader = null;
    try {
      fileReader = new FileReader(file);
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    }
    BufferedReader br = new BufferedReader(fileReader);

    try {
      while ((scan = br.readLine()) != null)
        ret.add(scan);
    } catch (IOException e) {
      e.printStackTrace();
    }
    try {
      br.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
    
    return ret;
  }
  
  public void deleteFile() {
    File directory = getAlbumStorageDir("BusTicketer");
    File file = new File(directory, filename);
    
    file.delete();
  }
  
  public static boolean checkFileExistance(String name) {
    File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_NOTIFICATIONS), "BusTicketer");
    File[] files = file.listFiles();
    
    for(File f : files) {
      if(f.getName().equals(name))
        return true;
    }
    
    return false;
  }
  
  public static SparseArray<ArrayList<Ticket>> getTicketCount() {
    File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_NOTIFICATIONS), "BusTicketer");
    File[] files = file.listFiles();
    SparseArray<ArrayList<Ticket>> ret = new SparseArray<ArrayList<Ticket>>();
    ArrayList<Ticket> t1Tickets = new ArrayList<Ticket>();
    ArrayList<Ticket> t2Tickets = new ArrayList<Ticket>();
    ArrayList<Ticket> t3Tickets = new ArrayList<Ticket>();
    
    for(File f : files) {
      if(f.getName().contains("t1Ticket"))
        t1Tickets.add(new Ticket(Integer.parseInt(f.getName().split("-")[1].split("\\.")[0])));
      else if(f.getName().contains("t2Ticket"))
        t2Tickets.add(new Ticket(Integer.parseInt(f.getName().split("-")[1].split("\\.")[0])));
      else if(f.getName().contains("t3Ticket"))
        t3Tickets.add(new Ticket(Integer.parseInt(f.getName().split("-")[1].split("\\.")[0])));
    }
    
    ret.append(1, t1Tickets);
    ret.append(2, t2Tickets);
    ret.append(3, t3Tickets);
    
    return ret;
  }
  
  public void deleteFiles() {
    File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_NOTIFICATIONS), "BusTicketer");
    File[] files = file.listFiles();
    
    for(File f : files)
      f.delete();
  }
  
  public void setToWrite(String toWrite) {
    this.toWrite = toWrite;
  }

  public String getUsername() {
    filename = "client";
    return readFromFile().get(0);
  }
  
}




Java Source Code List

bus.ticketer.adapters.CentralPagerAdapter.java
bus.ticketer.adapters.DialogAdapter.java
bus.ticketer.adapters.SpinnerAdapter.java
bus.ticketer.connection.ConnectionRunnable.java
bus.ticketer.connection.ConnectionRunnable.java
bus.ticketer.connection.ConnectionRunnable.java
bus.ticketer.connection.ConnectionThread.java
bus.ticketer.connection.ConnectionThread.java
bus.ticketer.connection.ConnectionThread.java
bus.ticketer.filters.MonthInputFilter.java
bus.ticketer.fragments.BuyTicketsFragment.java
bus.ticketer.fragments.ShowTicketsFragment.java
bus.ticketer.inspector.BusInspector.java
bus.ticketer.inspector.MainActivity.java
bus.ticketer.inspector.ResultActivity.java
bus.ticketer.inspector.ScanActivity.java
bus.ticketer.listeners.BusTabListener.java
bus.ticketer.listeners.PurchaseListener.java
bus.ticketer.listeners.RadioGroupListener.java
bus.ticketer.listeners.SwipeListener.java
bus.ticketer.listeners.TicketPurchaseListener.java
bus.ticketer.listeners.ValidationListener.java
bus.ticketer.objects.Ticket.java
bus.ticketer.passenger.BeamActivity.java
bus.ticketer.passenger.BusTicketer.java
bus.ticketer.passenger.CentralActivity.java
bus.ticketer.passenger.MainActivity.java
bus.ticketer.runnable.BuyRunnable.java
bus.ticketer.runnable.ShowRunnable.java
bus.ticketer.runnable.TimerRunnable.java
bus.ticketer.runnable.TimerThread.java
bus.ticketer.utils.BusTimer.java
bus.ticketer.utils.BusUtils.java
bus.ticketer.utils.FileHandler.java
bus.ticketer.utils.FileWriter.java
bus.ticketer.utils.Method.java
bus.ticketer.utils.Method.java
bus.ticketer.utils.Method.java
bus.ticketer.utils.RESTFunction.java
bus.ticketer.utils.RESTFunction.java
bus.ticketer.utils.RESTFunction.java
bus.ticketer.utils.SpinnerAdapter.java
bus.ticketer.validation.MainActivity.java
bus.ticketer.validation.ResultActivity.java