我正在尝试从我的资源文件夹中读取一个plist文件,但该文件只读取到第153行 - 而不是结尾。任何人都可以帮助我,告诉我为什么会这样吗?
如果我记录这样做Log.v("ReadFromFile("filename.plist",this);
我的内容是第153行。我错过了什么,为什么我没有得到整个文件的内容?
这是我的代码:
public String ReadFromfile(String fileName, Context context) {
InputStream input;
String text = null;
try {
input = context.getAssets().open(fileName);
int size = input.available();
byte[] buffer = new byte[size];
input.read(buffer);
input.close();
// byte buffer into a string
text = new String(buffer);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return text;
}
我正在尝试阅读的文件内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
<dict>
<key>Link</key>
<string>http://yaislam.com/quran/mp3/</string>
<key>Number</key>
<integer>0</integer>
<key>RecitorLabel</key>
<string>abdul-rahman-al-sudais</string>
<key>Title</key>
<string>Abdul Rahman Al Soudais عبد الرحمن السديس</string>
</dict>
<dict>
<key>Link</key>
<string>http://yaislam.com/quran/mp3/</string>
<key>Number</key>
<integer>1</integer>
<key>Title</key>
<string>Surat Al-Fātiĥah (The Opener) - سورة الفاتحة</string>
</dict>
</dict>
</array>
<dict>
<key>Link</key>
<string>http://yaislam.com/quran/mp3/</string>
<key>Number</key>
<integer>2</integer>
<key>Title</key>
<string>Surat Al-Baqarah (The Cow) - سورة البقرة</string>
</dict>
<dict>
<key>Link</key>
<string>http://yaislam.com/quran/mp3/</string>
<key>Number</key>
<integer>3</integer>
<key>Title</key>
<string>Surat 'Āli `Imrān (Family of Imran) - سورة آل عمران</string>
</dict>
<dict>
<key>Link</key>
<string>http://yaislam.com/quran/mp3/</string>
<key>New item</key>
<string></string>
<key>Number</key>
<integer>4</integer>
<key>Title</key>
<string>Surat An-Nisā' (The Women) - سورة النساء</string>
</dict>
<dict>
<key>Link</key>
<string>http://yaislam.com/quran/mp3/</string>
<key>New item</key>
<string></string>
<key>Number</key>
<integer>5</integer>
<key>Title</key>
<string>Surat Al-Mā'idah (The Table Spread) - سورة المائدة </string>
</dict>
<dict>
<key>Link</key>
<string>http://yaislam.com/quran/mp3/</string>
<key>New item</key>
<string></string>
<key>Number</key>
<integer>6</integer>
<key>Title</key>
<string>Surat Al-'An`ām (The Cattle) - سورة الأنعام </string>
</dict>
<dict>
<key>Link</key>
<string>http://yaislam.com/quran/mp3/</string>
<key>New item</key>
<string></string>
<key>Number</key>
<integer>7</integer>
<key>Title</key>
<string>Surat Al-'A`rāf (The Heights) - سورة الأعراف </string>
</dict>
<dict>
<key>Link</key>
<string>http://yaislam.com/quran/mp3/</string>
<key>New item</key>
<string></string>
<key>Number</key>
<integer>8</integer>
<key>Title</key>
<string>Surat Al-'Anfāl (The Spoils of War) - سورة الأنفال</string>
</dict>
<dict>
<key>Link</key>
<string>http://yaislam.com/quran/mp3/</string>
<key>New item</key>
<string></string>
<key>Number</key>
<integer>9</integer>
<key>Title</key>
<string>Surat At-Tawbah (The Repentance) - سورة التوبة </string>
</dict>
<dict>
<key>Link</key>
<string>http://yaislam.com/quran/mp3/</string>
<key>New item</key>
<string></string>
<key>Number</key>
<integer>10</integer>
<key>Title</key>
<string>Surat Yūnus (Jonah) - سورة يونس </string>
</dict>
<dict>
<key>Link</key>
<string>http://yaislam.com/quran/mp3/</string>
<key>New item</key>
<string></string>
<key>Number</key>
<integer>11</integer>
<key>Title</key>
<string>Surat Hūd (Hud) - سورة هود</string>
</dict>
<dict>
<key>Link</key>
<string>http://yaislam.com/quran/mp3/</string>
<key>New item</key>
<string></string>
<key>Number</key>
<integer>12</integer>
<key>Title</key>
<string>Surat Yūsuf (Joseph) - سورة يوسف</string>
</dict>
<dict>
<key>Link</key>
<string>http://yaislam.com/quran/mp3/</string>
<key>New item</key>
<string></string>
<key>Number</key>
<integer>13</integer>
<key>Title</key>
<string>Surat Ar-Ra`d (The Thunder) - سورة الرعد </string>
</dict>
<dict>
<key>Link</key>
<string>http://yaislam.com/quran/mp3/</string>
<key>New item</key>
<string></string>
<key>Number</key>
<integer>14</integer>
<key>Title</key>
<string>Surat 'Ibrāhīm (Abrahim) - سورة ابراهيم</string>
</dict>
<dict>
<key>Link</key>
<string>http://yaislam.com/quran/mp3/</string>
<key>New item</key>
<string></string>
<key>Number</key>
<integer>15</integer>
<key>Title</key>
<string>Surat Al-Hijr (The Rocky Tract) - سورة الحجر</string>
</dict>
<dict>
<key>Link</key>
<string>http://yaislam.com/quran/mp3/</string>
<key>New item</key>
<string></string>
<key>Number</key>
<integer>16</integer>
<key>Title</key>
<string>Surat An-Naĥl (The Bee) - سورة النحل </string>
</dict>
<dict>
<key>Link</key>
<string>http://yaislam.com/quran/mp3/</string>
<key>New item</key>
<string></string>
<key>Number</key>
<integer>17</integer>
<key>Title</key>
<string>Surat Al-'Isrā' (The Night Journey) - سورة الإسراء </string>
</dict>
<dict>
<key>Link</key>
<string>http://yaislam.com/quran/mp3/</string>
<key>New item</key>
<string></string>
<key>Number</key>
<integer>18</integer>
<key>Title</key>
<string>Surat Al-Kahf (The Cave) - سورة الكهف </string>
</dict>
<dict>
<key>Link</key>
<string>http://yaislam.com/quran/mp3/</string>
<key>New item</key>
<string></string>
<key>Number</key>
<integer>19</integer>
<key>Title</key>
<string>Surat Maryam (Mary) - سورة مريم </string>
</dict>
<dict>
<key>Link</key>
<string>http://yaislam.com/quran/mp3/</string>
<key>New item</key>
<string></string>
<key>Number</key>
<integer>20</integer>
<key>Title</key>
<string>Surat Ţāhā (Ta-Ha) - سورة طه </string>
</dict>
</array>
</plist>
答案 0 :(得分:0)
除了使用Log
进行测试时可能出现的问题之外,您对available()
的行为做出了无效的假设。引用the documentation:
特别重要的是要意识到您必须不使用此方法来调整容器大小,并假设您可以读取整个流而无需调整容器大小。
最佳解决方案是将文件移出assets/
并移至res/xml/
,然后使用getXml()
上的Resources
将其读入。这将解析速度提高约10倍而不是以其他方式解析它。
如果您确实需要在资产中使用它,或者出于某种原因需要将其作为完整字符串,则需要循环,如Read/convert an InputStream to a String中所述。
答案 1 :(得分:0)
我将您的文件放在名为“text.xml”的资源中,我可以读取字符串中的完整内容: -
String jsonString = null ;
try{
InputStream is = getAssets().open("test.xml");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
jsonString = new String(buffer, "UTF-8");
}
catch(Exception e){
e.printStackTrace();
}
System.out.println(jsonString);