【发布时间】:2012-11-29 01:21:39
【问题描述】:
可能重复:
Best Way to Write Bytes in the Middle of a File in Java
我正在编写一个修改 PostScript 文件以添加打印属性的程序,因此我需要在文件中间添加行。这些 PostScript 文件非常大,我想知道我使用的代码是否最有效。此代码读取源文件并写入一个临时文件,在需要的地方添加一行。
Writer out = null;
Scanner scanner = null;
String newLine = System.getProperty("line.separator");
scanner = new Scanner(new FileInputStream(fileToRead));
out = new OutputStreamWriter(new FileOutputStream(fileToWrite));
String line;
while(scanner.hasNextLine()){
line = scanner.nextLine();
out.write(line);
out.write(newLine);
if(line.equals("%%BeginSetup")){
out.write("<< /Duplex true /Tumble true >> setpagedevice");
out.write(newLine);
}
}
scanner.close();
out.close();
任何帮助将不胜感激,在此先感谢。
【问题讨论】:
-
在文件中你可以覆盖数据,你可以在最后追加数据。如果要在中间添加线条,则必须至少复制必须手动移动到稍后位置的部分。 -> 除了提高创建副本的速度之外,您无能为力提高效率
标签: java io postscript