博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
android-读取和保存文件实例
阅读量:3934 次
发布时间:2019-05-23

本文共 2806 字,大约阅读时间需要 9 分钟。

敲多了就会了,哈哈哈!一起加油!

注意路径我是用的雷电模拟器

layout.xml

<?xml version="1.0" encoding="utf-8"?>

main.java

public class MainActivity extends AppCompatActivity {

Button savebtn,readbtn,savesbtn,readsbtn;
EditText edit;
String fileName = “test.txt”;
String str;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edit = findViewById(R.id.edit);
savebtn = findViewById(R.id.savebtn);
savebtn.setOnClickListener(new mClick());
savesbtn = findViewById(R.id.savesbtn);
savesbtn.setOnClickListener(new mClick());
readbtn = findViewById(R.id.readbtn);
readbtn.setOnClickListener(new mClick());
readsbtn = findViewById(R.id.readsbtn);
readsbtn.setOnClickListener(new mClick());
}
private class mClick implements View.OnClickListener {
@Override
public void onClick(View v) {
if (vsavebtn){
savefile();
}
else if (v
readbtn){
readfile(fileName);
}
else if (vsavesbtn){
savesSDcar();
}
else if (v
readsbtn){
readSDcar(fileName);
}
}
}
void readSDcar(String fileName) {
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
File path = Environment.getExternalStorageDirectory();
File sdfile = new File(path,fileName);
try {
FileInputStream in_file = new FileInputStream(sdfile);
byte[] buffer = new byte[1024];
int bytes = in_file.read(buffer);
str = new String(buffer,0,bytes);
Toast.makeText(MainActivity.this,“文件内容”+str,Toast.LENGTH_LONG).show();
} catch (FileNotFoundException e) {
System.out.println(“文件不存在”);
// e.printStackTrace();
} catch (IOException e) {
System.out.println(“IO流错误”);
// e.printStackTrace();
}
}
}
void savesSDcar() {
str = edit.getText().toString();
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED));
{
File path = Environment.getExternalStorageDirectory();
File sdfile = new File(path,fileName);
try {
FileOutputStream f_out = new FileOutputStream(sdfile);
f_out.write(str.getBytes());
Toast.makeText(MainActivity.this,“文件保存SD”,Toast.LENGTH_LONG).show();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
void readfile(String fileName) {
byte[] buffer = new byte[1024];
try {
FileInputStream in_file = openFileInput(fileName);
int bytes = in_file.read(buffer);
str = new String(buffer,0,bytes);
Toast.makeText(MainActivity.this,“文件内容”+str,Toast.LENGTH_LONG).show();
} catch (FileNotFoundException e) {
System.out.println(“文件不存在”);
// e.printStackTrace();
} catch (IOException e) {
System.out.println(“IO流错误”);
// e.printStackTrace();
}
}
void savefile() {
str = edit.getText().toString();
try {
FileOutputStream f_out = openFileOutput(fileName, Context.MODE_PRIVATE);
f_out.write(str.getBytes());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

转载地址:http://dkegn.baihongyu.com/

你可能感兴趣的文章
Dubbo与Zookeeper、SpringMVC整合和使用
查看>>
sql查询优化
查看>>
mysql乐观锁总结和实践(含demo例子)
查看>>
mysql悲观锁总结和实践
查看>>
IO操作中关闭流的注意点(多个关闭时的异常需要单独处理)
查看>>
java中io读写时流的关闭注意,代码查错
查看>>
redis针对不同场景进行相关的策略的记录,临时未代码实现
查看>>
MySQL事务隔离级别详解
查看>>
dubbo使用经验及实现原理简单介绍(转载)
查看>>
jvm虚拟机优化以及案例(转载)
查看>>
草根程序员进入BAT
查看>>
Spring 框架的设计理念与设计模式分析
查看>>
ant + svn 自动部署项目
查看>>
TCP/IP,http,socket,长连接,短连接
查看>>
java对html转移的语言反编译-org.apache.commons.lang3包有个StringEscapeUtils
查看>>
PostgreSQL 9.5 连接redis及其使用
查看>>
redis_fdw使用简介
查看>>
python在Eclipse中集成pydev,以及window安装settools、requests
查看>>
MySQL中的日期计算unix_timestamp
查看>>
redis的消息订阅/发布总结
查看>>