日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

内部文件存储

發布時間:2025/3/19 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 内部文件存储 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
內部文件存儲 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:paddingBottom="@dimen/activity_vertical_margin" 7 android:paddingLeft="@dimen/activity_horizontal_margin" 8 android:paddingRight="@dimen/activity_horizontal_margin" 9 android:paddingTop="@dimen/activity_vertical_margin" 10 tools:context="com.hanqi.testapp3.MainActivity" 11 android:orientation="vertical"> 12 13 <TextView 14 android:layout_width="wrap_content" 15 android:layout_height="wrap_content" 16 android:text="Hello World!" 17 android:id="@+id/tv_1"/> 18 19 <EditText 20 android:layout_width="match_parent" 21 android:layout_height="wrap_content" 22 android:hint="輸入..." 23 android:id="@+id/et_1"/> 24 25 <Button 26 android:layout_width="match_parent" 27 android:layout_height="wrap_content" 28 android:text="寫內部文件" 29 android:onClick="bt2_OnClick"/> 30 31 <Button 32 android:layout_width="match_parent" 33 android:layout_height="wrap_content" 34 android:text="讀內部文件" 35 android:onClick="bt3_OnClick"/> 36 </LinearLayout> xml 1 package com.hanqi.testapp3; 2 3 import android.content.SharedPreferences; 4 import android.os.Bundle; 5 import android.support.v7.app.AppCompatActivity; 6 import android.view.View; 7 import android.widget.EditText; 8 import android.widget.TextView; 9 import android.widget.Toast; 10 11 import java.io.File; 12 import java.io.FileInputStream; 13 import java.io.FileOutputStream; 14 import java.io.PrintStream; 15 16 public class MainActivity extends AppCompatActivity { 17 18 EditText et_1; 19 TextView tv_1; 20 21 @Override 22 protected void onCreate(Bundle savedInstanceState) { 23 super.onCreate(savedInstanceState); 24 setContentView(R.layout.activity_main); 25 26 et_1 = (EditText)findViewById(R.id.et_1); 27 tv_1 = (TextView)findViewById(R.id.tv_1); 28 29 } 30 31 //寫內部文件 32 public void bt2_OnClick(View v) 33 { 34 //從內存里寫入文件 35 36 //1、得到內部的存儲目錄 37 try { 38 39 File file = getFilesDir(); 40 41 String path = file.getAbsolutePath(); 42 43 Toast.makeText(MainActivity.this, "path = " + path, Toast.LENGTH_SHORT).show(); 44 45 //2、用輸出流寫入文件 46 FileOutputStream fos = openFileOutput("text.txt",MODE_APPEND); 47 48 //3、寫入文件內容 49 PrintStream ps = new PrintStream(fos); 50 51 String str = et_1.getText().toString(); 52 53 ps.println(str); 54 //ps.println("自動換行"); 55 ps.close(); 56 fos.close(); 57 58 Toast.makeText(MainActivity.this, "保存成功", Toast.LENGTH_SHORT).show(); 59 } 60 catch (Exception e) 61 { 62 Toast.makeText(MainActivity.this, "保存失敗", Toast.LENGTH_SHORT).show(); 63 } 64 65 } 66 67 68 // 69 public void bt3_OnClick (View v) 70 { 71 try { 72 //輸入流 73 FileInputStream fis = openFileInput("text.txt"); 74 75 //1、定義byte[] 76 byte[] b = new byte[1024]; 77 int i = 0; //讀到的數據長度 78 79 String str1 = ""; 80 81 //2、循環讀 82 while ((i = fis.read(b)) > 0) 83 { 84 String str = new String(b, 0, i); 85 86 str1 += str; 87 } 88 89 fis.close(); 90 91 tv_1.setText(str1); 92 } 93 catch (Exception ex) 94 { 95 96 } 97 } 98 99 } Java

?

posted on 2016-05-27 09:30 那消逝的歲月 閱讀(...) 評論(...) 編輯 收藏

轉載于:https://www.cnblogs.com/future-zhenzhen/p/5533475.html

總結

以上是生活随笔為你收集整理的内部文件存储的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。