gpt4 book ai didi

java - 从 Android Studio 中的文件读取

转载 作者:行者123 更新时间:2023-12-01 11:18:23 25 4
gpt4 key购买 nike

我正在尝试将单词的文本文件读入数组或字符串中,我可以在其中访问每个单词。如果单词出现在一个长字符串中,则分隔单词是没有问题的,但我在读取文件时遇到了一些实际问题。我使用的是 Android Studio,该文件位于 asset 文件夹下(app/app/src/main/assets/wordsEasy.txt)到目前为止我有以下代码:

public String[] getWords(String difficulty){
ArrayList<String> wordList = new ArrayList<String>();

String[] words = new String[wordList.size()];

String wordList = getQuestions() //Location of error

//Haven't finished here but would consist of transferring the words in the String into an array

return words;
}

private static String getQuestions(Context ctx,String file_name) {

AssetManager assetManager = ctx.getAssets();
ByteArrayOutputStream outputStream = null;
InputStream inputStream = null;
try {
inputStream = assetManager.open(file_name);
outputStream = new ByteArrayOutputStream();
byte buf[] = new byte[1024];
int len;
try {
while ((len = inputStream.read(buf)) != -1) {
outputStream.write(buf, 0, len);
}
outputStream.close();
inputStream.close();
} catch (IOException e) {
}
} catch (IOException e) {
}
return outputStream.toString();

}

我不知道如何在这种情况下获取上下文,希望得到任何帮助。另外,如果您知道将文件读取到字符串或数组的替代方法,请分享。

最佳答案

检查这个,为我工作

import android.content.Context;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;


public class MainActivity extends AppCompatActivity {

List<String> wordList;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
wordList = getWordsFromFile("text_file.txt", MainActivity.this);
Toast.makeText(this,"Word Size:"+wordList.size(),Toast.LENGTH_SHORT).show();
TextView textView = (TextView) findViewById(R.id.tv_words);
for(String word : wordList){
textView.append("\n"+word);
}
}



public List<String> getWordsFromFile(String textFileName, Context context){
List<String> wordList = new ArrayList<>();
String textStr = "";
AssetManager am = context.getAssets();
try {
InputStream is = am.open(textFileName);
textStr = getStringFromInputStream(is);

} catch (IOException e) {
e.printStackTrace();
}
if(textStr !=null){
String[] words = textStr.split("\\s+");
for (int i = 0; i < words.length; i++) {
words[i] = words[i].replaceAll("[^\\w]", "");
wordList.add(words[i]);
}
}
return wordList;
}

private static String getStringFromInputStream(InputStream is) {

BufferedReader bufferedReader = null;
StringBuilder stringBuilder = new StringBuilder();

String line;
try {
bufferedReader = new BufferedReader(new InputStreamReader(is));
while ((line = bufferedReader.readLine()) != null) {
stringBuilder.append(line);
}

} catch (IOException e) {
e.printStackTrace();
} finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return stringBuilder.toString();
}
}

关于java - 从 Android Studio 中的文件读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31530355/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com