我移植已获得Symbian / iPhone的应用程序到Android,其中一部分是一些数据保存到文件中。我用的的FileOutputStream 以保存文件到私有文件夹 /数据/数据/程序包/文件
I'm porting the application from Symbian/iPhone to Android, part of which is saving some data into file. I used the FileOutputStream to save the file into private folder /data/data/package_name/files:
FileOutputStream fos = iContext.openFileOutput( IDS_LIST_FILE_NAME, Context.MODE_PRIVATE );
fos.write( data.getBytes() );
fos.close();
现在我正在寻找一种方式如何加载它们。我现在用的是的FileInputStream ,但它可以让我读出字节的文件字节,这是pretty的效率低下:
Now I am looking for a way how to load them. I am using the FileInputStream, but it allows me to read the file byte by byte, which is pretty inefficient:
int ch;
StringBuffer fileContent = new StringBuffer("");
FileInputStream fis = iContext.openFileInput( IDS_LIST_FILE_NAME );
while( (ch = fis.read()) != -1)
fileContent.append((char)ch);
String data = new String(fileContent);
所以我的问题是如何用更好的方式来读取文件?
So my question is how to read the file using better way?
使用FileInputStream.read(byte[])你可以更有效地阅读。
Using FileInputStream.read(byte[]) you can read much more efficiently.
在一般你不想被读取任意大小的文件到内存中。
In general you don't want to be reading arbitrary-sized files into memory.
大多数解析器将采取的InputStream
。也许你可以让我们知道你正在使用的文件,我们可以提出一个更好的选择。
Most parsers will take an InputStream
. Perhaps you could let us know how you're using the file and we could suggest a better fit.
下面是如何使用字节的缓冲区版本的阅读()
:
Here is how you use the byte buffer version of read()
:
byte[] buffer = new byte[1024];
int length;
while ((length = fis.read(buffer)) != -1) {
fileContent.append(new String(buffer));
}
下一篇:(安卓)获取周的第一天