File & I/O
[1]
파일 접근 경로 종류입니다.
getFileDir() : 앱 내부 디렉토리
getCacheDir() : 앱 캐시 디렉토리
외부 저장소 접근시 WRITE_EXTERNAL_STORAGE 권한을 등록하고, Environment.getExternalStorageState() 함수를 호출하여 사용가능를 확인합니다.
ex )
Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState());
참고로 getExternalFilesDir() 내 파일은 앱 삭제시 보존이 안되며, 유지를 원한다면 getExternalStoragePublickDirectory() 함수를 사용하면 됩니다.
ex)
new File(this.getExternalFilesDir(Environment.DIRECTORY_PICTURES), fileName);
new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), fileName);
public static final String MEDIA_BAD_REMOVAL = "bad_removal";
public static final String MEDIA_CHECKING = "checking";
public static final String MEDIA_EJECTING = "ejecting";
public static final String MEDIA_MOUNTED = "mounted";
public static final String MEDIA_MOUNTED_READ_ONLY = "mounted_ro";
public static final String MEDIA_NOFS = "nofs";
public static final String MEDIA_REMOVED = "removed";
public static final String MEDIA_SHARED = "shared";
public static final String MEDIA_UNKNOWN = "unknown";
public static final String MEDIA_UNMOUNTABLE = "unmountable";
public static final String MEDIA_UNMOUNTED = "unmounted";
접근이 되었고 파일 크기를 나타낼땐 android.text.format.Formatter.formatFileSize(this, bytes); 코드를 사용하여 String 값을 얻습니다.
------
Text file I/O version 1.0.1 (download, progress)
/**
* Created by lsh on 2015-11-25.
*/
public class FileManager {
public static String FileManager = "FileManager";
private static int lineCnt;
public static void makeFolder(String dirPath) {
File directory = new File(dirPath);
if (!directory.exists()) {
directory.mkdir();
Log.e(FileManager, "make directory : " + dirPath);
} else {
Log.e(FileManager, "directory exists, path : " + dirPath);
}
}
public static void makeFile(String dirPath, String filePath) {
boolean isSuccess = false;
File dir = new File(dirPath);
File file;
if (dir.isDirectory()) {
file = new File(filePath);
if (file != null && !file.exists()) {
Log.e(FileManager, "make file : " + filePath);
try {
isSuccess = file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
} finally {
Log.e(FileManager, "isFileExists : " + isSuccess);
}
} else {
Log.e(FileManager, "isFileExists : " + filePath);
}
}
}
public static void fileDelect(String filePath) {
File file = new File(filePath);
if (file != null && file.exists()) {
file.delete();
}
}
/**
* text file write
*/
public static void fileWrite(String filePath, String content) {
File file = new File(filePath);
try {
FileOutputStream fos = new FileOutputStream(file, true);
BufferedWriter buw = new BufferedWriter(new OutputStreamWriter(fos, "UTF8"));
buw.write(content);
buw.close();
fos.close();
//Toast.makeText(this, "저장되었습니다.", Toast.LENGTH_SHORT).show();
} catch (FileNotFoundException e) {
Log.e(FileManager, "FileWrite - Path not found!");
} catch (IOException e) {
Log.e(FileManager, "FileWrite - Error!");
}
}
/**
* text file read
*/
public static String fileRead(String filePath) {
String fileRead = "";
File file = new File(filePath);
try {
InputStream fis = new FileInputStream(file);
BufferedReader bufferReader = new BufferedReader(new InputStreamReader(fis));
String content = "", temp = "";
lineCnt = 1;
while ((temp = bufferReader.readLine()) != null) {
content += "\n" + temp;
lineCnt++;
}
fileRead = content;
Log.e(null, content);
} catch (FileNotFoundException e) {
Log.e(FileManager, "FileRead - Path not found!");
} catch (Exception e) {
Log.e(FileManager, "FileRead - Error!");
}
return fileRead;
}
/**
* return type ArrayList<String>
*/
public static ArrayList<String> fileReadArr(String filePath) {
ArrayList<String> fileReadArr = new ArrayList<String>();
File file = new File(filePath);
try {
InputStream fis = new FileInputStream(file);
BufferedReader bufferReader = new BufferedReader(new InputStreamReader(fis));
String content = "", temp = "";
while ((temp = bufferReader.readLine()) != null) {
fileReadArr.add("\n" + temp);
}
Log.e(null, content);
} catch (FileNotFoundException e) {
Log.e("Log.e", "FileRead - Path not found!");
} catch (Exception e) {
Log.e("Log.e", "FileRead - Error!");
}
return fileReadArr;
}
public static String filelineCnt(String filePath) {
lineCnt = 1;
File file = new File(filePath);
try {
InputStream fis = new FileInputStream(file);
BufferedReader bufferReader = new BufferedReader(new InputStreamReader(fis));
while (bufferReader.readLine() != null) {
lineCnt++;
}
bufferReader.close();
fis.close();
} catch (Exception e) {
e.getMessage();
}
return String.valueOf(lineCnt);
}
private void LogAbsolutePath(File dir, File file) {
//절대 경로
Log.e("FolderPath", "" + dir.getAbsolutePath());
Log.e("FilePath", "" + file.getAbsolutePath());
}
}
-------
[2] Rx
-------
[3] kotlin
-------