Android四大组件-File文件操作
来源:互联网 发布:结构优化 编辑:程序博客网 时间:2024/05/20 00:13
一、创建文件
第一种方法
File file=new File("/mnt/sdcard/test"); if(!file.exists()){ try { file.createNewFile(); Log.i("info","创建成功"); } catch (IOException e) { e.printStackTrace(); } } else { Toast.makeText(MainActivity.this,"已经存在",Toast.LENGTH_LONG).show(); Log.i("info","已经存在"); }
第二种方法
这种方法打开文件,如果不存在会新建一个同名文件
FileOutputStream fos=openFileOutput("a.txt",MODE_PRIVATE);
二、文件操作
少啰嗦直接上代码MainActivity.java
package com.example.tr.file;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;import android.widget.Toast;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;public class MainActivity extends AppCompatActivity implements View.OnClickListener { EditText editText; Button btn_w; Button btn_r; TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); editText= (EditText) findViewById(R.id.edit1); btn_w= (Button) findViewById(R.id.write); btn_r= (Button) findViewById(R.id.read); textView= (TextView) findViewById(R.id.text); btn_w.setOnClickListener(this); btn_r.setOnClickListener(this); File file=new File("/mnt/sdcard/test"); if(!file.exists()){ try { file.createNewFile(); Log.i("info","创建成功"); } catch (IOException e) { e.printStackTrace(); } } else { Toast.makeText(MainActivity.this,"已经存在",Toast.LENGTH_LONG).show(); Log.i("info","已经存在"); } //; /mnt/sdard/Android/data<包名> } public void WriteFiles(String content){ try { FileOutputStream fos=openFileOutput("a.txt",MODE_PRIVATE); fos.write(content.getBytes()); fos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public String readFiles(){ String content=null; FileInputStream fis= null; try { fis = openFileInput("a.txt"); ByteArrayOutputStream baos=new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len=0; while((len=fis.read(buffer))!=-1){ baos.write(buffer,0,len); } content=baos.toString(); fis.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return content; } @Override public void onClick(View view) { switch (view.getId()){ case R.id.write: WriteFiles(editText.getText().toString()); break; case R.id.read: textView.setText(readFiles()); break; } }}textView.setText(readFiles()); break; }}}
main.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <EditText android:id="@+id/edit1" android:layout_width="match_parent" android:layout_height="50dp" /> <Button android:id="@+id/write" android:layout_width="match_parent" android:layout_height="50dp" android:text="写入" /> <Button android:id="@+id/read" android:layout_width="match_parent" android:layout_height="50dp" android:text="读出" /> <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="match_parent" /></LinearLayout>
0 0
- Android四大组件-File文件操作
- Android四大组件及入门操作总结
- Android四大组件-SQLite数据库操作
- android 文件操作FILE
- Android文件操作File
- Android应用程序四大组件
- Android应用程序四大组件
- Android应用程序四大组件
- android 四大组件
- Android应用程序四大组件
- android应用程序四大组件
- Android四大组件
- Android应用程序四大组件
- Android四大组件
- Android四大组件
- android之四大组件
- Android四大组件介绍
- android四大组件
- C#中线程间操作无效: 从不是创建控件 txtBOX 的线程访问它
- [深度学习论文笔记][Semantic Segmentation] Learning Deconvolution Network for Semantic Segmentation
- oracle中表空间详解
- 不用strcmp实现字典序排列(不知道是否正确
- Spring注解的使用
- Android四大组件-File文件操作
- Linux调试器gdb和工程管理器make
- HADOOP安装过程问题总结
- 快速幂
- [C++]拓扑排序
- [gdc15]<暗黑破坏神3>中的合作模式设计
- ReLU激活函数:简单之美
- 小程序_201611
- marquee 和JS 滚动效果