FBReader打开txt优化方案,加入本地格式好后得数据写入和读取
2012-11-06 16:23
781 查看
///------------- 下面代码用于获取缓存的内容,就不用去本地文件获取内容了 ----by nil-----------------
public boolean getTxtFromCache()
{
String md5Path=MD5Util.md5(tempModel.Book.File.getPath()+"/"+tempModel.Book.File.getLongName());
String fileName=Paths.myLocalCacheDirectory()+"/"+ md5Path+".txt";
File file=new File(fileName);
if(file.exists())
{
getTheString(file);
return true;
}else
{
return false;
}
}
public void getTheString(File file)
{
int size = (int)file.length();
if (size < 0) {
// L.l("==============file is null=======");
}
char[] block = new char[size / 2];
InputStreamReader reader = null;
try {
reader = new InputStreamReader(
new FileInputStream(file),
"UTF-16LE"
);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
if (reader.read(block) != block.length) {
}
else
{
for(int offset=0;offset<block.length;)
{
final int labelLength = (int)block[offset++];
String contentString=new String(block,offset,labelLength);
if (labelLength == 0||labelLength>block.length) {
}else
{
txtReader.beginParagraph();
txtReader.addData(contentString.toCharArray());
txtReader.endParagraph();
}
offset=offset+labelLength;
}
}
} catch (IOException e) {
e.printStackTrace();
}
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public void writeLocalTxtToCache(String[] content)
{
String md5Path=MD5Util.md5(tempModel.Book.File.getPath()+"/"+tempModel.Book.File.getLongName());
String fileName=Paths.myLocalCacheDirectory()+"/"+ md5Path+".txt";
File file=new File(Paths.myLocalCacheDirectory());
if(!file.isDirectory())
{
file.mkdirs();
}
OutputStreamWriter writer;
try {
writer = new OutputStreamWriter(
new FileOutputStream(
fileName, true)
,
"UTF-16LE"
);
for(int i=0;i<content.length-1;i++)
{
char[] contentCharArray=content[i].toCharArray();
final int length=contentCharArray.length; //输入的内容的长度
char lengthChar=(char)length;
char[] block=new char[length+1];
int offset=0;
block[offset]=lengthChar;
offset++;
System.arraycopy(contentCharArray,0, block,1, block.length-1);
writer.write(block);
}
writer.close();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//--------上面的代码用于从缓存内获取内容---------by nil------------------
1.本文章用于备份代码。
2.研究fb的同学,可以修改之来提高打开速度。
public boolean getTxtFromCache()
{
String md5Path=MD5Util.md5(tempModel.Book.File.getPath()+"/"+tempModel.Book.File.getLongName());
String fileName=Paths.myLocalCacheDirectory()+"/"+ md5Path+".txt";
File file=new File(fileName);
if(file.exists())
{
getTheString(file);
return true;
}else
{
return false;
}
}
public void getTheString(File file)
{
int size = (int)file.length();
if (size < 0) {
// L.l("==============file is null=======");
}
char[] block = new char[size / 2];
InputStreamReader reader = null;
try {
reader = new InputStreamReader(
new FileInputStream(file),
"UTF-16LE"
);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
if (reader.read(block) != block.length) {
}
else
{
for(int offset=0;offset<block.length;)
{
final int labelLength = (int)block[offset++];
String contentString=new String(block,offset,labelLength);
if (labelLength == 0||labelLength>block.length) {
}else
{
txtReader.beginParagraph();
txtReader.addData(contentString.toCharArray());
txtReader.endParagraph();
}
offset=offset+labelLength;
}
}
} catch (IOException e) {
e.printStackTrace();
}
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public void writeLocalTxtToCache(String[] content)
{
String md5Path=MD5Util.md5(tempModel.Book.File.getPath()+"/"+tempModel.Book.File.getLongName());
String fileName=Paths.myLocalCacheDirectory()+"/"+ md5Path+".txt";
File file=new File(Paths.myLocalCacheDirectory());
if(!file.isDirectory())
{
file.mkdirs();
}
OutputStreamWriter writer;
try {
writer = new OutputStreamWriter(
new FileOutputStream(
fileName, true)
,
"UTF-16LE"
);
for(int i=0;i<content.length-1;i++)
{
char[] contentCharArray=content[i].toCharArray();
final int length=contentCharArray.length; //输入的内容的长度
char lengthChar=(char)length;
char[] block=new char[length+1];
int offset=0;
block[offset]=lengthChar;
offset++;
System.arraycopy(contentCharArray,0, block,1, block.length-1);
writer.write(block);
}
writer.close();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//--------上面的代码用于从缓存内获取内容---------by nil------------------
1.本文章用于备份代码。
2.研究fb的同学,可以修改之来提高打开速度。
相关文章推荐
- 打开 txt 读取数据 写入实例
- matlab的各种数据读取(txt,dat,mat等格式),文件打开关闭
- 使用Java读取xlxs文件和写入txt文件,并将数据写入到本地文件
- matlab的各种数据读取(txt,dat,mat等格式),文件打开关闭
- Unity中把数据写入到本地Txt和读取本地Txt的数据
- php 读取TXT文本生成 想要的数据格式
- [C++]读写txt文件和从txt文件中读取数据写入数组中
- python读取文本数据写入到数据库及查询优化
- 数据在本地文件的写入和读取
- [转贴]EXCEL读取与写入数据的最佳方案
- matlab读取写入图像数据格式uint8,double
- C语言创建一个文件,写入数据并换行,再依次读取每行的数据,将读取的行数据以一定的格式命名为另一文件的文件名
- VB.NET 读取INI,TXT文件 写入数据
- MATLAB: 如何读取txt中的文件 如何将数据批量写入数据 文件名递增
- [转贴]EXCEL读取与写入数据的最佳方案
- 转:EXCEL读取与写入数据的最佳方案
- js读取本地磁盘文本文件并保存为JSON数据(有格式的文本)
- js读取本地txt文件中的json数据
- 从本地目录读取指定.txt文件,并且对数据进行解析。
- Python读取txt内容写入xls格式excel中的方法