我的.txt文件包含字符串和整数(非常简化):
String Q1 = "aflasjke";
String Q2 = "agdsgsdg";
String Q3 = "asdgdsgn";
String Q1_1 = "sgadg";
String Q1_2 = "agajgsdgn";
String Q1_3 = "sdgjsdgj";
int M1 = 1;
int M2 = 2;
int M3 = 4;
我需要在MainActivity中使用这些字符串和int。我怎样才能“导入/读取”它们?
谢谢!
答案 0 :(得分:0)
这两种方法可以帮助你
修改
您可以在此处阅读SdCard中的文件
public static String getStringFromFile (String filePath) throws Exception {
File dir = Environment.getExternalStorageDirectory();
File fl = new File(dir, "path/to/the/file/inside/the/sdcard.ext");
FileInputStream fin = new FileInputStream(fl);
String ret = convertStreamToString(fin);
//Make sure you close all streams.
fin.close();
return ret;
}
这里有来自上面文件的字符串
public static String convertStreamToString(InputStream is) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line).append("\n");
}
reader.close();
return sb.toString();
}
并为您的清单添加正确的权限
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
我在这个答案here
中找到了