Содержание файла:
19846 Шорты пляжные синие 159.00 12
198478 Шорты пляжные черные с рисунко173.00 17
19847983Куртка для сноубордистов, разм10173.991234
С параметром 198478 выводит:
198478 Шорты пляжные черные с рисунко173.00 17package com.javarush.task.task18.task1822;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
/*
Поиск данных внутри файла
*/
public class Solution {
public static void main(String[] args) {
String fileName = consoleReader();
System.out.println(getInformation(fileName, args[0]));
}
private static String getInformation(String fileName, String id) {
String information = "Not found";
try (BufferedReader fileReader = new BufferedReader(new FileReader(fileName))) {
while (fileReader.ready()) {
String currentLine = String.format("%-50s",fileReader.readLine());
String foundId = currentLine.substring(0, 8).trim();
if (id.equals(foundId)) {
information = currentLine;
break;
}
}
} catch (IOException e) {
e.getStackTrace();
}
return information;
}
private static String consoleReader() {
BufferedReader consoleReader = new BufferedReader(new InputStreamReader(System.in));
String string = null;
try {
string = consoleReader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return string;
}
}