使用存储在 ASSETS 文件夹中的文本文件
Use a text file stored in ASSETS folder
我试图了解资产文件夹中存储的数据文件与 res/raw 文件夹中存储的数据文件之间的区别。我的目标是将数据文件存储在 app 目录结构中,其中(在本例中)存储测试分数,可以访问它们,然后可以修改它们。据我了解,为此我需要使用 ASSET 而不是 RAW 文件。
当文本文件存储在 RES/RAW 文件夹中时,我设法将文本文件中的数据加载到数组中,但现在我无法使用 ASSET 文件.我认为这就像使用 AssetManager.open.
一样简单
最后我的一个问题是这个。如何读写 ASSET 文件夹中的文本文件?
这是我的代码:
public class MainActivity extends AppCompatActivity {
String[][] testScoreList = new String[3][3];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Load test scores into arraylist
nameArrayListMethod();
}
//This method loads test scores into an array
public void nameArrayListMethod (){
InputStreamReader InputSR = null;
BufferedReader BufferedRdr = null;
String thisLine = null;
AssetManager am = getAssets();
InputSR = new InputStreamReader(am.open("test_scores.txt"));
BufferedRdr = new BufferedReader(InputSR);
try {
// open input stream test_scores for reading purpose.
int i = 0;
while ((thisLine = BufferedRdr.readLine()) != null) {
// System.out.println(thisLine);
String[] parts = thisLine.split(" ");
testScoreList[i][0] = parts[0];
testScoreList[i][1] = parts[1];
i = i +1;
}
} catch (Exception e) {
e.printStackTrace();
}
我在 (am.open("test_scores.txt"))
行收到 Unhandled exception: java.io.IOException
错误。
为输入干杯
InputStream is = getResources().openRawResource(R.raw.yourTxtFile);
String s = IOUtils.toString(is); // Here is the whole string you want
IOUtils.closeQuietly(is); // don't forget to close your streams
尝试使用 try / catch 来防止您的应用程序崩溃。他们在那里是有原因的。
这是我在 Android 中打开文件的方式:
try {
InputStream inputstream = getResources().openRawResource(getResources().getIndentifier("file_name", "folder", getPackageName()));
BufferedReader bufferedRdr = new BufferedReader(inputstream);
} catch (IOException ioe) {
ioe.printStackTrace(); // error handling should be better then this...
} // and you should close the InputStream and BufferedReader with .close().
AssetManger#open(String) 会抛出异常,您需要处理它。
public final InputStream open(String fileName) throws IOException {
return open(fileName, ACCESS_STREAMING);
}
所以你需要:
try {
InputSR = new InputStreamReader(am.open("test_scores.txt"));
BufferedRdr = new BufferedReader(InputSR);
// open input stream test_scores for reading purpose.
int i = 0;
while ((thisLine = BufferedRdr.readLine()) != null) {
// System.out.println(thisLine);
String[] parts = thisLine.split(" ");
testScoreList[i][0] = parts[0];
testScoreList[i][1] = parts[1];
i = i +1;
}
} catch (Exception e) {
e.printStackTrace();
}
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(getAssets().open("myfile.txt")));
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
}
我试图了解资产文件夹中存储的数据文件与 res/raw 文件夹中存储的数据文件之间的区别。我的目标是将数据文件存储在 app 目录结构中,其中(在本例中)存储测试分数,可以访问它们,然后可以修改它们。据我了解,为此我需要使用 ASSET 而不是 RAW 文件。
当文本文件存储在 RES/RAW 文件夹中时,我设法将文本文件中的数据加载到数组中,但现在我无法使用 ASSET 文件.我认为这就像使用 AssetManager.open.
一样简单最后我的一个问题是这个。如何读写 ASSET 文件夹中的文本文件?
这是我的代码:
public class MainActivity extends AppCompatActivity {
String[][] testScoreList = new String[3][3];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Load test scores into arraylist
nameArrayListMethod();
}
//This method loads test scores into an array
public void nameArrayListMethod (){
InputStreamReader InputSR = null;
BufferedReader BufferedRdr = null;
String thisLine = null;
AssetManager am = getAssets();
InputSR = new InputStreamReader(am.open("test_scores.txt"));
BufferedRdr = new BufferedReader(InputSR);
try {
// open input stream test_scores for reading purpose.
int i = 0;
while ((thisLine = BufferedRdr.readLine()) != null) {
// System.out.println(thisLine);
String[] parts = thisLine.split(" ");
testScoreList[i][0] = parts[0];
testScoreList[i][1] = parts[1];
i = i +1;
}
} catch (Exception e) {
e.printStackTrace();
}
我在 (am.open("test_scores.txt"))
行收到 Unhandled exception: java.io.IOException
错误。
为输入干杯
InputStream is = getResources().openRawResource(R.raw.yourTxtFile);
String s = IOUtils.toString(is); // Here is the whole string you want
IOUtils.closeQuietly(is); // don't forget to close your streams
尝试使用 try / catch 来防止您的应用程序崩溃。他们在那里是有原因的。
这是我在 Android 中打开文件的方式:
try {
InputStream inputstream = getResources().openRawResource(getResources().getIndentifier("file_name", "folder", getPackageName()));
BufferedReader bufferedRdr = new BufferedReader(inputstream);
} catch (IOException ioe) {
ioe.printStackTrace(); // error handling should be better then this...
} // and you should close the InputStream and BufferedReader with .close().
AssetManger#open(String) 会抛出异常,您需要处理它。
public final InputStream open(String fileName) throws IOException {
return open(fileName, ACCESS_STREAMING);
}
所以你需要:
try {
InputSR = new InputStreamReader(am.open("test_scores.txt"));
BufferedRdr = new BufferedReader(InputSR);
// open input stream test_scores for reading purpose.
int i = 0;
while ((thisLine = BufferedRdr.readLine()) != null) {
// System.out.println(thisLine);
String[] parts = thisLine.split(" ");
testScoreList[i][0] = parts[0];
testScoreList[i][1] = parts[1];
i = i +1;
}
} catch (Exception e) {
e.printStackTrace();
}
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(getAssets().open("myfile.txt")));
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
}