Last active
July 10, 2016 14:04
-
-
Save webserveis/f00e652a079e8e092b069d234610f905 to your computer and use it in GitHub Desktop.
Android Helper class for Filesystem
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.net.Uri; | |
import java.io.BufferedReader; | |
import java.io.BufferedWriter; | |
import java.io.File; | |
import java.io.FileInputStream; | |
import java.io.FileOutputStream; | |
import java.io.InputStreamReader; | |
import java.io.OutputStreamWriter; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.Locale; | |
import java.util.Scanner; | |
/* | |
http://stackoverflow.com/questions/14037404/java-read-large-text-file-with-70million-line-of-text | |
http://stackoverflow.com/questions/20311266/read-line-with-scanner | |
http://stackoverflow.com/questions/2016894/how-to-split-a-large-text-file-into-smaller-files-with-equal-number-of-lines | |
http://stackoverflow.com/questions/2735323/how-to-read-and-write-utf-8-to-disk-on-the-android | |
http://stackoverflow.com/questions/15183761/how-to-check-the-end-of-line-using-scanner | |
*/ | |
public class FileDirUtils { | |
private static final String TAG = FileDirUtils.class.getSimpleName(); | |
public static final String UTF8 = "UTF-8"; | |
private static final int BUFFER_SIZE = 1024; | |
private static final String BREAK_LINE_WIN = "\r\n"; | |
private static final String BREAK_LINE_UNIX = "\n"; | |
private static final String BREAK_LINE_SYSTEM = System.getProperty("line.separator"); | |
public static boolean existDirectory(File dir) { | |
boolean result = false; | |
if (dir.isDirectory()) { | |
result = true; | |
} | |
return result; | |
} | |
public static boolean createDirectory(File dir) { | |
return !existDirectory(dir) && dir.mkdir(); | |
} | |
public static boolean writeFile(File filePath, String content) throws Exception { | |
boolean result = filePath.exists() || filePath.createNewFile(); | |
FileOutputStream fos = new FileOutputStream(filePath, false); //true for append false for new | |
// get the content in bytes | |
byte[] contentInBytes = content.getBytes(); | |
fos.write(contentInBytes); | |
fos.flush(); | |
fos.close(); | |
return result; | |
} | |
public static void createFile(File file) throws Exception { | |
FileOutputStream oFile = new FileOutputStream(file, false); | |
oFile.close(); | |
} | |
public static boolean writeFileByLine(File filePath, String content) throws Exception { | |
boolean result = filePath.exists() || filePath.createNewFile(); | |
BufferedWriter out = new BufferedWriter(new OutputStreamWriter( | |
new FileOutputStream(filePath), UTF8), BUFFER_SIZE); | |
//BufferedWriter writer = new BufferedWriter(new FileWriter(filePath )); //true for append false for new | |
Scanner sc = new Scanner(content); | |
while (sc.hasNextLine()) { | |
String line = sc.nextLine(); | |
out.write(line); | |
if (sc.hasNextLine()) | |
out.write(BREAK_LINE_SYSTEM); | |
//out.write(); //writer.newLine(); | |
} | |
out.flush(); | |
out.close(); | |
return result; | |
} | |
public static String readFileByLine(File filePath) throws Exception { | |
BufferedReader br = new BufferedReader(new InputStreamReader( | |
new FileInputStream(filePath), UTF8), BUFFER_SIZE); | |
StringBuilder sb = new StringBuilder(); | |
String mLine = br.readLine(); | |
while (mLine != null) { | |
sb.append(mLine); // process line | |
mLine = br.readLine(); | |
if (mLine != null) sb.append(BREAK_LINE_SYSTEM); | |
} | |
return sb.toString(); | |
/*FileInputStream fis = new FileInputStream(filePath); | |
StringBuilder fileContent = new StringBuilder(""); | |
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 String getName(String filePath) { | |
if (filePath == null) return null; | |
Uri uri = Uri.parse(filePath); | |
return uri.getLastPathSegment(); | |
} | |
public static String getBaseName(String filePath) { | |
if (filePath == null) return null; | |
Uri uri = Uri.parse(filePath); | |
String fileName = getName(filePath); | |
String[] tokens = fileName.split("\\.(?=[^\\.]+$)"); | |
return tokens[0]; | |
} | |
public static String getExtension(String fileName) { | |
if (fileName == null) return null; | |
int i = fileName.lastIndexOf('.'); | |
if (i > 0) { | |
return fileName.substring(i + 1); | |
} else return ""; | |
} | |
public static String getPath(String filePath) { | |
if (filePath == null) return null; | |
Uri uri = Uri.parse(filePath); | |
String result = ""; | |
List<String> parts = new ArrayList<String>(uri.getPathSegments()); | |
parts.remove(parts.size()-1); | |
for (int i = 0; i < parts.size(); ++i) { | |
result += "/" + parts.get(i); | |
} | |
return result; | |
} | |
public static String generateFileNamePattern(String filePath, int count) { | |
if (filePath == null) return null; | |
String ext = (getExtension(filePath).length() > 0) ? "." + getExtension(filePath) : ""; | |
return String.format(Locale.getDefault(), | |
"%s/%s%03d%s", | |
getPath(filePath), | |
getBaseName(filePath), | |
count, | |
ext); | |
} | |
public static long getSize(File file) { | |
if (file == null ) return 0; | |
if (file.exists()) { | |
return file.length(); | |
} else return 0; | |
} | |
public static long getLastMod(File file) { | |
if (file == null ) return 0; | |
if (file.exists()) { | |
return file.lastModified(); | |
} else return 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment