我有多个看起来像这样的文件:
aaa bob ccc
;; ;; hh
did shhh ffff
sds dfd fgf
ff
aaa dd
我只想从每一行中取第二个单词。
这是我的Python代码。我希望在Java中也一样。请帮忙!
fin=open(path+'/'+item);
content= fin.readlines();
fin.close();
for line in content:
if ('*' in line) ==1 | ('#' in line==1) | (',' in line==1)|(';' in line==1) :
#it would be better if I could check for existence of characters alone!
continue;
else:
x=line.split();
foutput.write("\n".join(x))
我这样做了:但是新文件是空的,我得到了例外:
java.io.FileNotFoundException: Too many open files open
这是我的Java代码片段:
File folder = new File(path);
File[] listOfFiles = folder.listFiles();
System.out.println(listOfFiles.length);
List<String> filenames= new ArrayList<String>();
for (File file : listOfFiles) {
if (file.isFile() ) {
filenames.add(file.getName());
}
}
String path1,path2;
for (String s : filenames) {
path1 = path+"/"+s;
path2=old_path+"/fullfile.txt";
File file = new File(path1);
System.out.println(file.getName());
try {
Scanner sc = new Scanner(file);
while (sc.hasNextLine()) {
String i = sc.nextLine();
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(path2));
writer.write(i);
}
catch(Exception e) {
System.out.println(e);
}
}
sc.close();
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
我不确定如何在这里提取第二个单词。在Python中我通过检查空格来做到这一点,但我在这里一无所知!