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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

Io流存入和读取

發布時間:2023/11/27 生活经验 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Io流存入和读取 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1.首先設置布局可以輸入

2.在onCreate()方法中得到 id

3.定義一個方法用來向文件寫入(記得一定要關閉寫入流,要不然可能寫入不了

4,定義一個方法得到輸入的值,并且傳入到寫入文件中

下面程序為關閉后再保存到文件中

public class MainActivity extends AppCompatActivity {EditText editText;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);editText= (EditText) findViewById(R.id.et);}@Overrideprotected void onDestroy()  {super.onDestroy();String et= editText.getText().toString();seave(et);}public  void seave(String input)  {FileOutputStream out = null;BufferedWriter writer = null;try {out = openFileOutput("data", Context.MODE_PRIVATE);writer = new BufferedWriter(new OutputStreamWriter(out));writer.write(input);} catch (Exception e) {}finally {try {if (writer!=null){writer.close();}}catch (IOException e) {e.printStackTrace();}}}
}

?

?

讀取文件夾中的內容

1.FileInputStream is;? 獲取文件

BuffereReader read = new Buff(new Input(is)),

StringBuilder content = new String;? 這是字符串容器,可以將讀取到的文件存入里面以便于補充內容,和修改內容。

?

TextUtils.isEmpty()判斷讀取字符串是否為空,空的時候返回?? true

setText(),將獲取的能容添加到你指定的地方,

setSelection(),將光標固定到最后

  //讀取文件public String load() {FileInputStream is =null;BufferedReader reader = null;StringBuffer content = new StringBuffer();try {is= openFileInput("data");reader = new BufferedReader(new InputStreamReader(is));String line = null;if ((line =reader.readLine())!=null){content.append(line);}} catch (Exception e) {e.printStackTrace();}finally {if (reader!=null){try {reader.close();} catch (IOException e) {e.printStackTrace();}}return content.toString();}}

?徐I改后的Oncreate()

    protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);editText= (EditText) findViewById(R.id.et);String input = load();//獲取到讀取的字符串if (!TextUtils.isEmpty(input)){//判斷讀取的字符是否為空editText.setText(input);//添加到輸入框中editText.setSelection(input.length());//這一句是將光標移動到最后位置以便于添加內容Toast.makeText(getApplicationContext(),input,Toast.LENGTH_SHORT).show();}}

轉載于:https://www.cnblogs.com/lyl123/p/7162679.html

總結

以上是生活随笔為你收集整理的Io流存入和读取的全部內容,希望文章能夠幫你解決所遇到的問題。

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