Examples for Reading a file into an arraylist.

1. Using FileReader
2. Using BufferedReader
3. Using Scanner(String and int)
4. Using Files.readAllLines
This commit is contained in:
RanjeetKaur17 2018-07-30 23:48:50 +04:00
parent b7d4a00dac
commit 8eb1b1bb9f
7 changed files with 171 additions and 0 deletions

View File

@ -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<String> generateArrayListFromFile(String filename) throws IOException {
ArrayList<String> result = new ArrayList<>();
try (BufferedReader br = new BufferedReader(new FileReader(filename))) {
while (br.ready()) {
result.add(br.readLine());
}
return result;
}
}
}

View File

@ -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<Character> generateArrayListFromFile(String filename) throws IOException {
ArrayList<Character> 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;
}
}
}

View File

@ -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<String> generateArrayListFromFile(String filename) throws IOException {
List<String> result = Files.readAllLines(Paths.get(filename));
return (ArrayList<String>) result;
}
}

View File

@ -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<Integer> generateArrayListFromFile(String filename) throws IOException {
ArrayList<Integer> result = new ArrayList<>();
try (Scanner s = new Scanner(new FileReader(filename))) {
while (s.hasNext()) {
result.add(s.nextInt());
}
return result;
}
}
}

View File

@ -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<String> generateArrayListFromFile(String filename) throws IOException {
ArrayList<String> result = new ArrayList<>();
try (Scanner s = new Scanner(new FileReader(filename))) {
while (s.hasNext()) {
result.add(s.nextLine());
}
return result;
}
}
}

View File

@ -0,0 +1,2 @@
111
222

View File

@ -0,0 +1,2 @@
Hello
World