RuneHive-Game
Loading...
Searching...
No Matches
TextFileParser.java
Go to the documentation of this file.
1package com.runehive.util.parser;
2
3import java.io.BufferedReader;
4import java.io.FileNotFoundException;
5import java.io.FileReader;
6import java.io.IOException;
7
8/**
9 * A simple parser designed for text files.
10 *
11 * @author Seven
12 */
13public abstract class TextFileParser extends GenericParser {
14
15 /**
16 * Creates a new {@code TextFileParser}.
17 *
18 * @param path
19 * The path of the file to parse.
20 */
21 public TextFileParser(String path) {
22 this(path, true);
23 }
24
25 /**
26 * Creates a new {@code TextFileParser}.
27 *
28 * @param path
29 * The path of the file to parse.
30 *
31 * @param log
32 * The flag that denotes to log messages.
33 */
34 public TextFileParser(String path, boolean log) {
35 super(path, ".txt", log);
36 }
37
38 /**
39 * The method called when the file is being parsed.
40 *
41 * @param reader
42 * The underlying parser.
43 */
44 public abstract void parse(BufferedReader reader) throws IOException;
45
46 @Override
47 public void deserialize() {
48 try(BufferedReader reader = new BufferedReader(new FileReader(path.toFile()))) {
49 while(reader.readLine() != null) {
50 parse(reader);
51 index++;
52 }
53 reader.close();
54 } catch (FileNotFoundException e) {
55 e.printStackTrace();
56 } catch (IOException e) {
57 e.printStackTrace();
58 }
59 }
60
61}
62
int index
The index of the current line being parsed.
GenericParser(String path, String extension, boolean log)
Creates a new GenericParser.
final Path path
The path of the file to parse.
abstract void parse(BufferedReader reader)
The method called when the file is being parsed.
void deserialize()
The method that deserializes the file information.
TextFileParser(String path)
Creates a new TextFileParser.
TextFileParser(String path, boolean log)
Creates a new TextFileParser.