android将一个String保存到.txt文本中
2013-07-10 20:06
120 查看
TXT文本的编码方式有UNICODES,UTF8,UTF16等等。 这里只针对UTF8编码的TXT文本作为例子。保存该txt文本到SD卡的download目录。
private boolean saveText(String text, String name) {
if(text == null) return false;
byte[] data = text.getBytes("UTF8");
if(null == data || data.length == 0){
return false;
}
FileOutputStream fout = null;
try {
String downloadDirectoryString = MmsConfig.DIRECTORY_DOWNLOADS_UNKNOW_TYPE;
// Depending on the location, there may be an
// extension already on the name or not
String dir = Environment.getExternalStorageDirectory() + "/"
+ downloadDirectoryString + "/";
String extension = ".txt";
File file = dir + name + extension;
// make sure the path is valid and directories created for this file.
File parentFile = file.getParentFile();
if (!parentFile.exists() && !parentFile.mkdirs()) {
return false;
}
fout = new FileOutputStream(file);
int size = data.length;
//UTF8 TEXT HEAD
byte[] heads = new byte[3];
heads[0] = (byte)(-17);
heads[1] = (byte)(-69);
heads[2] = (byte)(-65);
fout.write(heads, 0, heads.length);
fout.write(data, 0, size);
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
Uri.fromFile(file)));
} catch (IOException e) {
// Ignore
Log.e(TAG, "IOException caught while opening or reading stream", e);
return false;
} finally {
if (null != fout) {
try {
fout.close();
} catch (IOException e) {
// Ignore
Log.e(TAG, "IOException caught while closing stream", e);
return false;
}
}
}
return true;
}
private boolean saveText(String text, String name) {
if(text == null) return false;
byte[] data = text.getBytes("UTF8");
if(null == data || data.length == 0){
return false;
}
FileOutputStream fout = null;
try {
String downloadDirectoryString = MmsConfig.DIRECTORY_DOWNLOADS_UNKNOW_TYPE;
// Depending on the location, there may be an
// extension already on the name or not
String dir = Environment.getExternalStorageDirectory() + "/"
+ downloadDirectoryString + "/";
String extension = ".txt";
File file = dir + name + extension;
// make sure the path is valid and directories created for this file.
File parentFile = file.getParentFile();
if (!parentFile.exists() && !parentFile.mkdirs()) {
return false;
}
fout = new FileOutputStream(file);
int size = data.length;
//UTF8 TEXT HEAD
byte[] heads = new byte[3];
heads[0] = (byte)(-17);
heads[1] = (byte)(-69);
heads[2] = (byte)(-65);
fout.write(heads, 0, heads.length);
fout.write(data, 0, size);
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
Uri.fromFile(file)));
} catch (IOException e) {
// Ignore
Log.e(TAG, "IOException caught while opening or reading stream", e);
return false;
} finally {
if (null != fout) {
try {
fout.close();
} catch (IOException e) {
// Ignore
Log.e(TAG, "IOException caught while closing stream", e);
return false;
}
}
}
return true;
}
相关文章推荐
- Android将String保存为SD卡中TXT文件的方法
- C#控制台基础 file.readalllines一行一行的读取一个txt文本,把txt中的内容放到一个string数组里
- Android在终端本地保存txt文本文件及zip文件
- 读取一个文件夹下所有的txt文件,并保存到一个txt文本中
- java 实现将一个string保存到txt文档中
- 一个简单的保存string 到 txt 中的方法,流的运用要注意
- Android在终端本地保存txt文本文件及zip文件
- 将List<Map<String,String>>中的数据输出保存到TXT文本文件中
- 建立一个文本文件in.txt,在其中输入若干个正整数,中间用空格分隔,然后保存起来;在窗体上添加一个命令按钮和一个标签框,当单击命令按钮时将in.txt文件中存放的所有的能被3整除的数的和显示在标签框
- Android将String保存为SD卡中TXT文件
- java将一个string保存到txt文档中
- Android中读取一个txt文本文件,然后显示到界面上的操作
- 将List<Map<String,String>>中的数据输出保存到TXT文本文件中
- 【Linux】将终端的命令输出保存为txt文本文件
- C语言 创建一个 txt 文件 bin输入字符 保存文件在工作文件夹里
- Qt实现数据文本保存TXT文件
- [AHK]用DOS命令Dir把一个文件夹里的文件名保存到txt中
- Android 分享一个SharedPreferences的工具类,方便保存数据
- 把图像数据写进一个txt文本中,然后整成二维数组初始化的形式,复制到CCS中
- [转载]如何将cmd中命令输出保存为TXT文本文件