<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="16dp"> <EditText android:id="@+id/ed_file_save" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/btn_file_save" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="保存内容" /> <Button android:id="@+id/btn_file_read" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="读取内容" /> <TextView android:id="@+id/tv_read_file" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:textColor="#000" android:textSize="14sp" /> </LinearLayout>
package com.example.datasave;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* Created by Devin on 2016/7/19.
*/
public class FileDataActivity extends AppCompatActivity {
private EditText ed_file_save;
private Button btn_file_save;
private Button btn_file_read;
private TextView tv_read_file;
private String fileName = " hello.txt";
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_file);
ed_file_save = (EditText) findViewById(R.id.ed_file_save);
btn_file_save = (Button) findViewById(R.id.btn_file_save);
btn_file_read = (Button) findViewById(R.id.btn_file_read);
tv_read_file = (TextView) findViewById(R.id.tv_read_file);
btn_file_save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String fileContent = ed_file_save.getText().toString();
try {
save(fileContent);
ToastUtils.showToast(FileDataActivity.this, "文件写入成功");
} catch (Exception e) {
e.printStackTrace();
ToastUtils.showToast(FileDataActivity.this, "文件写入失败");
}
}
});
btn_file_read.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
String content = read();
tv_read_file.setText("文件的内容是:" + content);
} catch (IOException e) {
e.printStackTrace();
ToastUtils.showToast(FileDataActivity.this, "读取文件失败!");
}
}
});
}
public void save(String fileContent) throws Exception {
FileOutputStream output = this.openFileOutput(fileName, Context.MODE_PRIVATE);
output.write(fileContent.getBytes());
output.close();
}
public String read() throws IOException {
//打开文件输入流
FileInputStream input = this.openFileInput(fileName);
byte[] temp = new byte[1024];
StringBuffer stringBuffer = new StringBuffer("");
int len = 0;
while ((len = input.read(temp)) > 0) {
stringBuffer.append(new String(temp, 0, len));
}
//关闭输入流
input.close();
return stringBuffer.toString();
}
}
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<EditText android:id="@+id/ed_file_save_sd" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" /> <Button android:id="@+id/btn_file_save_sd" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="写入到SDcard" /> <Button android:id="@+id/btn_file_read_sd" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="从SDcard读取" /> <TextView android:id="@+id/tv_read_file_sd" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:textColor="#000" android:textSize="14sp" />
ed_file_save_sd = (EditText) findViewById(R.id.ed_file_save_sd);
tv_read_file_sd = (TextView) findViewById(R.id.tv_read_file_sd);
btn_file_read_sd = (Button) findViewById(R.id.btn_file_read_sd);
btn_file_read_sd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
String content = readFromSD();
tv_read_file_sd.setText("从SDCard读取到的内容是:" + content);
} catch (Exception e) {
e.printStackTrace();
ToastUtils.showToast(FileDataActivity.this, "读取文件失败!");
}
}
});
btn_file_save_sd = (Button) findViewById(R.id.btn_file_save_sd);
btn_file_save_sd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String content = ed_file_save_sd.getText().toString();
try {
save2SDCard(content);
ToastUtils.showToast(FileDataActivity.this, "文件写入SDCard成功");
} catch (Exception e) {
e.printStackTrace();
ToastUtils.showToast(FileDataActivity.this, "文件写入SDCard失败");
}
}
});
public void save2SDCard(String content) throws Exception {
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { // 如果sdcard存在
String fileName3 = Environment.getExternalStorageDirectory().getCanonicalPath() + File.separator + "test" + File.separator + fileName2;
File file = new File(fileName3);
if (!file.getParentFile().exists()) {
file.getParentFile().mkdir();
}
FileOutputStream fileOutputStream = new FileOutputStream(file);
fileOutputStream.write(content.getBytes());
fileOutputStream.close();
} else {
ToastUtils.showToast(this, "SDCard不存在");
}
}
public String readFromSD() throws Exception {
StringBuffer stringBuffer = new StringBuffer("");
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
String fileName3 = Environment.getExternalStorageDirectory().getCanonicalPath() + File.separator + "test" + File.separator + fileName2;
File file = new File(fileName3);
if (!file.getParentFile().exists()) {
file.getParentFile().mkdir();
}
FileInputStream fileInputStream = new FileInputStream(file);
byte[] temp = new byte[1024];
int len = 0;
while ((len = fileInputStream.read(temp)) > 0) {
stringBuffer.append(new String(temp, 0, len));
}
fileInputStream.close();
} else {
ToastUtils.showToast(this, "SDCard不存在");
}
return stringBuffer.toString();
}
AssetManager assetsManager = getAssets();
InputStream inputStream = assetsManager.open("fileName");
package com.example.datasave.io;
import android.content.Context;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
/**
* IO流 工具类<br>
* 很简单,仅支持文本操作
*/
public class IOUtils {
/**
* 文本的写入操作
*
* @param filePath 文件路径。一定要加上文件名字 <br>
* 例如:../a/a.txt
* @param content 写入内容
* @param append 是否追加
*/
public static void write(String filePath, String content, boolean append) {
BufferedWriter bufw = null;
try {
bufw = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(filePath, append)));
bufw.write(content);
} catch (Exception e1) {
e1.printStackTrace();
} finally {
if (bufw != null) {
try {
bufw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 文本的读取操作
*
* @param path 文件路径,一定要加上文件名字<br>
* 例如:../a/a.txt
* @return
*/
public static String read(String path) {
BufferedReader bufr = null;
try {
bufr = new BufferedReader(new InputStreamReader(
new FileInputStream(path)));
StringBuffer sb = new StringBuffer();
String str = null;
while ((str = bufr.readLine()) != null) {
sb.append(str);
}
return sb.toString();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bufr != null) {
try {
bufr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
/**
* 文本的读取操作
*
* @param is 输入流
* @return
*/
public static String read(InputStream is) {
BufferedReader bufr = null;
try {
bufr = new BufferedReader(new InputStreamReader(is));
StringBuffer sb = new StringBuffer();
String str = null;
while ((str = bufr.readLine()) != null) {
sb.append(str);
}
return sb.toString();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bufr != null) {
try {
bufr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
/**
* @param context 上下文
* @param fileName 文件名
* @return 字节数组
*/
public static byte[] readBytes(Context context, String fileName) {
FileInputStream fin = null;
byte[] buffer = null;
try {
fin = context.openFileInput(fileName);
int length = fin.available();
buffer = new byte[length];
fin.read(buffer);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fin != null) {
fin.close();
fin = null;
}
} catch (IOException e) {
e.printStackTrace();
}
}
return buffer;
}
/**
* 快速读取程序应用包下的文件内容
*
* @param context 上下文
* @param filename 文件名称
* @return 文件内容
* @throws IOException
*/
public static String read(Context context, String filename)
throws IOException {
FileInputStream inStream = context.openFileInput(filename);
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = inStream.read(buffer)) != -1) {
outStream.write(buffer, 0, len);
}
byte[] data = outStream.toByteArray();
return new String(data);
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有