Android 文件的保存和读取
16lz
2021-01-23
Android 给我们提供了两个方法返回输入、输出流,分别为:openFileInput(String fileName)
、openFileOutput(String fileName,int mode);
下面看一自己写了一个简单的例子:
FileService.java
、openFileOutput(String fileName,int mode);
下面看一自己写了一个简单的例子:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <EditText android:id="@+id/fileName" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入文件名称" > <requestFocus /> </EditText> <EditText android:id="@+id/fileContent" android:layout_width="match_parent" android:layout_height="200dp" /> <LinearLayout android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" > <Button android:id="@+id/saveData" android:text="保存" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/readData" android:text="读取" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout></LinearLayout>
public class FileActivity extends Activity { EditText fileName; EditText fileContent; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); fileName = (EditText)findViewById(R.id.fileName); fileContent = (EditText)findViewById(R.id.fileContent); Button saveData = (Button)findViewById(R.id.saveData); saveData.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {OutputStream output;try { output = FileActivity.this.openFileOutput(fileName.getText().toString(), Context.MODE_APPEND);FileService.save(output, fileContent.getText().toString());Toast.makeText(getApplicationContext(), "保存成功", Toast.LENGTH_LONG).show();} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}); Button readData = (Button)findViewById(R.id.readData); readData.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {try {InputStream intput = FileActivity.this.openFileInput(fileName.getText().toString()); String content = FileService.read(intput);Toast.makeText(getApplicationContext(), content, Toast.LENGTH_LONG).show();} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}); }}
FileService.java
public class FileService {/** * 保存文件 * @param output * @param content * @throws IOException */public static void save(OutputStream output,String content) throws IOException{output.write(content.getBytes());output.close();}/** * 读取文件 * @param input * @throws IOException */public static String read(InputStream intput) throws IOException{ByteArrayOutputStream output = new ByteArrayOutputStream();byte[] bt = new byte[1024];int length = intput.read(bt);while(length != -1){output.write(bt, 0, bt.length);length = intput.read(bt);}byte[] data = output.toByteArray();intput.close();output.close();return new String(data);}}
更多相关文章
- Android实现文件上传功能(接收端用strust2)
- android 获取文件的扩展名和去掉文件的扩展名
- android平台上的文件下载,文件和文件的操作
- Android APK文件安装过程小结
- Android 文件操作
- android从资源文件中读取文件流显示
- android 快速移动sdCard的文件夹
- Android Studio module里面放switch语句报错 R文件找不到