使程序从文件夹中检索.txt文件。每个.txt文件包含1或2行字。我从文件夹中获得每个.txt文件的每个文件名的输出:输出.txt文件中的每个字符串
Donatello
Galileo
Leonardo
Michael
Rafael
ex。代码我到目前为止:
import java.io.*;
import java.util.*;
public class Liarliar {
public static void main(String args[])throws IOException{
File listofnames = new File("C:\\List of names");
File[] names = listofnames.listFiles();
for(File person : names){
int index = person.getName().lastIndexOf(".");
if(index>0&& index <= person.getName().length()-2){
System.out.println(person.getName().substring(0, index));
}
}
}
}
这都是文件名,但那不是我想成为输出。我想要的输出是:
Donatello 2
Hi
Hello
Galileo 2
Fine
Thanks
Leonardo 1
Hi
Michael 1
Hello
Rafael 1
Hi
说明:上面给出的示例输出例如, Donatello 2,空格后面的数字2和名称Donatello,2对应于文件名Donatello.txt内的单词数量。 2因为有两行文字。我希望你明白我的观点。
任何帮助将非常感激。谢谢你...
[输出目录中多个.txt文件中的字符串]的可能重复(http://stackoverflow.com/questions/5481534/output-the-strings-inside-of-multiple-txt-files-in -a-directory) – 2011-03-31 02:41:44