当前位置: 代码迷 >> 综合 >> 初涉android 读写 文件 / SharedPreferences Demo
  详细解决方案

初涉android 读写 文件 / SharedPreferences Demo

热度:3   发布时间:2023-12-28 08:42:42.0

 android官网——读写文件 / 读写SharedPreferences

package com.clc.app17;import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;import androidx.appcompat.app.AppCompatActivity;import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;public class MainActivity extends AppCompatActivity {public static final String TAG = "MainActivity";@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//storefindViewById(R.id.btn_store).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {String fileName = "text2";String content = "123qwe哈哈";try {//得到一个输出流对象//openFileOutput参数fileName只能是文件名,不能带路径,即不能带"/"//对于应用私有文件,系统将文件保存到了特定的位置FileOutputStream output = openFileOutput(fileName, Context.MODE_PRIVATE);//向文件写入数据,再次执行将覆盖上一次内容output.write(content.getBytes());} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}});//readfindViewById(R.id.btn_read).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {String fileName = "text1";StringBuilder stringBuilder = new StringBuilder();try {FileInputStream fileInputStream = openFileInput(fileName);InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, StandardCharsets.UTF_8);BufferedReader bufferedReader = new BufferedReader(inputStreamReader);String line = bufferedReader.readLine();while (line != null){stringBuilder.append(line).append('\n');line = bufferedReader.readLine();}} catch (Exception e) {e.printStackTrace();}finally {String content = stringBuilder.toString();Log.d(TAG, "onClick: "+content);}}});//list filefindViewById(R.id.btn_list).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {String[] fileNames = fileList();for (int i = 0; i < fileNames.length; i++) {Log.d(TAG, "onClick: "+fileNames[i]);}}});//dirfindViewById(R.id.btn_dir).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {File filesDir = getFilesDir();}});//write preferencesfindViewById(R.id.btn_writepreferences).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {SharedPreferences preferences = getPreferences(Context.MODE_PRIVATE);SharedPreferences.Editor edit = preferences.edit();edit.putString("key1","value1");edit.commit();}});//read preferencesfindViewById(R.id.btn_readpreferences).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {SharedPreferences preferences = getPreferences(Context.MODE_PRIVATE);//如果key1已有值,返回其值;如果key1没有值,则返回default1,并不将key1的值设为default1,想设置值请用edit.putXxx();String key1 = preferences.getString("key1","default1");Log.d(TAG, "onClick: "+key1);}});}
}

 

  相关解决方案