Hi i want to read a txt file with N lines and the result put it in an Array of strings.
Use a java.util.Scanner
and java.util.List
.
Scanner sc = new Scanner(new File(filename));
List<String> lines = new ArrayList<String>();
while (sc.hasNextLine()) {
lines.add(sc.nextLine());
}
String[] arr = lines.toArray(new String[0]);
FileUtils.readLines(new File("/path/filename"));
From apache commons-io
This will get you a List
of String
. You can use List.toArray()
to convert, but I'd suggest staying with List
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With