(Java)如何逐块读取文本文件

时间:2014-07-13 17:03:57

标签: java

假设我有以下文本文件,如何读取Java中由2个空行分隔的每个行块?

谢谢!

Reference Type:  Journal Article
Record Number: 153
Author: Yang, W. and Kang, J.
Year: 2005
Title: Acoustic comfort evaluation in urban open public spaces
Journal: Applied Acoustics
Volume: 66
Issue: 2
Pages: 211-229
Short Title: Acoustic comfort evaluation in urban open public spaces
ISSN: 0003682X
DOI: 10.1016/j.apacoust.2004.07.011
'File' Attachments: internal-pdf://0633242026/Acoustic comfort evaluation in urban open public spaces.pdf


Reference Type:  Thesis
Record Number: 3318
Author: Wienold, Jan
Year: 2009
Title: Daylight glare in offices
University: Fraunhofer Institute for Solar Energy Systems ISE
Thesis Type: PhD Dissertation
Short Title: Daylight glare in offices
URL: http://publica.fraunhofer.de/eprints/urn:nbn:de:0011-n-1414579.pdf
'File' Attachments: internal-pdf://2172014641/Daylight glare in offices.pdf

在这个论坛上回答问题似乎很挑剔......我认为这没有必要。然而,这是我的尝试通过Processing,一个基于Java的编程环境:

import java.util.*;
String fileName = "";
String line;
BufferedReader br;

void setup(){
  fileName = "My_EndNote_Library_2014-07-04.txt";
  br = createReader(fileName);  
}

void draw(){
  try {
    line = br.readLine();
    println(line);
    println();
  } catch (IOException e) {
    e.printStackTrace();
    line = null;
  }

   if (line == null) {
    // Stop reading because of an error or file is empty
    noLoop();  
  }
}

2 个答案:

答案 0 :(得分:0)

由于每个块的数据(行)不相同,您可以执行类似的操作。使用\n\n作为每个块的分隔符,并为每行使用\n

import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException {   
        BufferedReader br = new BufferedReader(new FileReader("file.txt"));
        StringBuffer sb = new StringBuffer();
        while (true) {
            String line = br.readLine();
            if (line == null) break;
            sb.append(line).append("\n");
        }       

        String[] blocks = sb.toString().split("\n\n");

        for (String block : blocks) {
            block = block.trim();
            // block - individual block from file
            String[] data = block.split("\n");
            for (String d : data) {
                // d - individual line of block 
            }   
        }   
    }    
}

答案 1 :(得分:0)

尽管逻辑的本质是正确的,因为您不需要任何复杂的正则表达式等,但是在接受的答案中有两个缺陷,

1。由于\n是硬编码的,因此代码不是操作系统无关的

2。第二,由于在每行之后添加了\n,所以在两个块之间将有三个\n字符,而不是两个\n字符(两个空行中的两个,一个上一个区块的额外内容)。在两个字符上拆分也可以,但是在病房上 block-1 会在开始时包含额外的新行,因此您可能需要修剪。

代码假定该文件位于类路径上,而不位于磁盘上。

import java.io.BufferedReader;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class ReferenceType {

    public static void main(String[] args) {
        ReferenceType app = new ReferenceType();
        String allLines = null;
        String[] blocks = null;
        String lineSeparator = System.getProperty("line.separator");
        try {
            allLines = app.getFileAsString(lineSeparator);
            blocks = allLines.split(lineSeparator+lineSeparator+lineSeparator);
        } catch (URISyntaxException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    private String getFileAsString(String lineSeparator) throws URISyntaxException, IOException {
        Path path = Paths.get(this.getClass().getResource("ReferenceType.txt").toURI());
        String textLine = null;
        StringBuilder builder = new StringBuilder();
        try (BufferedReader br = Files.newBufferedReader(path)) {
            while ((textLine = br.readLine()) != null) {
                    builder.append(textLine);
                    builder.append(lineSeparator);
            }
        }
        return builder.toString();
    }
}