Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了从Android Studio Java中的文本文件中读取大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个类QuoteBank需要读取扫描仪的txt文件,但它给我一个文件未找到异常

java文件是在
应用程序/ SRC /主/爪哇/ nate.marxBros / QuoteBank.java

txt文件位于
应用程序/ src目录/主/资产/ Quotes.txt

代码

File file = new File("assets/QuotesMonkeyBusiness.txt");
    ScAnner input = null;
    try {
        input = new ScAnner(filE);
    } catch (FileNotFoundException E) {
        e.printStackTrace();
    }@H_673_11@ 
 

不应该像其他任何java程序一样工作吗?但它给文件找不到异常

在这个网站上尝试过很多东西
Android Studio Reading from Raw Resource Text File
但该方法不起作用,因为我不知道如何传递上下文

谢谢你的帮助

更新的代码

public class QuoteBank {
private ArrayList<ArrayList<QuoteBank>> bank;
private Context mContext;
private ArrayList<QuoteQuestion> monkeyBuisness;


public QuoteBank(Context context){
    mContext = context;
    InputStream is = null;
    try {
        is = mContext.getAssets().open("QuotesMonkeyBusiness.txt");
    } catch (IOException E) {
        e.printStackTrace();
    }

    ArrayList<QuoteQuestion> monkeyBuisness = parseFileToBank(is);
}@H_673_11@ 
 

主要活动

public class MainActivity extends ActionBarActivity {
QuoteBank b = new QuoteBank(MainActivity.this);@H_673_11@

解决方法

你应该有@L_174_1@mainActivity.java或一些实例化QuoteBank的Activity.您希望构造函数接受上下文的参数:

在QuoteBank.java中设置一个私有变量:

private Context mContext;@H_673_11@ 
 

设置构造函数

public QuoteBank(Context context) {
   this.mContext = context;
}@H_673_11@ 
 

然后在你的活动中实例化它,

QuoteBank quoteBank = new QuoteBank(context);@H_673_11@ 
 

可以通过this命令或Activity.this在活动中调用上下文变量,其中将“Activity”替换为您的活动名称.或者,如果您在片段内,则可以从onCreateView(…)方法中的View对象获取上下文.通常通过调用view.getContext().

现在,在您抓取资产的方法中,您可以使用上下文:

InputStream is = mContext.getAssets().open("QuotesMonkeyBusiness.txt")@H_673_11@ 
 

既然您正在使用android studio,您可以创建@L_174_1@main(String [] args){…}方法并运行它或者只是启动模拟器并让它使用Log.d(…)来显示来自文件.

或者,您也可以使用以下方法

AssetManager am = mContext.getAssets();
InputStream is = am.open("QuotesMonkeyBusiness.txt");@H_673_11@ 
 

将QuoteBank作为单例实例也可能有意义,这可能会提高效率,尽管这完全取决于您的要求,可能是这样的

List<String> allTextLines = QuoteBank.readFromFile(context,path_to_filE);@H_673_11@ 
 

然后在QuoteBank.java类中,您可以使用如下方法

/**
* Created by AndyRoid on 5/23/15.
*/
public class QuoteBank {

private Context mContext;

public QuoteBank(Context context) {
    this.mContext = context;
}

public List<String> readLine(String path) {
    List<String> mLines = new ArrayList<>();

    AssetManager am = mContext.getAssets();

    try {
        InputStream is = am.open(path);
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        String line;

        while ((line = reader.readLine()) != null)
            mLines.add(linE);
    } catch (IOException E) {
        e.printStackTrace();
    }

    return mLines;
}@H_673_11@ 
 

}

然后在我的MainActivity.java类中,我有以下内容

/**
 * Created by AndyRoid on 5/23/15.
 */
public class MainActivity extends AppCompatActivity {

public static final String TAG = MainActivity.class.getSimplename();

public static final String mPath = "adventur.txt";
private QuoteBank mQuoteBank;
private List<String> mLines;

@Override
protected void onCreate(Bundle savedInstanceStatE) {
    super.onCreate(savedInstanceStatE);
    setContentView(R.layout.activity_main);

    mQuoteBank = new QuoteBank(this);
    mLines = mQuoteBank.readLine(mPath);
    for (String String : mLines)
        Log.d(tag,String);
}

@Override
public Boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main,menu);
    return true;
}

@Override
public Boolean onOptionsItemSELEcted(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button,so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_setTings) {
        return true;
    }

    return super.onOptionsItemSELEcted(item);
}
}@H_673_11@ 
 

这是我的项目结构:

这是我从随机数据库下载的adventur.txt文件

这是我的日志输出

更新:为什么你不应该在Android中使用扫描仪

从官方文档:

http://developer.android.com/reference/java/util/Scanner.html

最后注意:

我强烈建议您阅读此处使用的所有对象的文档,以便您了解该过程.

大佬总结

以上是大佬教程为你收集整理的从Android Studio Java中的文本文件中读取全部内容,希望文章能够帮你解决从Android Studio Java中的文本文件中读取所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。