用Java读取多个文本文件
我几乎没有文本文件。每个文本文件都包含一些路径和/或其他文件的引用 文件1 文件2 我想要的是,通过只提供File1.txt作为java程序的输入,将所有路径Mod1、Mod2、Mod3、Mod4复制到另一个文本文件中 到现在为止我做了什么用Java读取多个文本文件,java,recursion,readfile,Java,Recursion,Readfile,我几乎没有文本文件。每个文本文件都包含一些路径和/或其他文件的引用 文件1 文件2 我想要的是,通过只提供File1.txt作为java程序的输入,将所有路径Mod1、Mod2、Mod3、Mod4复制到另一个文本文件中 到现在为止我做了什么 public void readTextFile(String fileName){ try { br = new BufferedReader(new FileReader(new File(fileName)));
public void readTextFile(String fileName){
try {
br = new BufferedReader(new FileReader(new File(fileName)));
String line = br.readLine();
while(line!=null){
if(line.startsWith("#file#>")){
String string[] = line.split(">");
readTextFile(string[1]);
}
else if(line.contains(">")){
String string[] = line.split(">");
svnLinks.put(string[0], string[1]);
}
line=br.readLine();
}
} catch (Exception e) {
e.printStackTrace();
}
}
目前我的代码只读取File2.txt的内容,控件不会返回File1.txt。
请询问是否需要更多的输入。似乎很简单。在填充svnLinks映射后,您需要编写文件,前提是您当前的代码工作时没有看到任何太奇怪的东西 因此,一旦填充了地图,您可以使用以下内容:
File newFile = new File("myPath/myNewFile.txt");
// TODO check file can be written
// TODO check file exists or create
FileOutputStream fos = null;
OutputStreamWriter osw = null;
BufferedWriter bw = null;
try {
fos = new FileOutputStream(newFile);
osw = new OutputStreamWriter(fos);
bw = new BufferedWriter(osw);
for (String key: svnLinks.keySet()) {
bw.write(key.concat(" my separator ").concat(svnLinks.get(key)).concat("myNewLine"));
}
}
catch (Throwable t) {
// TODO handle more gracefully
t.printStackTrace();
if (bw != null) {
try {
bw.close();
}
catch (Throwable t) {
t.printStackTrace();
}
}
首先,您在不关闭当前读卡器的情况下跳转到另一个文件,当您返回时,光标将丢失。首先读取一个文件,然后写入与另一个文件匹配的所有内容。关闭当前读卡器不要关闭写卡器,然后打开下一个要读取的文件,依此类推。这里是方法的非递归实现:
public static void readTextFile(String fileName) throws IOException {
LinkedList<String> list = new LinkedList<String>();
list.add(fileName);
while (!list.isEmpty()) {
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(new File(list.pop())));
String line;
while ((line = br.readLine()) != null) {
if (line.startsWith("#file#>")) {
String string[] = line.split(">");
list.add(string[1]);
} else if (line.contains(">")) {
String string[] = line.split(">");
svnLinks.put(string[0], string[1]);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
br.close();
}
}
}
只是用LinkedList来维持秩序。我建议你增加一些计数器,如果你想把文件的读取限制在某个数字以内。例如:
while (!list.isEmpty() && readCount < 10 )
在循环引用的情况下,这将消除将代码运行到无穷远处的机会。请在否决表决后添加注释。第二个文件还可能包含“file>D:/FilePath/File3.txt”类型的行。这是对的吗?实际上代码并没有按预期工作。它只读取File2.txt的内容,即输出文件包含Mod3>/home/admin1/Mod3 Mod4>/home/admin1/mod4@SachinMhetre否,它会为第二个文件创建一个同名的新文件。因此,您只能看到上次读取的文件的内容。路径的顺序很重要。由于File1.txt仅在开头包含对File2.txt的引用,因此应首先读取File2.txt中的内容。@SachinMhetre然后首先创建一个列表。打开file1.txt,然后您会看到它引用了file2.txt。在列表中添加file2.txt。关闭file1.txt,然后打开file2.txt,如果是file3.txt,则将file3.txt放入列表中,依此类推。当您没有更多的文件要读取时,请浏览您创建的列表,然后按顺序读取。我弄错了吗?
public static void readTextFile(String fileName) throws IOException {
LinkedList<String> list = new LinkedList<String>();
list.add(fileName);
while (!list.isEmpty()) {
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(new File(list.pop())));
String line;
while ((line = br.readLine()) != null) {
if (line.startsWith("#file#>")) {
String string[] = line.split(">");
list.add(string[1]);
} else if (line.contains(">")) {
String string[] = line.split(">");
svnLinks.put(string[0], string[1]);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
br.close();
}
}
}
while (!list.isEmpty() && readCount < 10 )