【问题标题】:Android: Reading a file using InputStream character by characterAndroid:使用 InputStream 逐字符读取文件
【发布时间】:2015-04-17 17:28:00
【问题描述】:

已解决:我最初发布的代码很好,我只是尝试从 assets 中的目录读取 txt 文件,而不在“getAssets()”错误假设下在“path”字符串中添加目录名称从所有资源assets 中的子目录。

我正在制作一个使用带有名称和描述的卡片的游戏,名称和描述是从 assets 文件夹中的 .txt 文件中读取的。该方法是这样编写的,它读取 '$' 和 '#' 之间的所有内容作为名称,并读取 '@' 和 '#' 作为描述。我在 Eclipse(控制台应用程序)中编写了原始方法,它就像一个魅力。然后我将它转移到 Android Studio,添加上下文(因为该方法在类文件中)和 AssetManager 等,并按照其他类似 stackoverflow 问题的说明进行操作,但由于某种原因它无法正常工作。

代码如下:

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);

}

该方法在 CardAction card 中,它是 Card 的子类。设置方法在 Card 类中。

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

每次我测试它(通过调用 name 和 desc 的 get 方法)它都会返回空字符串。我在这里做错了什么?

附加信息:所有 Card 子类都从 Card 超类继承上下文“con”,该超类将来自主要活动的上下文作为参数传递给 Card 构造函数,因此我 100% 确定 Context 和 AssetManager 工作正常。

编辑:从主活动调用 readFile 方法的代码

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());

【问题讨论】:

  • 您能否将这些字段设置为其他未设置的实例混合在一起?这将有助于显示将所有内容联系在一起的代码,或者添加数据和拥有实例(它,“this”)的日志记录,您可以在其中设置和获取值,看看这是否有助于您自己弄清楚。
  • 添加了代码,我在发布原始帖子之前考虑了您所说的内容,这就是为什么我在所有内容中添加“this”以确保程序中不会有任何混淆。我承认,我仍然是这方面的业余爱好者,所以我不知道 100% 在哪些情况下我应该和不应该把“这个”放在哪里,所以我把它放在任何地方都是出于偏执。
  • 你真的应该让你的 catch 块做一个 e1.printStackTrace() - 现在它只会默默地吸收错误,这往往会导致 无法解释的故障......添加后,查看日志,您可能会遇到一些与读取尝试相关的异常。至于过度使用“this”——这通常不会帮助事情或防止混淆,因为“this”是什么完全取决于它出现在哪里。
  • 我知道 catch 的事情,这个特殊的 try-catch 块只是为了测试方法而不是最终程序的一部分。不过,我将阅读有关如何正确使用“this”的内容,感谢您引起我的注意。除了“this”这个东西,关于方法的其他所有内容(InputStream、while 循环等)都写成应该怎么写?
  • 不,你的 catch 块被误导了——在开发过程中你真的必须有嘈杂的块。如果你愿意,你可以让它们静音以释放,但是现在在你的 I/O 代码上有一个空的 catch 块是软件等价于把你的手指伸进你的耳朵,然后继续说你怎么不知道哪里出了问题你的程序。

标签: java android inputstream android-context


【解决方案1】:

解决了。该方法很好,我的问题是我试图打开位于资产自己文件夹中的txt文件,所以我只是在方法中添加了另一个字符串参数(用于目录)并将它们连接起来(在它们之间添加“/”) .

好吧,考虑到这是一个完全其他问题的解决方案,我仍然很高兴它可以正常工作,因此任何努力阅读文件的人都可以参考我最初发布的代码:)

【讨论】:

    猜你喜欢
    • 2018-06-18
    • 1970-01-01
    • 2019-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多