这是我的情况。我正在将IPreferenceStore store = EditorsUI.getPreferenceStore();
store.getInt(AbstractDecoratedTextEditorPreferenceConstants.EDITOR_TAB_WIDTH));
声明写入XML文件。我使用扫描仪读取原始XML文件。当扫描程序读取输入文件时,我将这些行写入BufferedWriter。当扫描仪在第2行时,我从传递给该方法的ArrayList中写入<!ENTITY>
值。我的<!ENTITY>
值写没有问题。我遇到的问题是我只将400行左右的文件写入输出文件。
我已经阅读了一些关于BufferedWriters没有完成文件写入的帖子,所有这些似乎都指向确保编写器关闭。我关闭了我的作家对象。
<!ENTITY>
编写器的输出用于生成PDF。正在读取的文件和添加的private void addEntityRefs(Map<String, String> icns, File xml)
{
String path = xml.getAbsolutePath().substring(0,xml.getAbsolutePath().lastIndexOf(File.separator)+1);
ArrayList<String> list = new ArrayList<String>();
Scanner reader = null;
BufferedWriter writer = null;
for(Map.Entry<String, String> e : icns.entrySet())
{
list.add(e.getValue());
}
try
{
reader = new Scanner(xml);
//standardOut.println("Reading " + xml.getName());
//System.out.println();
int c = 0;
String output = path + "out2.xml";
writer = new BufferedWriter(new FileWriter(new File(output)));
while(reader.hasNextLine())
{
c++;
if(c == 1)
{
writer.append(reader.nextLine().replaceAll("\\s", " "));
}
else if(c == 2)
{
System.out.println("writing entities @ line 2");
writer.append("\n<!DOCTYPE pm [\n");
for(int i = 0; i < list.size(); i++)
{
writer.append("<!ENTITY " + list.get(i).trim() + " SYSTEM \"" + list.get(i).trim() + ".cgm\" NDATA cgm>\n");
}
writer.append("<!NOTATION cgm PUBLIC \"cgm\" \"\">\n]>\n");
}
else
{
System.out.println("Writing line " + c);
writer.append(reader.nextLine().replaceAll("\\s", " ")+ "\r");
}
}
}
catch (FileNotFoundException ex)
{
Logger.getLogger(AARPdfGenUI.class.getName()).log(Level.WARN, null, ex);
JOptionPane.showMessageDialog(this, ex, "File Not Found Exception", JOptionPane.ERROR_MESSAGE);
}
catch (IOException ex)
{
Logger.getLogger(AARPdfGenUI.class.getName()).log(Level.WARN, null, ex);
JOptionPane.showMessageDialog(this, ex, "IO Exception", JOptionPane.ERROR_MESSAGE);
}
finally
{
try
{
reader.close();
writer.close();
}
catch(Exception e)
{
Logger.getLogger(AARPdfGenUI.class.getName()).log(Level.WARN, null, e);
JOptionPane.showMessageDialog(this, e, "Exception", JOptionPane.ERROR_MESSAGE);
}
}
}
声明大约为26,000行。有人能指出我哪里出错了吗?当我从NetBeans运行应用程序时,此方法可以正常工作,但是一旦我构建它并尝试从JAR文件运行,它就会在大约400行之后停止。
答案 0 :(得分:0)
当它停在某一行时。你看到用这些行创建的文件?可能是那个时候刷新了缓冲区并且在那个操作中失败了。
当c == 2时,您不会将指针移动到下一行,而是在下一次迭代中继续写入。 在读/写时使用charset总是更好。 最后试着冲洗。 并独立地关闭读者和作者。 不必使用ArrayList。
我对这个做了一些小改动。看它是否仍然有效。
private void addEntityRefs(Map<String, String> icns, File xml) {
Scanner reader = null;
BufferedWriter writer = null;
try {
reader = new Scanner(xml, "utf-8");
// standardOut.println("Reading " + xml.getName());
// System.out.println();
int count = 0;
File targetFile = new File(xml.getParentFile(), "out2.xml");
if (!targetFile.exists()) {
boolean created = targetFile.createNewFile();
System.out.println("File created: " + created);
}
writer = new BufferedWriter(new FileWriter(targetFile));
while (reader.hasNextLine()) {
count++;
if (count == 1) {
writer.append(reader.nextLine().replaceAll("\\s", " "));
} else if (count == 2) {
System.out.println("writing entities @ line 2");
writer.append("\n<!DOCTYPE pm [\n");
for (String item : icns.keySet()) {
item = item.trim();
writer.append("<!ENTITY " + item + " SYSTEM \"" + item + ".cgm\" NDATA cgm>\n");
}
writer.append("<!NOTATION cgm PUBLIC \"cgm\" \"\">\n]>\n");
} else {
System.out.println("Writing line " + count);
writer.append(reader.nextLine().replaceAll("\\s", " ")).append("\r");
}
}
// done writing
writer.flush();
} catch (FileNotFoundException ex) {
Logger.getLogger(AARPdfGenUI.class.getName()).log(Level.WARN, null, ex);
JOptionPane.showMessageDialog(this, ex, "File Not Found Exception", JOptionPane.ERROR_MESSAGE);
} catch (IOException ex) {
Logger.getLogger(AARPdfGenUI.class.getName()).log(Level.WARN, null, ex);
JOptionPane.showMessageDialog(this, ex, "IO Exception", JOptionPane.ERROR_MESSAGE);
} finally {
try {
reader.close();
} catch (Exception e) {
Logger.getLogger(AARPdfGenUI.class.getName()).log(Level.WARN, null, e);
JOptionPane.showMessageDialog(this, e, "Exception", JOptionPane.ERROR_MESSAGE);
}
try {
writer.close();
} catch (Exception e) {
Logger.getLogger(AARPdfGenUI.class.getName()).log(Level.WARN, null, e);
JOptionPane.showMessageDialog(this, e, "Exception", JOptionPane.ERROR_MESSAGE);
}
}
}