android怎么读取txt文件长度,Android中TXT文本文件写入与读取(总结)

/**

* txt文件内容管理器

*/

public class TXTManager {

public static String rootXMLPath = Environment.getExternalStorageDirectory().getPath() + "/testTXT";

/**

* 保存内容到TXT文件中

*

* @param fileName

* @param content

* @return

*/

public static boolean writeToXML(String fileName, String content) {

FileOutputStream fileOutputStream;

BufferedWriter bufferedWriter;

createDirectory(rootXMLPath);

File file = new File(rootXMLPath + "/" + fileName + ".txt");

try {

file.createNewFile();

fileOutputStream = new FileOutputStream(file);

bufferedWriter = new BufferedWriter(new OutputStreamWriter(fileOutputStream));

bufferedWriter.write(content);

bufferedWriter.close();

} catch (IOException e) {

e.printStackTrace();

return false;

}

return true;

}

/**

* 读取XML内容

*

* @param filePath

* @return

*/

public static String readFromXML(String filePath) {

FileInputStream fileInputStream;

BufferedReader bufferedReader;

StringBuilder stringBuilder = new StringBuilder();

File file = new File(filePath);

if (file.exists()) {

try {

fileInputStream = new FileInputStream(file);

bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream));

String line;

while ((line = bufferedReader.readLine()) != null) {

stringBuilder.append(line);

}

bufferedReader.close();

} catch (FileNotFoundException e) {

e.printStackTrace();

return null;

} catch (IOException e) {

e.printStackTrace();

return null;

}

}

return stringBuilder.toString();

}

/**

* 创建文件夹

*

* @param fileDirectory

*/

public static void createDirectory(String fileDirectory) {

File file = new File(fileDirectory);

if (!file.exists()) {

file.mkdirs();

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值