这里我逐字逐句阅读文件并用这些词操纵列表视图。这里的问题是名字和姓氏出现在不同的行中。例如Name =“John Clerk”然后我在第一行获得“John”,在List视图的第二行获得“Clerk”。它们必须是单行的,以此类推其他数据。我应该做些什么改变才能正常工作?我的代码......
String myData = "";
String strLine;
String listName = "" ;
FileOutputStream fos;
FileInputStream fstream;
DataInputStream in;
String[] SavedFiles;
BufferedReader br;
public void readFile(String file) throws IOException
{
fstream = openFileInput(file);
Scanner scanFile = new Scanner(new DataInputStream(fstream));
ArrayList<String> words = new ArrayList<String>();
String theWord, theWord1, theWord2;
while (scanFile.hasNext())
{
theWord = scanFile.next();
words.add(theWord);
}
Toast.makeText(getBaseContext(), "" + size, 1000).show();
adapterFriends = new ArrayAdapter<String>(getBaseContext(), R.layout.text, words);
lvFinal.setAdapter(adapterFriends);
adapterFriends.notifyDataSetChanged();
}
答案 0 :(得分:2)
尝试使用nextLine()而不是next(),因为它应该返回\ n字符之间的每个字符串。
http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Scanner.html#nextLine()
希望有所帮助
答案 1 :(得分:1)
如果我理解你需要什么,试试这个:
while (scanFile.hasNext())
{
String name = scanFile.next();
if (scanFile.hasNext())
{
name = String.format("%s %s", name, scanFile.next());
}
words.add(name);
}