wafers
wafers

Reputation: 1241

How to get the filename as string if the file is under the assets directory?

The file.xml:

    <Items>
        <ItemData>
            <ItemNumber>ABC</ItemNumber>
            <Description>Desc1</Description>        
            <Price>9.95</Price>        
            <Weight>10.00</Weight>    
        </ItemData>    
        <ItemData>        
            <ItemNumber>XYZ</ItemNumber>        
            <Description>"Desc2"</Description>        
            <Price>19.95</Price>
            <Weight>22.22</Weight>
        </ItemData>
    </Items>

QUESTIONS:

Thank you!

Upvotes: 3

Views: 299

Answers (2)

Anu
Anu

Reputation: 1914

The following would work:

InputStream is = context.getAssets().open("xml/file.xml");

Upvotes: 2

siliconeagle
siliconeagle

Reputation: 7383

the path is just the path under the assets directory using forward slash (/)

so assets/x/y.z is referenced as this.getAssets().open("x/y.z");

That isnt the correct way to read the data - Inputstream.read isnt garunteed to read all the data it returns the number of bytes read - likely this will work for smaller file but you might get problems with bigger ones.

This is general code i read to to read text files, instead of a FileReader use an InputStreamReader

StringBuilder sw = new StringBuilder();
BufferedReader reader = new BufferedReader( new FileReader(file));
String readline = "";
while ((readline = reader.readLine()) != null) { 
    sw.append(readline);
}
String string = sw.toString();

Upvotes: 1

Related Questions