【发布时间】:2016-12-12 09:30:32
【问题描述】:
我想阅读一个文本文件。为此,我给出了文件的路径,但它没有被读取。
出现错误,例如:ClassLoader 引用了未知路径:/data/app/com.kiranaapp-1/lib/arm
我已将文本文件保存在应用程序的帮助文件夹中。
public void ReadFile() {
try {
BufferedReader in = new BufferedReader(new FileReader("E:/siddhiwork/KiranaCustomerApp/app/src/main/java/com/kiranacustomerapp/helper/itemNames.txt"));
String str;
List<String> list = new ArrayList<String>();
while ((str = in.readLine()) != null) {
list.add(str);
}
String[] stringArr = list.toArray(new String[0]);
}
catch (FileNotFoundException e)
{
System.out.print(e);
}
catch (IOException e)
{
System.out.print(e);
}
}
当我调试以查看文件是否正在被读取并且字符串是否存储在数组中时, 但什么也没发生。
请帮忙,谢谢..
编辑:
我尝试在列表中获取字符串,但在 itemList 中没有得到任何值
public class StartupActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
List<String> itemList = new ArrayList<>();
itemList = readRawTextFile(StartupActivity.this);
}
public static List<String> readRawTextFile(Context context) {
String sText = null;
List<String> stringList;
try{
InputStream is = context.getResources().openRawResource(R.raw.item_names);
//Use one of the above as per your file existing folder
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
sText = new String(buffer, "UTF-8");
stringList = new ArrayList<String>(Arrays.asList(sText.split(" ")));
System.out.print(stringList);
} catch (IOException ex) {
ex.printStackTrace();
return null;
}
return stringList;
}
}
【问题讨论】:
-
用双反斜杠替换每个斜杠可能会有所帮助
-
也试过了。不工作。 @XtremeBaumer
-
我认为,该文件应该放在原始资源文件夹中。
-
我们有布局和drawabels的文件夹? @艾哈迈德