package com.example.lenovo.myapplication;import android.content.SharedPreferences;import android.content.res.AssetManager;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.os.Environment;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.EditText;import android.widget.ImageView;import android.widget.TextView;import android.widget.Toast;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.InputStream;import java.io.PrintStream;public class MainActivity extends AppCompatActivity { EditText et1; TextView tv1; ImageView iv1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et1 = (EditText) findViewById(R.id.et1); tv1 = (TextView) findViewById(R.id.tv1); iv1 = (ImageView) findViewById(R.id.iv1); } public void bt_1(View v) { //1-得到SharedPreferences SharedPreferences sharedPreferences = getSharedPreferences("abc", MODE_PRIVATE); //2-得到编译器 SharedPreferences.Editor editor = sharedPreferences.edit(); //3-使用Editor添加数据 editor.putString("a", "abcdfe"); editor.putLong("b", 123456); //4-提交保存 editor.commit(); Toast.makeText(MainActivity.this, "保存成功", Toast.LENGTH_SHORT).show(); } //读取 public void bt_2(View v) { SharedPreferences sharedPreferences = getSharedPreferences("abc", MODE_PRIVATE); String s = sharedPreferences.getString("b", null); Toast.makeText(MainActivity.this, "key=b" + "value=" + s, Toast.LENGTH_SHORT).show(); } //写内部文件 public void bt_3(View v) { //从内存里写入文件 try { //1-得到内部的存储文件 File file = getFilesDir(); String path = file.getAbsolutePath(); Toast.makeText(MainActivity.this, "path=" + path, Toast.LENGTH_SHORT).show(); //2-用输出流写入文件 FileOutputStream fileOutputStream = openFileOutput("text.txt", MODE_PRIVATE); //3-写入文件内容 PrintStream printStream = new PrintStream(fileOutputStream); String str = et1.getText().toString(); printStream.println(str); //printStream.println("自动换行"); printStream.close(); Toast.makeText(MainActivity.this, "保存成功", Toast.LENGTH_SHORT).show(); } catch (Exception ex) { Toast.makeText(MainActivity.this, "保存失败", Toast.LENGTH_SHORT).show(); } } // public void bt_4(View v) { try { //输入流 FileInputStream fis = openFileInput("text.txt"); //1-定义byte[] byte[] b = new byte[1024]; int i = 0;//读到的数据长度 String str1 = ""; //2-循环读 while ((i = fis.read(b)) > 0) { String str = new String(b, 0, i); str1 += str; } fis.close(); tv1.setText(str1); } catch (Exception ex) { } } public void bt_5(View v) { try { //操作assets目录的文件 //1-得到assets目录管理器Manager AssetManager assetManager = getAssets(); //2-操作资产目录,边读边写入 //1)-先读文件到内存 inputstream InputStream inputStream = assetManager.open("abc.jpg"); //2)-写文件到目录outputstream FileOutputStream fileOutputStream = openFileOutput("text.jpg", MODE_PRIVATE); //先读后写入 byte[] b = new byte[1024]; int i = 0; while ((i = inputStream.read(b)) > 0) { fileOutputStream.write(b, 0, i); } fileOutputStream.close(); inputStream.close(); Toast.makeText(MainActivity.this, "保存文件成功", Toast.LENGTH_SHORT).show(); } catch (Exception e) { Toast.makeText(MainActivity.this, "保存文件出错", Toast.LENGTH_SHORT).show(); } } public void bt_6(View v) { //3-得到文件路径 String path = getFilesDir().getAbsolutePath() + "/text.jpg"; Toast.makeText(MainActivity.this, "path=" + path, Toast.LENGTH_SHORT).show(); //2-从内部存储的图片得到Bitmap BitmapFactory.decodeFile("文件路径"); Bitmap bm = BitmapFactory.decodeFile(path); //1-设置图片视图的图片数据来源 iv1.setImageBitmap(bm); } public void bt_7(View v) { //1-判断SC卡是否挂载 if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { //得到文本框内容 String str = et1.getText().toString(); try { //写入 //1-构造输出流 //1)得到文件路径。得到SD卡的根目录 //String path=Environment.getExternalStorageDirectory().getCanonicalPath(); //2)得到包名对应的目录 String path = getExternalFilesDir("Musics").getCanonicalPath(); Toast.makeText(MainActivity.this, "path=" + path, Toast.LENGTH_SHORT).show(); //2)构造 FileOutputStream fos = new FileOutputStream("path"); PrintStream printStream = new PrintStream(fos); printStream.print(str); printStream.close(); fos.close(); Toast.makeText(MainActivity.this, "存储成功", Toast.LENGTH_SHORT).show(); } catch (Exception e) { Toast.makeText(MainActivity.this, "存储失败", Toast.LENGTH_SHORT).show(); } } else { Toast.makeText(MainActivity.this, "SD没有", Toast.LENGTH_SHORT).show(); } } public void bt_8(View v) { //1-判断SC卡是否挂载 if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { try { String path = getExternalFilesDir("Music").getCanonicalPath() + "/text.jpg"; FileInputStream fls=new FileInputStream(path); byte[]b=new byte[1024]; int i=0; String str=""; while ((i=fls.read(b))>0) { str+=new String(b,0,i); } fls.close(); Toast.makeText(MainActivity.this, "读取成功,文件内容:"+str, Toast.LENGTH_SHORT).show(); } catch (Exception e) { Toast.makeText(MainActivity.this, "读取失败", Toast.LENGTH_SHORT).show(); } } else { Toast.makeText(MainActivity.this, "SD没有", Toast.LENGTH_SHORT).show(); } }}
xml