我是Android Studio的新手,但有两个问题。
因此,当前我正在使用一种方法来从.txt文件中提取行并将其转换为ArrayList。我正在使用FileReader和LineNumberReader。当我运行该程序时,即使此代码在Eclipse上也可以运行捕获。
我的问题:1.我目前正在将.txt文件存储在资产的原始文件夹中。这是放正确的地方吗?2.我使用的位置是“ \ res \ raw \ city.txt”,我称文件错误吗?
这是我的代码(在Android Studio中>],用于更多上下文:
//Method to take a file and convert into an ArrayList private ArrayList<String> txtArray(String fileLoc) throws IOException { int n = 22; File file = new File(fileLoc); //Counts how many lines are in the system try { FileReader fr = new FileReader(fileLoc); LineNumberReader lr = new LineNumberReader(fr); lr.skip(Long.MAX_VALUE); n = lr.getLineNumber(); Toast errorToast3 = Toast.makeText(getActivity(), "the Number of lines is " + n, Toast.LENGTH_SHORT); errorToast3.show(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); Toast errorToast5 = Toast.makeText(getActivity(), "Fuk " + n, Toast.LENGTH_SHORT); errorToast5.show(); } Toast errorToast3 = Toast.makeText(getActivity(), "Still ok here " + n, Toast.LENGTH_SHORT); errorToast3.show(); //Takes lines from the text file and converts into Array List Scanner kb = new Scanner(new File(fileLoc)); Toast errorToast7 = Toast.makeText(getActivity(), "Pls work " + n, Toast.LENGTH_SHORT); errorToast7.show(); ArrayList<String> test = new ArrayList<String>(); for (int i = 0; i < n; i++) { test.add(kb.nextLine()); //kb.next(); } return test;
感谢您的帮助!
我是Android Studio的新手,但有两个问题。因此,当前我正在使用一种方法来从.txt文件中提取行并将其转换为ArrayList。我正在使用FileReader和...