Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/396.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java,如何从一个大文件中提取一些文本并将其导入到一个较小的文件中_Java_Xml_Text_Io - Fatal编程技术网

Java,如何从一个大文件中提取一些文本并将其导入到一个较小的文件中

Java,如何从一个大文件中提取一些文本并将其导入到一个较小的文件中,java,xml,text,io,Java,Xml,Text,Io,我对Java编程比较陌生,我正在尝试创建一个可以帮助一些同事的应用程序 我尝试做的背景是,读取一个大文件的内容,最多可能超过400000行,其中包含XML,但不是有效的XML文档,就像日志一样 我试图做的是构建一个应用程序,其中用户输入一个唯一的ID,然后扫描文档以确定它是否存在,如果存在,并且该唯一ID通常会在生成的XML中出现几次,然后我想向后遍历到一个节点ID,然后将所有内容从该节点复制到其关闭节点,并将其放入自己的文档中 我知道如何创建新文档,但我正在努力找出如何基本上“向后查找”并将所

我对Java编程比较陌生,我正在尝试创建一个可以帮助一些同事的应用程序

我尝试做的背景是,读取一个大文件的内容,最多可能超过400000行,其中包含XML,但不是有效的XML文档,就像日志一样

我试图做的是构建一个应用程序,其中用户输入一个唯一的ID,然后扫描文档以确定它是否存在,如果存在,并且该唯一ID通常会在生成的XML中出现几次,然后我想向后遍历到一个节点ID
,然后将所有内容从该节点复制到其关闭节点,并将其放入自己的文档中

我知道如何创建新文档,但我正在努力找出如何基本上“向后查找”并将所有内容复制到结束标记,非常感谢您的帮助

编辑

不幸的是,到目前为止,我还没有弄清楚如何实施这三个建议中的任何一个

相关ID是前面提到的唯一参考

我目前的代码是

String correlationId = correlationID.getText();
BufferedReader bf = new BufferedReader(new FileReader(f));
System.out.println("Looking for " + correlationId);
int lineCount = 0;
String line;

while ((line = bf.readLine()) != null) {
    lineCount++;
    int indexFound = line.indexOf(correlationId);

    if (indexFound > -1) {
        System.out.println("Found CorrelationID on line " + "\t" + lineCount + "\t" + line);
    }
}

bf.close();
非常感谢任何进一步的帮助,我不是要求有人为我写,只是一些非常清楚和基本的说明:)请

编辑2


可以找到我试图读取和提取的文件副本

当您在文件中查找唯一ID时,请保留对您遇到的最新
documentRequestMessage
的引用。当您找到唯一ID时,您就已经拥有了提取消息所需的引用

在这种情况下,“参考”可能意味着几件事。由于您没有遍历DOM(因为它不是有效的XML),您可能只会将
documentRequestMessage
所在的位置存储在文件中。如果您使用的是
文件输入流
(或支持
标记
的任何
输入流
),则只需
标记
/
重置
即可存储并返回到文件中消息开始的位置

这是我相信您正在寻找的实现。它基于您链接的日志文件进行了很多假设,但它适用于示例文件:

private static void processMessages(File file, String correlationId)
{
    BufferedReader reader = null;

    try {
        boolean capture = false;
        StringBuilder buffer = new StringBuilder();
        String lastDRM = null;
        String line;

        reader = new BufferedReader(new FileReader(file));

        while ((line = reader.readLine()) != null) {
            String trimmed = line.trim();

            // Blank lines are boring
            if (trimmed.length() == 0) {
                continue;
            }

            // We only actively look for lines that start with an open
            // bracket (after trimming)
            if (trimmed.startsWith("[")) {
                // Do some house keeping - if we have data in our buffer, we
                // should check it to see if we are interested in it
                if (buffer.length() > 0) {
                    String message = buffer.toString();

                    // Something to note here... at this point you could
                    // create a legitimate DOM Document from 'message' if
                    // you wanted to

                    if (message.contains("documentRequestMessage")) {
                        // If the message contains 'documentRequestMessage'
                        // then we save it for later reference
                        lastDRM = message;
                    } else if (message.contains(correlationId)) {
                        // If the message contains the correlationId we are
                        // after, then print out the last message with the
                        // documentRequestMessage that we found, or an error
                        // if we never saw one.
                        if (lastDRM == null) {
                            System.out.println(
                                    "No documentRequestMessage found");
                        } else {
                            System.out.println(lastDRM);
                        }

                        // In either case, we're done here
                        break;
                    }

                    buffer.setLength(0);
                    capture = false;
                }

                // Based on the log file, the only interesting messages are
                // the ones that are DEBUG
                if (trimmed.contains("DEBUG")) {
                    // Some of the debug messages have the XML declaration
                    // on the same line, and some the line after, so let's
                    // figure out which is which...
                    if (trimmed.endsWith("?>")) {
                        buffer.append(
                                trimmed.substring(
                                    trimmed.indexOf("<?")));
                        buffer.append("\n");
                        capture = true;
                    } else if (trimmed.endsWith("Message:")) {
                        capture = true;
                    } else {
                        System.err.println("Can't handle line: " + trimmed);
                    }
                }
            } else {
                if (capture) {
                    buffer.append(line).append("\n");
                }
            }
        }
    } catch (IOException ex) {
        ex.printStackTrace(System.err);
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException ex) {
                /* Ignore */
            }
        }
    }
}
private static void processMessages(文件、字符串关联ID)
{
BufferedReader reader=null;
试一试{
布尔捕获=假;
StringBuilder缓冲区=新的StringBuilder();
字符串lastDRM=null;
弦线;
reader=newbufferedreader(newfilereader(file));
而((line=reader.readLine())!=null){
String trimmed=line.trim();
//空行很无聊
如果(trimmed.length()==0){
继续;
}
//我们只积极寻找以开放式开头的行
//支架(修剪后)
如果(用(“[”)修剪.startsWith){
//做一些内部管理-如果我们的缓冲区中有数据,我们
//我们应该检查一下,看看我们是否对它感兴趣
if(buffer.length()>0){
字符串消息=buffer.toString();
//这里需要注意的是…在这一点上你可以
//根据“消息”创建合法的DOM文档,如果
//你想
if(message.contains(“documentRequestMessage”)){
//如果消息包含“documentRequestMessage”
//然后我们保存它以供以后参考
lastDRM=消息;
}else if(message.contains(correlationId)){
//如果消息包含correlationId,我们将
//之后,用
//documentRequestMessage是我们发现的,还是一个错误
//如果我们从未见过。
if(lastDRM==null){
System.out.println(
“未找到documentRequestMessage”);
}否则{
System.out.println(lastDRM);
}
//不管是哪种情况,我们都结束了
打破
}
buffer.setLength(0);
捕获=假;
}
//根据日志文件,唯一有趣的消息是
//正在调试的那些
if(trimmed.contains(“调试”)){
//某些调试消息具有XML声明
//在同一条线上,还有一些在后面,所以让我们
//找出哪个是哪个。。。
如果(用(“?>”)修剪.endsWith){
buffer.append(
3.子串(

trimmed.indexOf(“在向前阅读文件以查找唯一ID时,请保留对您遇到的最新
documentRequestMessage
的引用。当您找到唯一ID时,您已经拥有了提取消息所需的引用

在此上下文中,“引用”可能意味着几件事。由于您没有遍历DOM(因为它不是有效的XML),您可能只会将
documentRequestMessage
所在的位置存储在文件中。如果您使用的是
FileInputStream
(或支持
标记的任何
InputStream
),您只需
标记
/
重置
即可存储并返回到文件中消息开始的位置

这是我相信你的一个实现