File read and write operations in the Android system
Last Update:2015-08-26
Source: Internet
Author: User
<span id="Label3"></p>Permissions<pre class="prettyprint"><pre class="prettyprint"><code class=" hljs r"><span class="hljs-keyword">...</span>> <uses-permission android:name=<span class="hljs-string">"android.permission.WRITE_EXTERNAL_STORAGE"</span> /> <span class="hljs-keyword">...</span></manifest></code></pre></pre> <ul> <ul> <li>Write_external_storage has implied Read permissions</li> </ul> </ul>Get the path file under the current application<pre class="prettyprint"><pre class="prettyprint"><code class=" hljs vhdl"><span class="hljs-keyword">File</span><span class="hljs-keyword">file</span><span class="hljs-keyword">new</span><span class="hljs-keyword">File</span>(<span class="hljs-keyword">context</span>.getFilesDir(), filename);</code></pre></pre>Write a file<pre class="prettyprint"><pre class="prettyprint"><code class=" hljs avrasm"><span class="hljs-string">"myfile"</span><span class="hljs-comment">;</span><span class="hljs-string">"Hello world!"</span><span class="hljs-comment">;</span>FileOutputStream outputStream<span class="hljs-comment">;</span>try { outputStream = openFileOutput(filename, Context<span class="hljs-preprocessor">.MODE</span>_PRIVATE)<span class="hljs-comment">;</span> outputStream<span class="hljs-preprocessor">.write</span>(string<span class="hljs-preprocessor">.getBytes</span>())<span class="hljs-comment">;</span> outputStream<span class="hljs-preprocessor">.close</span>()<span class="hljs-comment">;</span>} catch (Exception e) { e<span class="hljs-preprocessor">.printStackTrace</span>()<span class="hljs-comment">;</span>}</code></pre></pre>Cache files<pre class="prettyprint"><pre class="prettyprint"><code class=" hljs cs"><span class="hljs-keyword">public</span><span class="hljs-title">getTempFile</span>(Context context, String url) { File file; <span class="hljs-keyword">try</span> { String fileName = Uri.parse(url).getLastPathSegment(); <span class="hljs-keyword">null</span>, context.getCacheDir()); <span class="hljs-keyword">catch</span> (IOException e) { <span class="hljs-comment">// Error while creating file</span> } <span class="hljs-keyword">return</span> file;}</code></pre></pre>Whether the SD card is available<pre class="prettyprint"><code class=" hljs java"><span class="hljs-comment">/ <span class="hljs-comment">* SD card is writable *</span> /</span><span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">Boolean</span></span> <span class="hljs-title"><span class="hljs-title">isexternalstoragewritable</span></span>() {String state = Environment.getexternalstoragestate ();<span class="hljs-keyword"><span class="hljs-keyword">if</span></span>(Environment.MEDIA_MOUNTED.equals (state)) {<span class="hljs-keyword"><span class="hljs-keyword">return</span></span> <span class="hljs-keyword"><span class="hljs-keyword">true</span></span>; }<span class="hljs-keyword"><span class="hljs-keyword">return</span></span> <span class="hljs-keyword"><span class="hljs-keyword">false</span></span>;}<span class="hljs-comment">/ <span class="hljs-comment">* SD card is readable *</span> /</span><span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">Boolean</span></span> <span class="hljs-title"><span class="hljs-title">isexternalstoragereadable</span></span>() {String state = Environment.getexternalstoragestate ();<span class="hljs-keyword"><span class="hljs-keyword">if</span></span>(Environment.MEDIA_MOUNTED.equals (state) | | Environment.MEDIA_MOUNTED_READ_ONLY.equals (state)) {<span class="hljs-keyword"><span class="hljs-keyword">return</span></span> <span class="hljs-keyword"><span class="hljs-keyword">true</span></span>; }<span class="hljs-keyword"><span class="hljs-keyword">return</span></span> <span class="hljs-keyword"><span class="hljs-keyword">false</span></span>;}</code></pre>Create a file<p><p>Create a public file that is still present when the program is uninstalled</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs cs"><span class="hljs-keyword">public</span><span class="hljs-title">getAlbumStorageDir</span>(String albumName) { <span class="hljs-comment">//Environment.DIRECTORY_PICTURES为文件夹名称,这里使用的是系统常量</span> <span class="hljs-keyword">new</span> File(Environment.getExternalStoragePublicDirectory( Environment.DIRECTORY_PICTURES), albumName); <span class="hljs-keyword">if</span> (!file.mkdirs()) { <span class="hljs-string">"Directory not created"</span>); } <span class="hljs-keyword">return</span> file;}</code></pre></pre><p><p>Create a file that will be deleted when the program is uninstalled</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs cs"><span class="hljs-keyword">public</span><span class="hljs-title">getAlbumStorageDir</span>(Context context, String albumName) { <span class="hljs-comment">//如果没有适合的子目录名称,可以改为调用 getExternalFilesDir() 并传递 null。这将返回外部存储上该应用的专用目录的根目录。</span> <span class="hljs-keyword">new</span> File(context.getExternalFilesDir( Environment.DIRECTORY_PICTURES), albumName); <span class="hljs-keyword">if</span> (!file.mkdirs()) { <span class="hljs-string">"Directory not created"</span>); } <span class="hljs-keyword">return</span> file;}</code></pre></pre> <ul> <ul> <li>The directory names provided by API constants such as Directory_pictures are very important. These directory names ensure that the files are handled correctly by the System. For example, files saved in Directory_ringtones are categorized as ringtones by the system media scanner, not Music.</li> </ul> </ul>deleting files<p><p>General methods</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs sql">myFile.<span class="hljs-operator"><span class="hljs-keyword">delete</span>();</span></code></pre></pre><p><p>If the file is saved in the internal store, you can also request that the Context locate and delete the file by calling DeleteFile ():</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs avrasm">myContext<span class="hljs-preprocessor">.deleteFile</span>(fileName)<span class="hljs-comment">;</span></code></pre></pre>Reference articles<p><p>Save File</p></p> <p style="font-size:12px;"><p style="font-size:12px;">Copyright Notice: This article for Bo Master original article, without Bo Master permission not Reproduced.</p></p> <p><p>File read and write operations in the Android system</p></p></span>