android 读写文件数据
16lz
2021-12-04
android 读写文件数据 Demo:
public class HolderFile {
final String sFileName="android_info.csv";
//创建并写入
public void createFile(String info) {
File root = Environment.getExternalStorageDirectory();
File file = new File(root, sFileName);
try {
FileWriter Writer = new FileWriter(file);
// String str = "FileDemo";
Writer.write(info);
Writer.flush();
Writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//读取资料
public String read() {
String content = null;
File root = Environment.getExternalStorageDirectory();
File file = new File(root, sFileName);
try {
if (file.exists()) {
FileInputStream fis = new FileInputStream(file);
byte[] buffer = new byte[1024];
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int len = 0;
while ((len = fis.read(buffer)) != -1) {
baos.write(buffer, 0, len);
}
byte[] data = baos.toByteArray();
baos.close();
fis.close();
content = new String(data);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return content;
}
public boolean isFileExist() {
File file = new File(Environment.getExternalStorageDirectory() + "/"
+ sFileName);
return file.exists();
}
public boolean delFile() {
File file = new File(Environment.getExternalStorageDirectory() + "/"
+ sFileName);
return file.delete();
}
}
public class HolderFile {
final String sFileName="android_info.csv";
//创建并写入
public void createFile(String info) {
File root = Environment.getExternalStorageDirectory();
File file = new File(root, sFileName);
try {
FileWriter Writer = new FileWriter(file);
// String str = "FileDemo";
Writer.write(info);
Writer.flush();
Writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//读取资料
public String read() {
String content = null;
File root = Environment.getExternalStorageDirectory();
File file = new File(root, sFileName);
try {
if (file.exists()) {
FileInputStream fis = new FileInputStream(file);
byte[] buffer = new byte[1024];
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int len = 0;
while ((len = fis.read(buffer)) != -1) {
baos.write(buffer, 0, len);
}
byte[] data = baos.toByteArray();
baos.close();
fis.close();
content = new String(data);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return content;
}
public boolean isFileExist() {
File file = new File(Environment.getExternalStorageDirectory() + "/"
+ sFileName);
return file.exists();
}
public boolean delFile() {
File file = new File(Environment.getExternalStorageDirectory() + "/"
+ sFileName);
return file.delete();
}
}
更多相关文章
- SD卡读写文件 代码学习
- Android(安卓)文件读写 + sdcard + 文件的属性
- SD卡读写文件 代码学习
- android 的权限
- Android10共享文件总是读取不到文件,文件资源不存在!
- (转)Android(安卓)Contacts(一)—— 读取联系人
- Android(安卓)下载文件至SD卡,并用progressBar显示下载进度
- Android(安卓)下载文件及写入SD卡
- android朗读英文