Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Reads data from given text file, instantiates objects
- * (i.e. students and their marks) and inserts them into the teachers notebook.
- *
- * @param file Text file to read from
- * @throws FileNotFoundException if given file cannot be opened for reading
- * @throws IOException on any other I/O failure
- */
- public void load(File file) throws FileNotFoundException, IOException {
- if (file == null) {
- throw new IOException("File is null ");
- }
- if (!file.canRead()) {
- throw new FileNotFoundException("File can't be read");
- }
- BufferedReader br = null;
- try {
- br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
- String line = br.readLine();
- while(line != null) {
- String[] split = line.split(":");
- if (split.length != 2) {
- throw new IOException("Wrong input format ");
- }
- String[] studentSplit = split[0].split(" ");
- if (studentSplit.length != 3) {
- throw new IOException("Wrong input format ");
- }
- int uco = 0;
- try {
- uco = Integer.parseInt(studentSplit[0]);
- }
- catch (NumberFormatException ex) {
- throw new IOException("Wrong input format ");
- }
- if (uco == 0) {
- throw new IOException("Wrong input format ");
- }
- Student student = new Student(uco, studentSplit[1] + " " + studentSplit[2]);
- List<Mark> marks = new ArrayList<Mark>();
- String[] markSplit = split[1].split(" ");
- for (int i = 0; i < markSplit.length; i++) {
- try {
- marks.add(new Mark(Integer.parseInt(markSplit[i])));
- }
- catch (NumberFormatException ex) {
- throw new IOException("Wrong input format ");
- }
- }
- notebook.put(student, marks);
- line = br.readLine();
- }
- } catch (IOException ex) {
- throw new IOException(ex);
- } finally {
- br.close();
- }
- }
Add Comment
Please, Sign In to add comment