#openFileInput
Explore tagged Tumblr posts
Link
original source : http://www.lucazanini.eu/en/2016/android/saving-reading-files-internal-storage/
You can save and read files in the internal storage as private files which only your app can see except for other apps with root privileges. The absolute path of this folder is data\data\[your app package]\files The class Context provides some methods which help you to make operations such as:
openFileInput(String name) Open a private file associated with this Context’s application package for reading
openFileOutput(String name, int mode) Open a private file associated with this Context’s application package for writing
getFilesDir() Gets the absolute path to the filesystem directory where your internal files are saved
getDir() Creates (or opens an existing) directory within your internal storage space
deleteFile() Deletes a file saved on the internal storage
fileList() Returns an array of files currently saved by your application
and you don’t need to have special permissions in order to use these methods.
For example the following code taken from Using the Internal Storagecreates a file containing the string “hello world!”
where at the line 5, in place of MODE_PRIVATE you can use MODE_APPEND, other constants such as MODE_WORLD_READABLE and MODE_WORLD_WRITEABLE are deprecated.
The following code reads the same file and shows a message with the text “hello world!”
But these examples have a limitation: they can’t be used in sub folders, in other words you can’t create a tree structure of directories and files. If you need to create files in subdirectories you must use the standard technique of java, for example the following code creates a file inside the folder “sub”
and the following code reads the same file
1 note
·
View note
Text
データ格納へのディレクトリパスを取得する
データ格納へのディレクトリパスを取得する File getDataDirectory() File getDownloadCacheDirectory() File getExternalStrageDirectory() String getExternalStorageState() File getRootDirectory() File getExternalStoragePublicDirectory(String type) プリファレンス プリファレンスを取得する プリファレンスからデータを読み込む boolean contains(String key) Map<String, ?> getAll() boolean getBoolean(String key, boolean defValue) float getFloat(String key, float defValue) int getInt(String key, int defValue) long getLong(String key, int defValue) String defString(String key, String defValue) Set<String(String key, String defValue) Set<String> getStringSet(String key, Set<String> defValues) プリファレンスにデータを書き込む SharePreferences.EditorputBoolean(String key,boolean value) SharedPreferences.Editor putFloat(String key, float value) SharePreferences.EditorputInt(String key, long value) SharePreferences.EditorputLong(Stirng key, long value) SharedPreference.EditorputString(Stirng key, String value) SharePreferences.Editorremove(String key) SharedPreferences.Editorclear() boolean commit() assets AssetsManagerを取得する AssetManager getAssets() assets上のファイルを取得する InputStream open(String fileName) InputStream open(String fileName, int accessMode) ACCESS_UNKNOWN ACCESS_RANDOM ACCESS_STREAMING ACCESS_BUFFER assetsディレクトリ内のファイル一覧を取得する String[] list(String path) throws IOEXception ファイル ファイルの情報を読み込む FileInputStream openFileInput(String name) FileInputStream(File file) FileInputStream(FileDescriptor fd) FileInputStream(String path) InputStream(StringStream in) BufferREader(InputStream in) BufferReader(InputStream in) String readLine() void close() BufferedInputStream(InputStream in) int read(byte[] buffer, int offset, int byteCount) ファイルの情報を書き込む FileOutputStream openFileOutput(String name, int mode) FileOutStream(File file) File FileOutputStream(File file, boolean append) FileOutputStream(FileDescriptor fd) FileOutputStream(String path) fileOutputStream(String path,boolean append) OutputStreamWriter(OutputStream out) OutputStreamWriter(OutputStreamout, Charset cs) BufferedWriter(Writer out) void write(String str) void flush() void close() BuferedOutputStream(OutputStream out) void write(byte[] buffer) void flush() void close() MODE_PRIVATE MODE_APPEND MODE_WORLD_READABLE MODE_WORLD_WRITEABLE android.permission.WRITE_EXTERNAL_STROGE
0 notes
Text
android 고급 21강 file2 tacademy
original source: https://youtu.be/vFfbb1bfvhA
openFileInput()은 context의 method이다. 내�� 메모리내의 package의 공간에 접근하는 경우외에는 android에서 제공되는 File class나 file path를 FileInputStrem()에 construct arg로 전달해서 FileInputStrem obj를 만든다.
openFileInput -> InputStreamReader -> BufferedReader -> StringBuilder를 이용해서 읽어낸다.
===========================================================
MODE_PRIVATE이 내장메모리에 쓸때 기본으로적용되는 mode이다.
MODE_APPEND, MODE_WORLD_WRITEABLE등이 있다.
참조) android docs https://developer.android.com/reference/android/content/Context#MODE_PRIVATE
openFileOutput -> BufferedWriter 를 이용해 파일을 쓴다.
===========================================================
===========================================================
===========================================================
===========================================================
===========================================================
모니터하는 path는 미리 존재하고 있어야 하므로 보통 실제 존재하는지 확인하고 observing 작업을 한다. recursive하게 observing하지는 않는다.
===========================================================
===========================================================
===========================================================
===========================================================
0 notes
Text
【追記】データをファイルから読み込む機能
【追記】データをファイルから読み込む機能 MainActivity.java StringBuilder str = new StringBuilder();// try { render = new BufferedReader( new InputStreamReader( new InputStreamReader( openFileInput("memo.dat"))); // String line; while ((line = reader.readLine()) != null) { str.append(line); str.append(System.getProperty("line.separator")); } } catch (IOException e) { e.printStackTrace(); } finaly { try { if (reader != null) { reader.close(); } } catch(IOException e) { e.printStackTrace(); } }
openFileInput public abstract FileInputStream openFileInput(string name)
BufferedReader reader = new BufferedReader( new InputStreamReader(FileInputStream));
0 notes
Text
ファイルデータ保存 try { write = new BufferedWriteer( new OutputStreamWriter( openFileOutput("memo.dat", Context.MODE_PRIVATE))); writer.write(txtMemo.getText().toString()); } catch (IDException e) { e.printStackTrace(); } finaly { try { if (writer != null) { writer.close(); } } catch (IDException e) { e.printStackTrace(); } } openFileOutputメソッド public abstract FileOutputStream openFileOutput(String name, int mode) StringBuilder str = new StringBuilder(); // try { reader = new BufferedReader( new InputStreamReader( openFileInput("memo.daat"))); // String line; while ((line = reader.readLine()) != null) { str.append(line); str.append(System.getProperty("line.separator")); } } catch (IDException e ) { e.printStackTrace(); } finally { try { if (reader != null) { reader.colse(); } } catch (IDException e) { e.printStackTrace(); } } openFileInputメソッド public acstract fileInputStream openFileInput(String name)
0 notes
Text
ファイル出力ストリームオープン
OutStream openFileInput(String fileName, int mode)
0 notes