From c9e901abbea965dd94113e2dc110652830da65f6 Mon Sep 17 00:00:00 2001 From: RanjeetKaur17 Date: Mon, 30 Jul 2018 23:48:50 +0400 Subject: [PATCH] Examples for Reading a file into an arraylist. 1. Using FileReader 2. Using BufferedReader 3. Using Scanner(String and int) 4. Using Files.readAllLines --- .../fileparser/BufferedReaderExample.java | 34 +++++++++++++++++ .../fileparser/FileReaderExample.java | 37 +++++++++++++++++++ .../fileparser/FilesReadLineExample.java | 28 ++++++++++++++ .../fileparser/ScannerIntExample.java | 34 +++++++++++++++++ .../fileparser/ScannerStringExample.java | 34 +++++++++++++++++ file-parser/src/resources/num.txt | 2 + file-parser/src/resources/txt.txt | 2 + 7 files changed, 171 insertions(+) create mode 100644 file-parser/src/com/baeldung/fileparser/BufferedReaderExample.java create mode 100644 file-parser/src/com/baeldung/fileparser/FileReaderExample.java create mode 100644 file-parser/src/com/baeldung/fileparser/FilesReadLineExample.java create mode 100644 file-parser/src/com/baeldung/fileparser/ScannerIntExample.java create mode 100644 file-parser/src/com/baeldung/fileparser/ScannerStringExample.java create mode 100644 file-parser/src/resources/num.txt create mode 100644 file-parser/src/resources/txt.txt diff --git a/file-parser/src/com/baeldung/fileparser/BufferedReaderExample.java b/file-parser/src/com/baeldung/fileparser/BufferedReaderExample.java new file mode 100644 index 0000000000..a139ed80ab --- /dev/null +++ b/file-parser/src/com/baeldung/fileparser/BufferedReaderExample.java @@ -0,0 +1,34 @@ +package com.baeldung.fileparser; + +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; +import java.util.ArrayList; + +public class BufferedReaderExample { + + private static final String FILENAME = "src/resources/txt.txt"; + + public static void main(String[] args) { + try { + System.out.println(generateArrayListFromFile(FILENAME)); + } catch (IOException e) { + e.printStackTrace(); + } + } + + private static ArrayList generateArrayListFromFile(String filename) throws IOException { + + ArrayList result = new ArrayList<>(); + + try (BufferedReader br = new BufferedReader(new FileReader(filename))) { + + while (br.ready()) { + result.add(br.readLine()); + } + return result; + } + + } + +} diff --git a/file-parser/src/com/baeldung/fileparser/FileReaderExample.java b/file-parser/src/com/baeldung/fileparser/FileReaderExample.java new file mode 100644 index 0000000000..1ab98973c7 --- /dev/null +++ b/file-parser/src/com/baeldung/fileparser/FileReaderExample.java @@ -0,0 +1,37 @@ +package com.baeldung.fileparser; + +import java.io.FileReader; +import java.io.IOException; +import java.util.ArrayList; + +public class FileReaderExample { + + private static final String FILENAME = "src/resources/txt.txt"; + + public static void main(String[] args) { + try { + System.out.println(generateArrayListFromFile(FILENAME)); + } catch (IOException e) { + e.printStackTrace(); + } + } + + private static ArrayList generateArrayListFromFile(String filename) throws IOException { + + ArrayList result = new ArrayList<>(); + + try (FileReader f = new FileReader(filename)) { + + while (f.ready()) { + char c = (char) f.read(); + + if (c != ' ' && c != '\t' && c != '\n') { + result.add(c); + } + } + return result; + } + + } + +} diff --git a/file-parser/src/com/baeldung/fileparser/FilesReadLineExample.java b/file-parser/src/com/baeldung/fileparser/FilesReadLineExample.java new file mode 100644 index 0000000000..7f94525c22 --- /dev/null +++ b/file-parser/src/com/baeldung/fileparser/FilesReadLineExample.java @@ -0,0 +1,28 @@ +package com.baeldung.fileparser; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.List; + +public class FilesReadLineExample { + + private static final String FILENAME = "src/resources/txt.txt"; + + public static void main(String[] args) { + try { + System.out.println(generateArrayListFromFile(FILENAME)); + } catch (IOException e) { + e.printStackTrace(); + } + } + + private static ArrayList generateArrayListFromFile(String filename) throws IOException { + + List result = Files.readAllLines(Paths.get(filename)); + + return (ArrayList) result; + } + +} diff --git a/file-parser/src/com/baeldung/fileparser/ScannerIntExample.java b/file-parser/src/com/baeldung/fileparser/ScannerIntExample.java new file mode 100644 index 0000000000..aab7455c30 --- /dev/null +++ b/file-parser/src/com/baeldung/fileparser/ScannerIntExample.java @@ -0,0 +1,34 @@ +package com.baeldung.fileparser; + +import java.io.FileReader; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Scanner; + +public class ScannerIntExample { + + private static final String FILENAME = "src/resources/num.txt"; + + public static void main(String[] args) { + try { + System.out.println(generateArrayListFromFile(FILENAME)); + } catch (IOException e) { + e.printStackTrace(); + } + } + + private static ArrayList generateArrayListFromFile(String filename) throws IOException { + + ArrayList result = new ArrayList<>(); + + try (Scanner s = new Scanner(new FileReader(filename))) { + + while (s.hasNext()) { + result.add(s.nextInt()); + } + return result; + } + + } + +} diff --git a/file-parser/src/com/baeldung/fileparser/ScannerStringExample.java b/file-parser/src/com/baeldung/fileparser/ScannerStringExample.java new file mode 100644 index 0000000000..fc18b53609 --- /dev/null +++ b/file-parser/src/com/baeldung/fileparser/ScannerStringExample.java @@ -0,0 +1,34 @@ +package com.baeldung.fileparser; + +import java.io.FileReader; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Scanner; + +public class ScannerStringExample { + + private static final String FILENAME = "src/resources/txt.txt"; + + public static void main(String[] args) { + try { + System.out.println(generateArrayListFromFile(FILENAME)); + } catch (IOException e) { + e.printStackTrace(); + } + } + + private static ArrayList generateArrayListFromFile(String filename) throws IOException { + + ArrayList result = new ArrayList<>(); + + try (Scanner s = new Scanner(new FileReader(filename))) { + + while (s.hasNext()) { + result.add(s.nextLine()); + } + return result; + } + + } + +} diff --git a/file-parser/src/resources/num.txt b/file-parser/src/resources/num.txt new file mode 100644 index 0000000000..7787faa3c1 --- /dev/null +++ b/file-parser/src/resources/num.txt @@ -0,0 +1,2 @@ +111 +222 \ No newline at end of file diff --git a/file-parser/src/resources/txt.txt b/file-parser/src/resources/txt.txt new file mode 100644 index 0000000000..75cb50aafa --- /dev/null +++ b/file-parser/src/resources/txt.txt @@ -0,0 +1,2 @@ +Hello +World \ No newline at end of file