Android Open Source - Android-Project-Manager File Operations






From Project

Back to project page Android-Project-Manager.

License

The source code is released under:

GNU General Public License

If you think the Android project Android-Project-Manager 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.vaquerosisd.utils;
//from   w  w  w  . j av a2s  .  c om
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.ArrayList;

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;
import android.util.Log;

public class FileOperations {
  private Context context;
  
  public FileOperations(Context context){
    this.context = context;
  }
  
  //Create file
    public void createFile(String fileName, String fileText) {
    new File(context.getFilesDir(), fileName);
    FileOutputStream outputStream;
    try {
      outputStream = context.openFileOutput(fileName, Context.MODE_APPEND);
      outputStream.write(fileText.getBytes());
      outputStream.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
    
  //Create String file if it doesn't exist
  public void createStringFile(String fileName, String textFile) {
    if(!isFile(fileName)) {
      createFile(fileName, textFile);
    }
  }
  
  //Append text
  public void appendText(String fileName, String text) {
    FileOutputStream outputStream;
    try {
      outputStream = context.openFileOutput(fileName, Context.MODE_APPEND);
      outputStream.write(text.getBytes());
      outputStream.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  
  //Reads internal file
  public String readFile(String filename) {
    try {
      FileInputStream inputStream = context.openFileInput(filename);
      InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
      BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
      StringBuilder stringBuilder = new StringBuilder();
      String line;
      while ((line = bufferedReader.readLine()) != null) {
        stringBuilder.append(line).append("\n");
      }
      String fileText = stringBuilder.toString();
      return fileText;
    } catch (Exception e) {
      e.printStackTrace();
    }
    return "";
  }
  
  //Converts String to ArrayList
  public ArrayList<String> convertToStringList(String text) {
    ArrayList<String> statusList = new ArrayList<String>();
    String[] stringArray = text.split("\n");
    for(int i =0; i < stringArray.length; i++){
      statusList.add(stringArray[i]);
    }
    return statusList;
    
  }
  
  //Checks it there is an existing file in internal storage
  public boolean isFile(String fileName) {
    File directory = context.getFilesDir();
    for(String file : directory.list()){
      if(file.equals(fileName)){
        return true;
      }
        
    }
    return false;
  }
  
  public static void moveFile(String inputPath, String outputPath) {

      InputStream in = null;
      OutputStream out = null;
      try {

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

          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
          out.flush();
          out.close();
          out = null;

          // delete the original file
          new File(inputPath).delete();
          File f = new File(outputPath);
          f.setReadable(true);

      } catch (FileNotFoundException fnfe1) {
          Log.e("tag", fnfe1.getMessage());
      } catch (Exception e) {
          Log.e("tag", e.getMessage());
      }
  }
  
  public String getAudioFilePathFromUri(Uri uri) {
    Cursor cursor = context.getContentResolver().query(uri, null, null, null, null);
    cursor.moveToFirst();
    int index = cursor.getColumnIndex(MediaStore.Audio.AudioColumns.DATA);
    return cursor.getString(index);
  }
  
  public static ArrayList<String> getFilesByExtension(String path, String extension) {    
    ArrayList<String> filteredFiles = new ArrayList<String>();
    File directory = new File(path);
    
    String[] files = directory.list();
    
    int extLength = extension.length();
    String tempComp;
    for(int i = 0; i < files.length; i++){
      tempComp = files[i].substring(files[i].length()-extLength, files[i].length());
      if(tempComp.equals(extension)) {
        Log.i("Debug", files[i]);
        filteredFiles.add(files[i]);
      }
    }
    
    return filteredFiles;
  }
  
  public static void deleteFolder(String path) {
    File directory = new File(path);
    if(directory.isDirectory()) {
      String[] children = directory.list();
      for (int i = 0; i < children.length; i++) {
              new File(path, children[i]).delete();
          }
    }
    directory.delete();
  }
  
}




Java Source Code List

com.vaquerosisd.adapters.ProjectListViewAdapter.java
com.vaquerosisd.adapters.TaskListViewAdapter.java
com.vaquerosisd.adapters.VideoNoteGridViewAdapter.java
com.vaquerosisd.adapters.VoiceNoteGridViewAdapter.java
com.vaquerosisd.database.DatabaseHelper.java
com.vaquerosisd.database.ProjectOperations.java
com.vaquerosisd.dialog.CustomStatus.java
com.vaquerosisd.dialog.DatePickerFragment.java
com.vaquerosisd.dialog.DeleteDialog.java
com.vaquerosisd.dialog.DeletePhotoDialog.java
com.vaquerosisd.dialog.DeleteTaskDialog.java
com.vaquerosisd.dialog.EditProject.java
com.vaquerosisd.fragments.TaskDescriptionFragment.java
com.vaquerosisd.fragments.VideoNotesFragment.java
com.vaquerosisd.fragments.VoiceNotesFragment.java
com.vaquerosisd.object.JsonWrapper.java
com.vaquerosisd.object.PhotoRef.java
com.vaquerosisd.object.Project.java
com.vaquerosisd.object.Task.java
com.vaquerosisd.object.User.java
com.vaquerosisd.object.WebServiceManager.java
com.vaquerosisd.projectmanager.About.java
com.vaquerosisd.projectmanager.ContentTask.java
com.vaquerosisd.projectmanager.Login.java
com.vaquerosisd.projectmanager.MainActivity.java
com.vaquerosisd.projectmanager.NewAccount.java
com.vaquerosisd.projectmanager.NewProject.java
com.vaquerosisd.projectmanager.NewTask.java
com.vaquerosisd.projectmanager.OnSwipeListener.java
com.vaquerosisd.projectmanager.PhotoManager.java
com.vaquerosisd.projectmanager.ProjectList.java
com.vaquerosisd.projectmanager.TaskList.java
com.vaquerosisd.projectmanager.WebserviceCallback.java
com.vaquerosisd.utils.FileOperations.java
com.vaquerosisd.utils.OnSwipeListener.java
com.vaquerosisd.utils.SpinnerUtil.java
com.vaquerosisd.utils.TabListener.java