Sam
Sam

Reputation: 103

read file and splitting it's content when finding a delimiter

the content of my file is the following :

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" .

i need to split my file at the delimiter '.' and save what we have before it in a string. How can i do that ? i tried the following but it does not work

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();
}

Upvotes: 1

Views: 3857

Answers (3)

Mr. Polywhirl
Mr. Polywhirl

Reputation: 48683

USE the Scanner class. This scenario is perfect for it. All you need to do is specify the '\\.' delimiter.

There is no need to build a string AND THEN split it...

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();
        }
    }
}

Output

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"

Additional Information

useDelimiter

public Scanner useDelimiter(String pattern)

Sets this scanner's delimiting pattern to a pattern constructed from the specified String.

An invocation of this method of the form useDelimiter(pattern) behaves in exactly the same way as the invocation useDelimiter(Pattern.compile(pattern)).

Invoking the reset() method will set the scanner's delimiter to the default.

Parameters:
   pattern - A string specifying a delimiting pattern
Returns:
   this scanner

The Scanner constructor takes six (6) different types of objects: File, InputStream, Path, Readable, ReadableByteChannel, and 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)

Advanced Solution

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();
        }
    }
}

Upvotes: 8

Maria
Maria

Reputation: 1211

First read the contents of the file into a string, split the string and save it in a string array.

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();
    }

Upvotes: 3

ELITE
ELITE

Reputation: 5940

replace

splitted = sCurrentLine.split(".");

with

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

EDIT

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("\\.");

It'll work.

Upvotes: 1

Related Questions