一, 分类:
1, 内部存储 -- Internal Storage -- 私有的 -- 不要权限
特点:
1,文件只能被当前应用程序访问
2,属于应用程序自己, 程序卸载后, 文件也会被删除
存储的路径:
data/data/应用程序包名/files / ***.***
使用方式:
存:
//TODO 1 , 打开文件输出流 参数: String name 文件名称(带后缀), int mode 文件的操作模式
FileOutputStream fos = openFileOutput("set.txt",MODE_PRIVATE);
//TODO 2, 写入数据
String msg = "这是写入文件的数据";
fos.write(msg.getBytes());
//TODO 3, 关闭流
fos.close();
取:
//TODO 1, 打开文件输入流
FileInputStream fis = openFileInput("set.txt");
//TODO 2, 读取数据
String msg = new String(ByteStreams.toByteArray(fis));
//TODO 3, 关闭流
fis.close();
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
// TODO 写
public void save(View view) {
try {
FileOutputStream fos = openFileOutput("set.txt", MODE_PRIVATE);
String msg = "这是写入文件的数据";
fos.write(msg.getBytes());
fos.close();
Toast.makeText(this, "写入成功", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
}
}
// TODO 读
public void read(View view) {
try {
FileInputStream fis = openFileInput("set.txt");
String msg = new String(ByteStreams.toByteArray(fis)); //TODO 需要加 implementation 'com.google.guava:guava:24.0-android' //guava依赖
fis.close();
Toast.makeText(this, msg , Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
}
}
}
2, 外部存储 - SD卡 -- External Storage -- 私有/公共 -- 需要添加权限
特点:
私有内容: 程序卸载后, 文件删除 -- 只有自己访问
公共内容: 程序卸载后, 文件依然存在 -- 大家都可以访问
路径:
mnt/sdcard
sd卡必须添加权限
读:<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
写: <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
使用方式:
存:
//TODO 1, 得到sd卡中,写入文件的路径
//TODO Environment.getExternalStorageDirectory() 得到sd卡的根目录
File file = new File(Environment.getExternalStorageDirectory(),"msg.txt");
//TODO 2, 声明FileOutputStream
FileOutputStream fis = new FileOutputStream(file);
//TODO 3,通过FileOutputStream 写出数据
String msg = "我是sd卡储存";
fis.write(msg.getBytes());
//TODO 4, 关流
fis.close();
取:
//TODO 1, 得到sd卡中,需要读取的文件路径
File file = new File(Environment.getExternalStorageDirectory(),"msg.txt");
//TODO 2, 声明FileInputStream
FileInputStream fis = new FileInputStream(file);
//TODO 3, 流 -- String
String msg = new String(ByteStreams.toByteArray(fis));
//TODO 4, 关流
fis.close();
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
// TODO 写
public void save(View view) {
try {
File file = new File(Environment.getExternalStorageDirectory(), "msg.txt");
FileOutputStream fos = new FileOutputStream(file);
String msg = "我是sd卡储存" ;
fos.write(msg.getBytes());
fos.close();
Toast.makeText(this, "sd卡储存成功", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
}
}
// TODO 读
public void read(View view) {
try {
File file = new File(Environment.getExternalStorageDirectory(), "msg.txt");
FileInputStream fis = new FileInputStream(file);
String msg = new String(ByteStreams.toByteArray(fis));
fis.close();
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
}
}
}