[英]How can i display saved text file on list view?
File file = new
File(getApplicationContext().getFilesDir(),
"text.txt");
if(file.exists())
{
Toast.makeText(this, "File Exist" + file,
Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(this, "File Not Found" + file,
Toast.LENGTH_SHORT).show();
}
}
I want to display text.txt items on listview我想在列表视图中显示 text.txt 项目
text.txt:文本.txt:
1234
0000
abcd
i have checked if the file exist or not but i am not able to display the items on listview.我已经检查了文件是否存在,但我无法在列表视图中显示这些项目。
You can use a Scanner to read the components of your file like so:您可以使用扫描仪来读取文件的组成部分,如下所示:
// creates scanner of that file path
Scanner fileScanner = new Scanner(new File("path here"));
// read till there aren't elements
while(fileScanner.hasNext())
{ // printing whole that next line
System.out.println(fileScanner.next());
}
This will print the whole line till there are no more lines to read/print这将打印整行,直到没有更多的行可以读取/打印
NOTE fileScanner.next() is of type String, I am simply printing it to the console for display注意fileScanner.next() 是 String 类型,我只是将它打印到控制台进行显示
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.