Android之File内部存储和File外部存储
16lz
2021-01-23
File内部存储
- FileOutPutStream FileInputStream
FileActivity 以及对应的xml文件
package com.example.test0508;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;public class FileActivity extends AppCompatActivity { private EditText mEtName; private Button mBtnSave,mBtnShow; private TextView mTvContent; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_file); mEtName = findViewById(R.id.edit_file_name); mBtnSave = findViewById(R.id.btn_file_save); mBtnShow = findViewById(R.id.btn_file_show); mTvContent = findViewById(R.id.tv_file_content); mBtnSave.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { save(mEtName.getText().toString()); } }); mBtnShow.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { mTvContent.setText(read()); } }); } /** * 用来存储数据 */ private void save(String content){ FileOutputStream fileOutputStream = null; try { fileOutputStream = openFileOutput("test.txt",MODE_PRIVATE); fileOutputStream.write(content.getBytes()); } catch (IOException e) { e.printStackTrace(); }finally { if (fileOutputStream != null) { try { fileOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } } /** * 用来读取信息 */ private String read(){ FileInputStream fileInputStream = null; try { fileInputStream = openFileInput("test.txt"); byte[] buffer = new byte[1024]; StringBuffer stringBuffer = new StringBuffer(); int len = 0; while ((len = fileInputStream.read(buffer))>0){ stringBuffer.append(new String(buffer,0,len)); } return stringBuffer.toString(); } catch (IOException e) { e.printStackTrace(); } return ""; }}
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="15dp" > <EditText android:id="@+id/edit_file_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入内容" /> <Button android:id="@+id/btn_file_save" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="保存" android:layout_marginTop="10dp" /> <Button android:id="@+id/btn_file_show" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="显示" android:layout_marginTop="10dp" /> <TextView android:id="@+id/tv_file_content" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" /></LinearLayout>
File外部存储
package com.example.test0508;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;import android.os.Environment;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;public class FileActivity extends AppCompatActivity { private EditText mEtName; private Button mBtnSave,mBtnShow; private TextView mTvContent; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_file); mEtName = findViewById(R.id.edit_file_name); mBtnSave = findViewById(R.id.btn_file_save); mBtnShow = findViewById(R.id.btn_file_show); mTvContent = findViewById(R.id.tv_file_content); mBtnSave.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { save(mEtName.getText().toString()); } }); mBtnShow.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { mTvContent.setText(read()); } }); } /** * 用来存储数据 */ private void save(String content){ FileOutputStream fileOutputStream = null; try {//内部存储 fileOutputStream = openFileOutput("test.txt",MODE_PRIVATE); /** * 外部存储存储到sd卡 */ File dir = new File(Environment.getExternalStorageDirectory(),"skypan"); if (!dir.exists()){ dir.mkdirs(); } File file = new File(dir,"test.txt"); if (!file.exists()){ file.createNewFile(); } fileOutputStream = new FileOutputStream(file); fileOutputStream.write(content.getBytes()); } catch (IOException e) { e.printStackTrace(); }finally { if (fileOutputStream != null) { try { fileOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } } /** * 用来读取信息 */ private String read(){ FileInputStream fileInputStream = null; try {// fileInputStream = openFileInput("test.txt"); File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+File.separator+"skypan","test.txt"); fileInputStream = new FileInputStream(file); byte[] buffer = new byte[1024]; StringBuffer stringBuffer = new StringBuffer(); int len = 0; while ((len = fileInputStream.read(buffer))>0){ stringBuffer.append(new String(buffer,0,len)); } return stringBuffer.toString(); } catch (IOException e) { e.printStackTrace(); } return ""; }}
更多相关文章
- Android 定时获取系统内存信息
- Android 数据存储与读取:文件
- java android 获取手机操作系统相关信息
- Android 串口数据处理
- Android 关于 如何使用外界导入的数据库文件
- Android获取应用的签名信息
- Android获取WiFi ip相关信息