Java inputstream/outputstream 写入同名文件
Java inputstream/outputstream write to file with same name
我有一些代码遍历文件树并将根添加到 xml 没有根的文件中。我的问题是当我尝试从输入流写入输出流时。我想用更新版本(添加了根目录的版本)替换当前的 xml 文件。我认为如果我将输出流设为与输入流相同的文件,就会出现问题。直觉上,这似乎是个问题。如果没有,请告诉我。
我该如何补救?我怎样才能从本质上 "update" xml 文件,实际上覆盖另一个文件?我在这里查看了其他答案,但还没有深入了解。
private static void addRootHelper(File root){
FileInputStream fis;
List<InputStream> streams;
InputStream is;
OutputStream os;
File[] directoryListing = root.listFiles();
if (directoryListing != null) {
for (File child : directoryListing) {
addRootHelper(child);
}
}
else {
try{
// Add root to input stream and create output stream
fis = new FileInputStream(root);
streams = Arrays.asList(new ByteArrayInputStream("<root>".getBytes()),fis, new ByteArrayInputStream("</root>".getBytes()));
is = new SequenceInputStream(Collections.enumeration(streams));
os = new FileOutputStream(root.getAbsolutePath());
// Write from is -> os
byte[] buffer = new byte[1024];
int bytesRead;
// Read from is to buffer
while((bytesRead = is.read(buffer)) !=-1){
os.write(buffer, 0, bytesRead);
}
is.close();
os.flush();
os.close();
System.out.println("Added root to " + root.getName());
}
catch(IOException ex){
ex.printStackTrace();
}
}
}
如果您不想使用流行的临时文件方法,那么您始终可以只读入整个文件,然后再写回它。
这是一个直接的实现。
public static void addRootTag(File xml) throws IOException {
final List<String> lines = new ArrayList<>();;
try (Scanner in = new Scanner(xml)) {
while (in.hasNextLine())
lines.add(in.nextLine());
}
try (PrintStream out = new PrintStream(xml)) {
out.println("<root>");
for (String line : lines) {
// indentation, if you want
out.print(" ");
out.println(line);
}
out.println("</root>");
}
}
我有一些代码遍历文件树并将根添加到 xml 没有根的文件中。我的问题是当我尝试从输入流写入输出流时。我想用更新版本(添加了根目录的版本)替换当前的 xml 文件。我认为如果我将输出流设为与输入流相同的文件,就会出现问题。直觉上,这似乎是个问题。如果没有,请告诉我。
我该如何补救?我怎样才能从本质上 "update" xml 文件,实际上覆盖另一个文件?我在这里查看了其他答案,但还没有深入了解。
private static void addRootHelper(File root){
FileInputStream fis;
List<InputStream> streams;
InputStream is;
OutputStream os;
File[] directoryListing = root.listFiles();
if (directoryListing != null) {
for (File child : directoryListing) {
addRootHelper(child);
}
}
else {
try{
// Add root to input stream and create output stream
fis = new FileInputStream(root);
streams = Arrays.asList(new ByteArrayInputStream("<root>".getBytes()),fis, new ByteArrayInputStream("</root>".getBytes()));
is = new SequenceInputStream(Collections.enumeration(streams));
os = new FileOutputStream(root.getAbsolutePath());
// Write from is -> os
byte[] buffer = new byte[1024];
int bytesRead;
// Read from is to buffer
while((bytesRead = is.read(buffer)) !=-1){
os.write(buffer, 0, bytesRead);
}
is.close();
os.flush();
os.close();
System.out.println("Added root to " + root.getName());
}
catch(IOException ex){
ex.printStackTrace();
}
}
}
如果您不想使用流行的临时文件方法,那么您始终可以只读入整个文件,然后再写回它。
这是一个直接的实现。
public static void addRootTag(File xml) throws IOException {
final List<String> lines = new ArrayList<>();;
try (Scanner in = new Scanner(xml)) {
while (in.hasNextLine())
lines.add(in.nextLine());
}
try (PrintStream out = new PrintStream(xml)) {
out.println("<root>");
for (String line : lines) {
// indentation, if you want
out.print(" ");
out.println(line);
}
out.println("</root>");
}
}