簡體   English   中英

如果為空,則刪除Java中XML文件中的第一行

[英]Delete first line in XML file in Java if empty

我從服務器收到了我的大學時間表文件,並嘗試從中提取數據。 在某些文件中(對於某些部門),在頂部有一個空白行,它是文件的第一行,因此我得到:

[Fatal Error] lesson:2:6: The processing instruction target matching "[xX][mM][lL]" is not allowed.

如何檢查空白行並在Java的同一文件中將其刪除? 我無法對字符串和行進行任何處理,因為XML文件通常在行末沒有\\n

UPD

//it appeared on knt/151 file, so empty lines in the beginning of the file that caused fatal error
private void checkForEmptyLines(File f) {
    try {
        RandomAccessFile raf = new RandomAccessFile(f,"rw");
        while (raf.getFilePointer()!=raf.length()){
           //What should be here?
           Byte b = raf.readByte();
           if (b!=10)
               raf.write(b);
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }


}

UPD xml文件處理:

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文件示例

對此沒有快速簡單的答案,但足以說明您應該看到將輸入視為流。 我已經更新了您的“檢查空行”方法,從本質上使流前進,直到達到第一個“ <”字符,然后重置流並進行處理

//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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM