一、文件读取
1.1 使用BufferedReader类
(1)BufferedReader类介绍
https://blog.csdn.net/ai_bao_zi/article/details/81134801
(2)实现文件读取
//读取指定目录下所有TXT文件的文件内容
private String getFileContent(File file) {
String content = "";
if(!file.isDirectory()) {//判断该路径名的文件是否是一个目录(文件夹)
if(file.getName().endsWith("txt")) {//判断是否是文本文件
try {
InputStream inputStream = new FileInputStream(file);
if (inputStream != null) {
InputStreamReader inputReader = new InputStreamReader(inputStream, "UTF-8");
BufferedReader bufferedReader = new BufferedReader(inputReader);
String line = "";
//分行读取
while ((line = bufferedReader.readLine()) != null) {
content += line + "\n";
}
inputStream.close();//关闭输入流
}
}catch (java.io.FileNotFoundException e) {
Log.d("getFileContent: ", "The File doesn't exist");
} catch (IOException e) {
Log.d("getFileContent", e.getMessage());
}
}
}
return content;
}
二、文件写入
2.1 使用RandomAccessFile类
(1)RandomAccess类介绍
https://www.cnblogs.com/lijianli/p/9680265.html
(2)实现文件写入
//将字符串写入文本文件中(makeFilePath函数及配套使用的函数见文章后面的“附录”)
private void writeTxtToFile(String contentstr, String filePath, String fileName) {
//生成文件夹之后,再生成文件,不然会出错
makeFilePath(filePath, fileName);
String filePathStr = filePath + fileName;
//每次写入时,都换行写
String contentStr = contentstr + "\r\n";
try {
File file = new File(filePathStr);
if (!file.exists()) {
Log.d("writeTxtToFile", "create the file; " + filePathStr);
file.getParentFile().mkdirs();
file.createNewFile();
}
RandomAccessFile raf = new RandomAccessFile(file, "rwd");
raf.seek(file.length());
raf.write(contentStr.getBytes());
ToastUtil.showLong("写入成功!");
Log.d("writeTxtToFile","写入成功");
raf.close();
} catch (Exception e) {
Log.e("writeFIleToFile", "Error on write File: " + e);
}
}
三、文件修改
3.1 使用FileWriter类
(1)FileWriter类介绍
菜鸟教程:https://www.runoob.com/java/java-filewriter.html
(2)实现文件修改
//修改数据(以读取文件的编辑框中的内容覆盖原文件内容)
//makeFilePath函数及配套使用的函数见文章后面的“附录”
private void modifyFile() {
String contentStr = readFileEt.getText().toString();
//生成文件夹之后,再生成文件,不然会出错
makeFilePath(filePath, fileName);
String filePathStr = filePath + fileName;
try {
File file = new File(filePathStr);
if (!file.exists()) {
Log.d("modifyFile", "create the file: " + filePathStr);
file.getParentFile().mkdirs();
file.createNewFile();
}
FileWriter fw = new FileWriter(file);
fw.write(contentStr);
ToastUtil.showLong("修改成功!");
Log.d("modifyFile","修改成功");
fw.close();
} catch (Exception e) {
Log.e("modifyFile", "Error on Write File: " + e);
}
}
四、文件复制
4.1 使用FileInputStream和FileOutputStream类
//复制文件
public boolean copyFile(String filePathFrom, String filePathTo) {
File fileFrom = new File(filePathFrom);
File fileTo = new File(filePathTo);
if(!fileFrom.exists()) {
fileFrom.mkdir();
Log.d("FileMoveAsyncTask: ", "copyFile: " + "文件fileFrom不存在,重新创建!");
}
if(!fileTo.exists()) {
fileTo.mkdir();
Log.d("FileMOveAsyncTask: ", "copyFile: " + "文件fileTo不存在,重新创建!");
}
boolean isCopySuc = false;
File fileFrom2 = new File(filePathFrom + "data.txt");
File fileTo2 = new File(filePathTo + "data.txt");
try {
InputStream is = new FileInputStream(fileFrom2);
FileOutputStream fos = new FileOutputStream(fileTo2);
byte[] buffer = new byte[1024];
int byteCount = 0;
while ((byteCount = is.read(buffer)) != -1) {
fos.write(buffer, 0, byteCount);
}
fos.flush();
fos.close();
is.close();
isCopySuc = true;
Log.d("FileMoveAsyncTask", "copyFile: " + "文件复制成功");
}catch (java.io.FileNotFoundException e) {
Log.d("FileMoveAsyncTask: ", fileTo2.toString());
Log.d("FileMoveAsyncTask: ", "The FileTo doesn't exist");
} catch (IOException e) {
Log.d("FileMoveAsyncTask", "copyFile" + e.getMessage());
}
return isCopySuc;
}
五、附录
5.1 makeFilePath函数及配套函数
//生成文件
private File makeFilePath(String filePath, String fileName) {
File file = null;
makeRootDirectory(filePath);
try {
file = new File(filePath + fileName);
if (!file.exists()) {
file.createNewFile();
}
} catch (Exception e) {
e.printStackTrace();
}
return file;
}
//生成文件夹
private static void makeRootDirectory(String filePath) {
File file = null;
try {
file = new File(filePath);
if (!file.exists()) {
Log.d("makeRootDirectory", "mkdir:" + file.toString());
file.mkdir();
}
} catch (Exception e) {
Log.i("error:", e + "");
}
}
六、根据图片路径获取图片
6.1 核心代码
File file = new File(path);
ImageView img = (ImageView) findViewById(R.id.img);
if(file.exists()){
Bitmap bm = BitmapFactory.decodeFile(path);
img.setImageBitmap(bm);
}
6.2 文件读写权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
6.3 可能出现的问题
1. 在创建文件时,需要先创建目录文件夹,在创建文件,否则会报错(也就是说要把父文件夹和子文件的创建分开处理);
2. 使用File的createNewFile()函数时,出现Operation not permitted的报错,可能的原因是:手机没插内存卡(SD卡)(前提是你是想把文件存储在sd卡上,你创建的文件目录是在/sdcard下);
——————————————————————————————————————————
参考网址
参考:Android写入txt文件并读取 - 简书(读写文本文件)
参考:http://www.voidcn.com/article/p-vyjddivf-ba.html (文件修改,采用文件覆盖性写入)