Android/ローカルファイル
提供: 初心者エンジニアの簡易メモ
読み込みサンプル
String html = FileLocalUtil.read(getApplicationContext(), "index.html");
- src/info/nonip/android/lib/util/FileLocalUtil.java
package info.nonip.android.lib.util;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import android.content.Context;
import android.util.Log;
/**
* ファイル読み書きユーティリティ
*/
public class FileLocalUtil {
// ファイル読み込み
public static String read(Context context, String filepath) {
try {
InputStream fileInputStream = context.getAssets().open(filepath);
byte[] readBytes = new byte[fileInputStream.available()];
fileInputStream.read(readBytes);
String str = new String(readBytes);
Log.d("FileUtil", "file=" + filepath + " str=" + str);
return str;
} catch (FileNotFoundException e) {
Log.e("error", e.toString());
return "";
} catch (IOException e) {
Log.e("error", e.toString());
return "";
}
}
}
- src/main/assets/index.html
hoge
assetsはmainの下(javaやresと兄弟の部分に置く)
ローカルファイル削除
File file = new File(ファイル絶対パス); file.delete();
