「Android/ローカルファイル」の版間の差分
提供: 初心者エンジニアの簡易メモ
細 (Admin がページ「Android/ローカルファイル読み書き」を「Android/ローカルファイル」に、リダイレクトを残さずに移動しました) |
|||
| (同じ利用者による、間の1版が非表示) | |||
| 行32: | 行32: | ||
} | } | ||
| − | *assets/index.html | + | *src/main/assets/index.html |
hoge | hoge | ||
assetsはmainの下(javaやresと兄弟の部分に置く) | assetsはmainの下(javaやresと兄弟の部分に置く) | ||
| + | |||
| + | ==ローカルファイル削除== | ||
| + | File file = new File(ファイル絶対パス); | ||
| + | file.delete(); | ||
2019年5月14日 (火) 18:39時点における最新版
読み込みサンプル
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();
