android写入文件读取文件
来源:互联网 发布:淘宝无法拍照怎么设置 编辑:程序博客网 时间:2024/05/16 04:21
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="net.bwie.localdata.activity.FileActivity"> <Button android:id="@+id/read_file_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="读取文件"/> <Button android:id="@+id/write_file_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="写入文件"/> <TextView android:id="@+id/result_tv" android:text="结果" android:layout_width="wrap_content" android:layout_height="wrap_content"/></LinearLayout>
public class FileActivity extends AppCompatActivity implements View.OnClickListener { protected Button mReadFileBtn; protected Button mWriteFileBtn; protected TextView mResultTv; public static void startActivity(Context context) { context.startActivity(new Intent(context, FileActivity.class)); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); super.setContentView(R.layout.activity_file); initView(); } @Override public void onClick(View view) { if (view.getId() == R.id.read_file_btn) { String result = readFile(); mResultTv.setText(result); } else if (view.getId() == R.id.write_file_btn) { writeFile(); } } // 读取文件 private String readFile() { String filePath = Environment.getExternalStorageDirectory().getPath() + "/abc/"; String fileName = "xyz.txt"; File file = new File(filePath, fileName); BufferedReader br = null; try { br = new BufferedReader(new InputStreamReader(new FileInputStream(file))); String result = ""; String line = ""; while ((line = br.readLine()) != null) { result += line; } return result; } catch (Exception e) { e.printStackTrace(); } finally { if (br != null) { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } } return null; } // 写入文件 private void writeFile() { // 外部存储私有路径:Android文件夹// String privatePath = getExternalFilesDir(null).getPath();// 私有路径不分类为null// String filePath = privatePath + "/abc/"; // 外部存储公共路径:DICM,DOWNLOAD,MUSIC等系统提供的文件夹// String publicPath = Environment// .getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)// .getPath();// String filePath = publicPath + "/abc/"; // 自定义文件路径 String rootPath = Environment.getExternalStorageDirectory().getPath();// 外部存储路径(根目录) String filePath = rootPath + "/abc/"; String fileName = "xyz.txt"; File file = new File(filePath, fileName); FileOutputStream fos = null; try { fos = new FileOutputStream(file); fos.write("asdasdas".getBytes()); Toast.makeText(this, "成功", Toast.LENGTH_SHORT).show(); } catch (Exception e) { e.printStackTrace(); Log.d("1507", "error: " + e.getMessage()); } finally { if (fos != null) { try { fos.close(); } catch (IOException e) { e.printStackTrace(); } } } } private void initView() { mReadFileBtn = (Button) findViewById(R.id.read_file_btn); mReadFileBtn.setOnClickListener(FileActivity.this); mWriteFileBtn = (Button) findViewById(R.id.write_file_btn); mWriteFileBtn.setOnClickListener(FileActivity.this); mResultTv = (TextView) findViewById(R.id.result_tv); }}
阅读全文
0 0
- android写入文件读取文件
- Android 文件的读取与写入
- android 文件系统操作 文件读取与写入
- android文件的写入与读取
- android编程:xml文件读取和写入
- Android内部文件的读取和写入
- Android文件存储(写入,读取,删除)
- Android 文件的读取和写入
- 读取写入文件
- C# 读取写入文件
- 写入、读取文件
- 读取写入ini文件
- ruby读取、写入文件
- 读取写入文件内容
- EXCEL 读取 写入文件
- 文件读取写入
- c++文件写入读取
- java文件读取、写入
- 【膜摸摸】网红步进电机驱动A4988和DRV8825的对比和区别及选用实践 for 四轴CNC雕刻/3D打印
- JavaScript代码风格要素
- CUDA 做排列组合
- JavaScript语法结构,关键字,保留字,变量
- mysql的sql语句优化
- android写入文件读取文件
- BC95 NB-IOT模块通过云平台实现到任意设备的透传
- 音视频解码基础的知识积累
- change、keypress、input和propertychange
- 扩展Spring的几种方式
- Angularjs的入门
- 二维数组的初始化与内存存放关系。
- VB中如何设定COM组件和ActiveX控件的CLSID值
- 学习回顾算法(堆排序算法)