删除Java中XML文件的第一行(如果为空)
我从服务器上收到一个包含我的大学时间表的文件,并试图从中提取数据。在某些部门的某些文件中,顶部有一个空行,它是文件的第一行,因此我得到: [致命错误]课程:2:6:不允许处理指令目标与[xX][mM][lL]匹配 如何检查空白行并在java中删除同一个文件?我无法处理字符串和行,因为XML文件通常在行的末尾没有\n UPD UPD xml文件处理:删除Java中XML文件的第一行(如果为空),java,xml,parsing,Java,Xml,Parsing,我从服务器上收到一个包含我的大学时间表的文件,并试图从中提取数据。在某些部门的某些文件中,顶部有一个空行,它是文件的第一行,因此我得到: [致命错误]课程:2:6:不允许处理指令目标与[xX][mM][lL]匹配 如何检查空白行并在java中删除同一个文件?我无法处理字符串和行,因为XML文件通常在行的末尾没有\n UPD UPD xml文件处理: public String[][] parse(String path) { String[][] table = new String[
public String[][] parse(String path) {
String[][] table = new String[8][6];
File data = new File(path);
// checkForEmptyLines(data);
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = null;
Document doc = null;
try {
dBuilder = dbFactory.newDocumentBuilder();
doc = dBuilder.parse(data);
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
doc.getDocumentElement().normalize();
NodeList nodeList = doc.getElementsByTagName("Data");
int rowIndex = 0;
int columnIndex = 0;
for (int i = 0; i < nodeList.getLength(); ++i) {
if (i > 7 && !((i - 14) % 7 == 0)) {
Node node = nodeList.item(i);
String line = node.getTextContent().replaceAll("\\t+", " ");
line = line.replace("\n", " ");
if (columnIndex >= 6) {
columnIndex = 0;
++rowIndex;
}
table[rowIndex][columnIndex++] = line;
}
}
XML文件对此没有快速简单的答案,但可以说您应该了解如何将输入视为流。我已经更新了您的checkforemptylines方法,以基本上推进一个流,直到它到达第一个',我的同事添加了这段代码,它似乎可以工作。它不仅在开始时检查空字符串,还删除它并将正确的数据写入新文件 这个解决方案似乎很慢,如果可以做任何改进,请告诉我
private static File skipFirstLine(File inputFile) {
File outputFile = new File("skipped_" + inputFile.getName());
try (BufferedReader reader = new BufferedReader(new FileReader(inputFile));
BufferedWriter writer = new BufferedWriter(new FileWriter(outputFile))) {
String line;
int count = 0;
while ((line = reader.readLine()) != null) {
if (count == 0 && line.equals("")) {
++count;
continue;
}
writer.write(line);
writer.write("\n");
++count;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return outputFile;
}
展示你是如何阅读文件…显示你的代码高兴我怀疑空白线是造成错误。@波希米亚,但如果我不使用这种方法,删除第一个空白行,我把一切都做好了。今天晚些时候,我将附加XML处理。对于您试图解析的XML,我将使用line.trim.isEmpty发布一个示例。如果第一行甚至包含一个空格字符,代码将失败。
//it appeared on knt/151 file, so empty lines in the beginning of the file that caused fatal error
private void checkForEmptyLines(BufferedInputStream fs) throws IOException {
// Set mark and allow for up to 1024 characters to be read before this mark becomes invalid
fs.mark(1024);
int ch;
while( -1 != (ch = fs.read()) {
if( '<' == ch ) {
fs.reset();
break;
}
else {
fs.mark(1024);
}
}
}
public String[][] parse(String path) {
String[][] table = new String[8][6];
File data = new File(path);
FileInputStream dataStream= new FileInputStream(data);
BufferedInputStream bufferedDataStream= new BufferedDataStream(dataStream, 1024);
checkForEmptyLines(bufferedDataStream);
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = null;
Document doc = null;
try {
dBuilder = dbFactory.newDocumentBuilder();
doc = dBuilder.parse(bufferedDataStream);
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
doc.getDocumentElement().normalize();
NodeList nodeList = doc.getElementsByTagName("Data");
int rowIndex = 0;
int columnIndex = 0;
for (int i = 0; i < nodeList.getLength(); ++i) {
if (i > 7 && !((i - 14) % 7 == 0)) {
Node node = nodeList.item(i);
String line = node.getTextContent().replaceAll("\\t+", " ");
line = line.replace("\n", " ");
if (columnIndex >= 6) {
columnIndex = 0;
++rowIndex;
}
table[rowIndex][columnIndex++] = line;
}
}
private static File skipFirstLine(File inputFile) {
File outputFile = new File("skipped_" + inputFile.getName());
try (BufferedReader reader = new BufferedReader(new FileReader(inputFile));
BufferedWriter writer = new BufferedWriter(new FileWriter(outputFile))) {
String line;
int count = 0;
while ((line = reader.readLine()) != null) {
if (count == 0 && line.equals("")) {
++count;
continue;
}
writer.write(line);
writer.write("\n");
++count;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return outputFile;
}