我在目录C:\ Users \ Mahady \ Desktop \ Java 31122011 \ src \ register \
中有一个文件列表他们是这样的......
100100545.txt
100545454.txt等等
在每个文件中,文件数据就像这样逐行:
鲍勃
1234
4834
伦敦
9852
1
我的问题是,如何在目录中逐个读取每个文件,并且每个文件读取除第3行之外的所有行。然后我想将这些数据合并到word中并创建字母。谢谢
答案 0 :(得分:2)
详细答案......
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class FileRead {
public static void main(String[] args) {
FileReader fileReader = null;
BufferedReader bufferedReader = null;
try {
File folder = new File("C:/Users/Mahady/Desktop/Java 31122011/src/register/");
if (folder.isDirectory()) {
for (File file : folder.listFiles()) {
fileReader = new FileReader(file);
bufferedReader = new BufferedReader(fileReader);
String line = null;
int lineCount = 0;
while (null != (line = bufferedReader.readLine())) {
lineCount++;
if (3 != lineCount) {
System.out.println(line);
}
}
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (null != bufferedReader)
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
希望这会对你有所帮助。
答案 1 :(得分:1)
试试这个:
File dir = new File("C:\\Users\\Mahady\\Desktop\\Java 31122011\\src\\register\\");
for (string fn : dir.list()) {
FileInputStream fstream = new FileInputStream(fn);
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
while ((strLine = br.readLine()) != null) {
System.out.println (strLine);
}
in.close();
}
显然,您需要围绕此骨架实现添加异常处理代码。