您的位置:首页 > 其它

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的同学,可以修改之来提高打开速度。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: