简体   繁体   English

无法从资产读取整个文件

[英]Cannot read whole file from assets

I'm trying to read a plist file from my assets folder, but the file is read only to line 153 - not to the end. 我正在尝试从资产文件夹中读取一个plist文件,但是该文件仅读到第153行-未读到结尾。 Can anyone help me and tell me why is this happening? 谁能帮助我,告诉我为什么会这样吗?

If i log do this Log.v("ReadFromFile("filename.plist",this); I'm getting content to 153-rd line. What am I missing, why I'm not getting the content of the whole file? Here is my code: 如果我登录,请执行此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;
    }

And the content of the file I'm trying to read is this: 我尝试读取的文件内容是这样的:

<?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>A­bdul 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>

In addition to the possible issues with using Log for testing, you are making an invalid assumption about the behavior of available() . 除了使用Log进行测试可能带来的问题外,您还对available()的行为做出了无效的假设。 Quoting the documentation : 引用文档

It is particularly important to realize that you must not use this method to size a container and assume that you can read the entirety of the stream without needing to resize the container. 特别重要的是要认识到您一定不能使用此方法来调整容器的大小,并假定您可以读取整个流而无需调整容器的大小。

The best solution is to move the file out of assets/ and into res/xml/ , then use getXml() on Resources to read it in. This will parse ~10x faster than parsing it other ways. 最好的解决方案是将文件移出assets/并移至res/xml/ ,然后在Resources上使用getXml()读取文件。与其他方法解析相比,解析速度快约10倍。

If you absolutely do need to have this in assets, or you need it as a full string for some reason, you need to loop, as is covered in Read/convert an InputStream to a String . 如果您确实确实需要将此资产包含在资产中,或者出于某些原因需要将其作为完整字符串,则需要进行循环,如将InputStream读取/转换为String所述

I put your file in assets by the name "text.xml" and i am able to read the full content in a string :- 我将您的文件放在名称为“ 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM