IOUtils工具类-文件读写
一、作用
文件存取读写
二、使用
//覆盖写入 boolean isSuccess = IOUtil.writeTxtFile(file, array + "\n");
三、注意
1.文件的读写要开线程
2.需要相应的权限
3.Android6.0以上需要动态权限获取,参考动态权限获取的工具类和帮助类
四、代码
import android.content.Context;
import android.os.Environment;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.util.LinkedList;
import java.util.List;
/**
* author : GodH
* date : 2019/8/20 10:13
* desc : 输出输入流工具类
* version: 1.0
*/
public class IOUtil extends IOException {
/**
* 创建文件,但是路径不对时会报错
*
* @param fileName
* @return
*/
public static boolean createFile(File fileName) throws Exception {
try {
if (!fileName.exists()) {
fileName.createNewFile();
}
} catch (Exception e) {
e.printStackTrace();
}
return true;
}
/**
* 创建文件,但是路径不对时会自动创建
*
* @param file
* @return
*/
public static boolean createDirFile(File file) throws Exception {
File fileParent = file.getParentFile();
try {
if (!fileParent.exists()) {
fileParent.mkdirs();
}
if (!file.exists()) {
file.createNewFile();
}
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
/**
* 读取TXT内容
*
* @param file
* @return
*/
public static String readTxtFile(File file) {
String result = "";
try {
InputStreamReader reader = new InputStreamReader(new FileInputStream(file), "utf-8");
BufferedReader br = new BufferedReader(reader);
String s = null;
while ((s = br.readLine()) != null) {
result = result + s;
System.out.println(s);
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* 写入TXT,覆盖原内容
*
* @param content
* @param fileName
* @return
* @throws IOException
*/
public static boolean writeTxtFile(File fileName, String content) throws IOException {
RandomAccessFile mm = null;
boolean flag = false;
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream(fileName);
fileOutputStream.write(content.getBytes("utf-8"));
fileOutputStream.close();
flag = true;
} catch (IOException e) {
e.printStackTrace();
}
return flag;
}
/**
* 写入TXT,追加写入
*
* @param filePath
* @param content
*/
public static void fileChaseFW(String filePath, String content) {
try {
//构造函数中的第二个参数true表示以追加形式写文件
FileWriter fw = new FileWriter(filePath, true);
fw.write(content);
fw.close();
} catch (IOException e) {
System.out.println("文件写入失败!" + e);
}
}
/**
* @param filePath 文件路径
* @param context 查找的字符串
* @return 包含搜索的字符串的行
* @throws IOException
* @return List<String>
* @Title: findLines
* @Description: TODO
*/
public static List<String> findLines(String filePath, String context) throws IOException {
List<String> list = new LinkedList<String>();
BufferedReader read = new BufferedReader(new FileReader(filePath));
String str = null;
while ((str = read.readLine()) != null) {
if (str.indexOf(context) != -1) {
list.add(str);
}
}
read.close();
return list;
}
/**
* 将内容写入内存卡卡中
*
* @param context
* @param filename 要写入的文件名
* @param content 待写入的内容
* @throws IOException
*//*
public void outputJSON(Context context, String filename, String content) throws IOException {
File file = context.getFilesDir();
filename = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + filename;
//打开文件输出流
// FileOutputStream outputStream = new FileOutputStream(filename);
//
// //写数据到文件中
// outputStream.write(content.getBytes());
// outputStream.flush();
// outputStream.close();
FileWriter fileWriter = new FileWriter(filename);
fileWriter.write(content);
fileWriter.flush();
fileWriter.close();
Toast.makeText(context, "生成jjs.json文件成功,位置:" + filename, Toast.LENGTH_SHORT).show();
}*/
}



评论
1条评论Tiny Lv.3 回复
沙发
广东省深圳市 电信