Skip to content

Instantly share code, notes, and snippets.

@AswinpAshok
Created May 28, 2018 07:10
Show Gist options
  • Save AswinpAshok/46a6e99528b1beead1aa9890dfe7aa67 to your computer and use it in GitHub Desktop.
Save AswinpAshok/46a6e99528b1beead1aa9890dfe7aa67 to your computer and use it in GitHub Desktop.
Build json array of file tree structure.
import android.content.Context;
import android.os.Environment;
import android.util.Log;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Arrays;
public class FileTreeBuilder {
private static FileTreeBuilder mBuilder;
private static final String NAME = "name";
private static final String TYPE = "type";
private static final String CONTENTS = "contents";
private static final String DIRECTORY = "directory";
private static final String FILE = "file";
private static final String FILE_SIZE = "fileSize";
private static final String TAG = "FileTreeBuilder";
public FileTreeBuilder() {
}
public static FileTreeBuilder getInstance() {
if (mBuilder == null) {
mBuilder = new FileTreeBuilder();
}
return mBuilder;
}
public JSONArray getDirectoryContents(File directory) {
JSONArray contentsArray = new JSONArray();
if (directory.isDirectory()) {
File[] files = directory.listFiles();
Arrays.sort(files);
ArrayList <File> directoryList = new ArrayList<>();
ArrayList <File> fileList = new ArrayList<>();
for(int i = 0; i < files.length; i++){
if(files[i].isDirectory()){
directoryList.add(files[i]);
}else {
fileList.add(files[i]);
}
}
directoryList.addAll(fileList);
files = directoryList.toArray(files);
for (int i = 0; i < files.length; i++) {
File file = files[i];
JSONObject fileObject = new JSONObject();
try {
fileObject.put(NAME, file.getName());
fileObject.put(TYPE, getType(file));
fileObject.put(FILE_SIZE, getFileSize(file));
fileObject.put(CONTENTS, getDirectoryContents(file));
} catch (JSONException e) {
e.printStackTrace();
}
contentsArray.put(fileObject);
}
}
return contentsArray;
}
public JSONArray build() {
JSONArray fileTreeArray = new JSONArray();
File rootDirectory = Environment.getExternalStorageDirectory();
JSONObject rootObject = new JSONObject();
try {
rootObject.put(NAME, rootDirectory.getAbsolutePath());
rootObject.put(TYPE, getType(rootDirectory));
rootObject.put(FILE_SIZE, getFileSize(rootDirectory));
rootObject.put(CONTENTS, getDirectoryContents(rootDirectory));
} catch (JSONException e) {
e.printStackTrace();
}
fileTreeArray.put(rootObject);
return fileTreeArray;
}
private String getType(File file) {
String type = FILE;
if (file.isDirectory()) {
type = DIRECTORY;
}
return type;
}
public void saveFile(Context context, String content) {
String filename = "aswin.txt";
String fileContents = content;
FileOutputStream outputStream;
try {
outputStream = context.openFileOutput(filename, Context.MODE_PRIVATE);
outputStream.write(fileContents.getBytes());
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private String getFileSize(File file) {
if (!file.isDirectory()) {
int fileSizeInBytes = Integer.parseInt(String.valueOf(file.length()));
if (fileSizeInBytes > 1024) {
if (fileSizeInBytes > (1024 * 1024)) {
return fileSizeInBytes / (1024 * 1024) + " MB";
} else {
return fileSizeInBytes / (1024) + " KB";
}
} else {
return fileSizeInBytes + " bytes";
}
} else {
return "0 bytes";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment