简体   繁体   中英

Android: Reading a file using InputStream character by character

Solved: The code I originally posted is fine, I just tried reading a txt file from a directory in assets without adding the directory's name in the "path" string under the false assumption "getAssets()" gets all assets from all subdirectories in assets.

I am making a game that uses cards with a name and description, the name and description are read from a .txt file in the assets folder. The method is made so it reads everything between '$' and '#' as the name, and '@' and '#' as the description. I wrote the original method in Eclipse (console application) and it worked like a charm. Then I transferred it to Android Studio, added context (since the method is in a class file) and AssetManager, etc and followed instructions from other similar stackoverflow questions but for some reason it just won't work.

Here is the code:

public void readFile(String p) throws IOException {

    // Path of the directory
    String path = new String(p);
    // AssetManager opens assets
    AssetManager am = con.getAssets();
    // Open file
    InputStream i_s = am.open(path);

    // StringBuilders store the text read for specific members
    StringBuilder _name = new StringBuilder("");  // Records name of card from file
    StringBuilder _desc = new StringBuilder("");  // Records description of card from file

    // "c" stores each character at a time
    char c;

    // Main loop that reads entire file
    while(i_s.available() > 0) { 
        c = (char) i_s.read();

        if(c=='$') {
            c = (char) i_s.read();  // Sends to next character so it doesn't save '$'
            while(c!='#')            // Loop saves everything between '$' and '#'
            {_name.append(c); c = (char) i_s.read();}
        }
        else if(c=='@') {
            c = (char) i_s.read();  // Sends to next character so it doesn't save '@'
            while(c!='#')            // Loop saves everything between '@' and '#'
            {_desc.append(c); c = (char) i_s.read();}
        }
    }
    i_s.close();

    // Sets read info into Class properties
    this.setName(_name);
    this.setDesc(_desc);

}

The method is in CardAction card,a subclass of Card. The set methods are in the Card class.

public void setName(StringBuilder sb) {
    this.name = sb.toString();
}
public void setDesc(StringBuilder sb) {
    this.desc = sb.toString();
}

Every time I test this (by calling the get methods for name and desc) it return empty strings. What am I doing wrong here?

Additional info: All the Card subclasses inherit the context "con" from Card superclass which passes the context from the main activity as a parameter in the Card constructor, so I am 100% sure Context and AssetManager are working fine.

Edit: Code that calls the readFile method from main activity

String str;
Turn t = new Turn(3, 40, t_o_d, names, true, 1, con);

// chooseFile returns String of randomly chosen file
// name from the assets folder (randomly "draw" card), works
str = t.chooseFile(1); 

CardAction ca = new CardAction(con); //Passes context as parameter for constructor
 try {ca.readFile(str);}
 catch(IOException e1) {}

 // cardName and cardDesc are TextViews I set just for testing the readFile method
 cardName.setVisibility(View.VISIBLE);
 cardDesc.setVisibility(View.VISIBLE);
 cardName.setText(ca.getName());
 cardDesc.setText(ca.getDesc());

Solved. the method is fine, what my problem was that I tried to open txt files located in their own folders in assets, so I just added another String parameter to the method (for the directory) and concatenated them (adding "/" between them).

Well, considering this was a solution for a completely other problem, I'm still glad this works so anyone struggling with reading files character by character can refer to the code I originally posted :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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