gpt4 book ai didi

java - 写入文本文件时,未写入大约 4,000 个值

转载 作者:行者123 更新时间:2023-11-29 07:00:30 26 4
gpt4 key购买 nike

我一直在尝试解析 XML 文件以将我需要的每个节点存储到数组/ArrayList 中;我都试过了。解析器能够获取我想要的每个值并将其存储到数组(列表)中。我什至打印了之后的长度,它正好是 262,144 个值长 (512*512)。但是,一旦我尝试将此数组打印到文本文件中,它总是会遗漏大约 3800 个值。我已经尝试了很多方法来让它工作,但无论我尝试存储多少值(即使尝试使用只有大约 147,000 个值的 XML)它仍然遗漏了大约 4000 个值。这是我的解析器/编写器。

public class Reader 
{
private String path;

public static void main(String[] args)
{
Document xmlDoc = getDocument("./src/porc.tmx");

String fileName = "out.txt";
String[] array = new String[262144];
try
{
NodeList gidList = xmlDoc.getElementsByTagName("tile");
for(int i = 0; i < gidList.getLength(); i++)
{
Node g = gidList.item(i);
if(g.getNodeType() == Node.ELEMENT_NODE)
{
Element gid = (Element) g;
String id = gid.getAttribute("gid");
array[i]=id;
}
}

PrintWriter outputStream = new PrintWriter(fileName);
for(int j = 0; j < 262144; j++)
{
outputStream.println(array[j]);
}
}

catch(Exception e)
{
e.printStackTrace();
}
}

private static Document getDocument(String docString)
{
try
{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

factory.setIgnoringComments(true);
factory.setIgnoringElementContentWhitespace(true);
factory.setValidating(false);

DocumentBuilder builder = factory.newDocumentBuilder();

return builder.parse(new InputSource(docString));
}

catch(Exception ex)
{
System.out.println(ex.getMessage());
}

return null;
}

最佳答案

我建议您在处理结束时关闭 outputStream

PrintWriter outputStream = null;
try
{
NodeList gidList = xmlDoc.getElementsByTagName("tile");
for(int i = 0; i < gidList.getLength(); i++)
{
Node g = gidList.item(i);
if(g.getNodeType() == Node.ELEMENT_NODE)
{
Element gid = (Element) g;
String id = gid.getAttribute("gid");
array[i]=id;
}
}

outputStream = new PrintWriter(fileName);
for(int j = 0; j < 262144; j++)
{
outputStream.println(array[j]);
}
}

catch(Exception e)
{
e.printStackTrace();
}
finally
{
if (outputStream != null)
outputStream.close();
}

关于java - 写入文本文件时,未写入大约 4,000 个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26903549/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com