我正在使用J2ME Wireless Toolkit 2.2编写一个J2ME应用程序我有以下代码:
public class BusReader
{
private String[] fileNames;
private final String allFilesInfoFile = "files_in_dir";
public BusReader ()
{
fileNames = getFileNames ();
String busNo = getBusNo ("BusNo1p.bin");
}
public String[] getAllBusFiles ()
{
return fileNames;
}
public String getBusNo (String fileName)
{
String[] fileLines = loadResourceFile (fileName);
int linesCount = fileLines.length;
for (int i=0;i<linesCount;++i)
if (fileLines[i].equals ("[BusNo]") && i < linesCount-1)
return fileLines[i+1];
return null;
}
public String getDefaultDirection (String fileName)
{
String[] fileLines = loadResourceFile (fileName);
int linesCount = fileLines.length;
for (int i=0;i<linesCount;++i)
if (fileLines[i].equals ("[BusDirection]") && i < linesCount-1)
return fileLines[i+1];
return null;
}
private String[] getFileNames ()
{
return loadResourceFile (allFilesInfoFile);
}
private String[] loadResourceFile (String fileName)
{
String content = "";
try
{
Reader in = new InputStreamReader(this.getClass().getResourceAsStream(fileName), "iso-8859-2");
StringBuffer temp = new StringBuffer(1024);
char[] buffer = new char[1024];
int read;
while ((read=in.read(buffer, 0, buffer.length)) != -1)
temp.append(buffer, 0, read);
content = temp.toString();
} catch (IOException e) {
return null;
}
int len = content.length ();
if (content.charAt (len-1) == 'n' && content.charAt (len-2) == 'r')
{
String newContent = "";
for (int i=0;i<len-2;++i)
newContent += content.charAt (i);
content = newContent;
}
String[] fileLines = TString.Split ("rn", new TString(content));
for (int i=0;i<fileLines.length; ++i)
{
fileLines[i] = fileLines[i].trim ();
if (fileLines[i].length () == 0)
fileLines[i] = "";
}
return fileLines;
}
}
一切工作正常,但问题是,当我试图复制我的应用程序到手机。在我的手机上,当我打开应用程序之前,应用程序显示我必须等待35秒。这是因为,构造函数执行了两次函数:
loadResourceFile (String fileName)
加载资源文件。函数正在加载的文件大小为:1.22KB和29KB。问题是:如何加速加载功能(loadResourceFile)?
我试图创建java类文件作为资源数据,但它超出了java内存限制。我将数组String[][][][]更改为String[][][],它在15秒内加载到我的手机上。我想当我将加载数据作为资源时,它会工作得更快。手机:Nokia 3110c
我找到了。它是以下几行(花了35秒):
if (content.charAt (len-1) == 'n' && content.charAt (len-2) == 'r')
{
String newContent = "";
for (int i=0;i<len-2;++i)
newContent += content.charAt (i);
content = newContent;
}