读取文件并在找到分隔符时拆分它的内容

时间:2016-03-21 14:11:15

标签: java

我的文件内容如下:

nellkb:company_dc
  rdfs:label "dC"  "WASHINGTON" , "Washington" ;
  skos:prefLabel "www.wikipedia.com" .
nellkb:politicsblog_quami_ekta
  rdfs:label "Quami Ekta" ;
  skos:prefLabel "Quami Ekta" .
nellkb:female_ramendra_kumar
  rdfs:label "Ramendra Kumar" ;
  skos:prefLabel "Ramendra Kumar" .

我需要将文件拆分为分隔符'。'并在字符串中保存我们之前的内容。我怎样才能做到这一点 ?我尝试了以下但它不起作用

try {
    String sCurrentLine = null;
    int i = 0;
    br = new BufferedReader(new FileReader(rdfInstanceFile));
    while ((sCurrentLine = br.readLine()) != null) {
        splitted = sCurrentLine.split(".");
    }
} catch (IOException e) {
    e.printStackTrace();
}

3 个答案:

答案 0 :(得分:6)

使用Scanner课程。这种情况非常适合它。您需要做的就是指定'\\.'分隔符。

无需构建字符串,然后将其拆分......

import java.io.InputStream;
import java.util.Scanner;

public class ScanFile {
    public static void main(String[] args) {
        try {
            InputStream is = ScanFile.class.getClassLoader().getResourceAsStream("resources/foo.txt");
            Scanner scan = new Scanner(is);
            scan.useDelimiter("\\.[\r\n]+"); // Tokenize at dots (.) followed by CR/LF.

            int i = 1;
            while (scan.hasNext()) {
                String line = scan.next().trim();

                System.out.printf("Line #%d%n-------%n%n%s%n%n", i++, line);
            }

            scan.close();
            is.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

输出

Line #1
-------

nellkb:company_dc
  rdfs:label "dC"  "WASHINGTON" , "Washington" ;
  skos:prefLabel "WASHINGTON"

Line #2
-------

nellkb:politicsblog_quami_ekta
  rdfs:label "Quami Ekta" ;
  skos:prefLabel "Quami Ekta"

Line #3
-------

nellkb:female_ramendra_kumar
  rdfs:label "Ramendra Kumar" ;
  skos:prefLabel "Ramendra Kumar"

其他信息

  

useDelimiter

     

public Scanner useDelimiter(String pattern)

     

将此扫描仪的分隔模式设置为由指定的String构建的模式。

     

调用useDelimiter(pattern)形式的此方法的行为与调用useDelimiter(Pattern.compile(pattern)完全相同。

     

调用reset()方法会将扫描仪的分隔符设置为默认值。

     

<强>参数
  pattern - 指定分隔模式的字符串
  的返回
  这个扫描仪

Scanner构造函数需要六(6)种不同类型的对象:FileInputStreamPathReadableReadableByteChannel,和String

// Constructs a new Scanner that produces values scanned from the specified file.
Scanner(File source)
// Constructs a new Scanner that produces values scanned from the specified file.
Scanner(File source, String charsetName)
// Constructs a new Scanner that produces values scanned from the specified input stream.
Scanner(InputStream source)
// Constructs a new Scanner that produces values scanned from the specified input stream.
Scanner(InputStream source, String charsetName)
// Constructs a new Scanner that produces values scanned from the specified file.
Scanner(Path source)
// Constructs a new Scanner that produces values scanned from the specified file.
Scanner(Path source, String charsetName)
// Constructs a new Scanner that produces values scanned from the specified source.
Scanner(Readable source)
// Constructs a new Scanner that produces values scanned from the specified channel.
Scanner(ReadableByteChannel source)
// Constructs a new Scanner that produces values scanned from the specified channel.
Scanner(ReadableByteChannel source, String charsetName)
// Constructs a new Scanner that produces values scanned from the specified string.
Scanner(String source)

高级解决方案

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class ScanFile {
    private static ClassLoader loader = ScanFile.class.getClassLoader();

    private static interface LineProcessor {
        void process(String line);
    }

    private static interface Reader<T> {
        T read(String resource, String delimiter) throws IOException;
        void flush();
    }

    private abstract static class FileScanner<T> implements Reader<T> {
        private LineProcessor processor;
        public void setProcessor(LineProcessor processor) {
            this.processor = processor;
        }

        public T read(Scanner scan, String delimiter, boolean close) throws IOException {
            scan.useDelimiter(delimiter);
            while (scan.hasNext()) {
                processor.process(scan.next().trim());
            }
            if (close) {
                scan.close();
            }
            return null;
        }

        public T read(InputStream is, String delimiter, boolean close) throws IOException {
            T t = read(new Scanner(is), delimiter, true);
            if (close) {
                is.close();
            }
            return t;
        }

        public T read(String resource, String delimiter) throws IOException {
            return read(loader.getResourceAsStream("resources/" + resource), delimiter, true);
        }
    }

    public static class FileTokenizer extends FileScanner<List<String>> {
        private List<String> tokens;
        public List<String> getTokens() {
            return tokens;
        }
        public FileTokenizer() {
            super();
            tokens = new ArrayList<String>();
            setProcessor(new LineProcessor() {
                @Override
                public void process(String token) {
                    tokens.add(token);
                }
            });
        }
        public List<String> read(Scanner scan, String delimiter, boolean close) throws IOException {
            super.read(scan, delimiter, close);
            return tokens;
        }
        @Override
        public void flush() {
            tokens.clear();
        }
    }

    public static void main(String[] args) {
        try {
            FileTokenizer scanner = new FileTokenizer();
            List<String> items = scanner.read("foo.txt", "\\.[\r\n]+");

            for (int i = 0; i < items.size(); i++) {
                System.out.printf("Line #%d%n-------%n%n%s%n%n", i + 1, items.get(i));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

答案 1 :(得分:2)

首先将文件内容读入字符串,拆分字符串并将其保存在字符串数组中。

try {
        String sCurrentLine = "";
        StringBuilder content = new StringBuilder();
        String splitted[]= null;
        int i = 0;
        br = new BufferedReader(new FileReader(rdfInstanceFile));
        while ((sCurrentLine = br.readLine()) != null) {
            content.append(sCurrentLine) ;
        }
       splitted = content.toString().split("\\.");
    } catch (IOException e) {
        e.printStackTrace();
    }

答案 2 :(得分:1)

替换

splitted = sCurrentLine.split(".");

splitted = sCurrentLine.split("\\.");

修改

String sCurrentLine = null;
int i = 0;
br = new BufferedReader(new FileReader(rdfInstanceFile));
StringBuilder content = new StringBuilder();
while ((sCurrentLine = br.readLine()) != null) {
    content.append(sCurrentLine);
}
splitted = content.toString().split("\\.");

它会工作。