Я пытаюсь прочитать входной файл, разбить строки и сохранить их в списке или на карте. Может ли кто-нибудь помочь?
Мне нужно сохранить его в структуре данных, такой как список или карта, и сравнить их с функцией string.equals
, чтобы определить, присутствует ли она (String) в структуре данных или нет.
Я пробовал что-то подобное. Но на выходе я получаю "true" последовательно вместо строк.
public class Main {
public static void main(String[] args) throws IOException {
FileProcessor readInputFile;
readInputFile = new FileProcessor(args[0]);
String line = null;
List<String> result = new ArrayList<>();
while(null != (line = readInputFile.poll())) {
String lines[] = line.split("::|__|,|=|-|\\]|\\[");
System.out.println(result.add(String.valueOf(lines)));
}
}
}
Fileprocessor. java
public final class FileProcessor {
private BufferedReader reader;
/**
* Constructs a FileProcessor that can stream the contents of the provided input file
* line by line.
* @exception InvalidPathException On invalid path string.
* @exception SecurityException On not having necessary read permissions to the input file.
* @exception FileNotFoundException On input file not found.
* @exception IOException On any I/O errors while reading lines from input file.
*/
public FileProcessor(String inputFilePath)
throws InvalidPathException, SecurityException, FileNotFoundException, IOException {
if (!Files.exists(Paths.get(inputFilePath))) {
throw new FileNotFoundException("invalid input file or input file in incorrect location");
}
reader = new BufferedReader(new FileReader(new File(inputFilePath)));
}
/**
* Retrieves and returns the next line in the input file.
*
* @return String The next line read from the input file.
* @exception IOException On error encountered when reading from input file.
*/
public String poll() throws IOException {
return reader.readLine();
}
/**
* Close the buffered reader instance.
*
* @exception IOException On error encountered when closing the buffered reader.
*/
public void close() throws IOException {
reader.close();
}
}
Вход : input.txt
ADD_VIDEO::video1
ADD_VIDEO::video2
METRICS__video1::[VIEWS=1000,LIKES=20,DISLIKES=20]
AD_REQUEST__video1::LEN=8
METRICS__video2::[VIEWS=2000,LIKES=400,DISLIKES=20]
METRICS__video1::[VIEWS=20000,LIKES=1000,DISLIKES=-10]
AD_REQUEST__video2::LEN=39
METRICS__video2::[VIEWS=50,LIKES=-50,DISLIKES=0]
REMOVE_VIDEO::video2
ADD_VIDEO::video3
METRICS__video3::[VIEWS=2000,LIKES=100,DISLIKES=20]
METRICS__video1::[VIEWS=0,LIKES=-1000,DISLIKES=500]
ADD_VIDEO::video4
METRICS__video4::[VIEWS=100,LIKES=5,DISLIKES=0]
REMOVE_VIDEO::video1
AD_REQUEST__video3::LEN=15
REMOVE_VIDEO::video3
REMOVE_VIDEO::video4
Ожидаемый результат:
ADD_VIDEO
video1
ADD_VIDEO
video2
METRICS
video1
VIEWS
1000
LIKES
20
DISLIKES
20
AD_REQUEST
video1
LEN
8
METRICS
video2
VIEWS
2000
LIKES
400
DISLIKES
20
METRICS
video1
VIEWS
20000
LIKES
1000
DISLIKES
10
AD_REQUEST
video2
LEN
39
METRICS
video2
VIEWS
50
LIKES
50
DISLIKES
0
REMOVE_VIDEO
video2
ADD_VIDEO
video3
METRICS
video3
VIEWS
2000
LIKES
100
DISLIKES
20
METRICS
video1
VIEWS
0
LIKES
1000
DISLIKES
500
ADD_VIDEO
video4
METRICS
video4
VIEWS
100
LIKES
5
DISLIKES
0
REMOVE_VIDEO
video1
AD_REQUEST
video3
LEN
15
REMOVE_VIDEO
video3
REMOVE_VIDEO
video4
Получение вывода:
true
true
true
true
.
.
.
true