Read a text file line by line into strings
Solution 1
You should use an ArrayList
.
File file = new File(fileName);
Scanner input = new Scanner(file);
List<String> list = new ArrayList<String>();
while (input.hasNextLine()) {
list.add(input.nextLine());
}
Then you can access to one specific element of your list from its index as next:
System.out.println(list.get(0));
which will give you the first line (ie: Purlplemonkeys)
Solution 2
You can read text file to list:
List<String> lst = Files.readAllLines(Paths.get("C:\\test.txt"));
and then access each line as you want
P.S. Files - java.nio.file.Files
Solution 3
If you use Java 7 or later
List<String> lines = Files.readAllLines(new File(fileName));
for(String line : lines){
// Do whatever you want
System.out.println(line);
}
Solution 4
Sinse JDK 7 is quite easy to read a file into lines:
List<String> lines = Files.readAllLines(new File("text.txt").toPath())
String p1 = lines.get(0);
String p2 = lines.get(1);
Solution 5
You can use apache.commons.io.LineIterator
LineIterator it = FileUtils.lineIterator(file, "UTF-8");
try {
while (it.hasNext()) {
String line = it.nextLine();
// do something with line
}
} finally {
it.close();
}
One can also validate line by overriding boolean isValidLine(String line)
method.
refer doc
Cardinal System
I am a passionate self-taught software developer with 7 years of experience in Java and a very natural affinity for software in general. I love what I do, and I love to learn! I was employed at MinebeaMitsumi as a part-time programmer in 2022 and I am completing my education in 2023.
Updated on July 09, 2022Comments
-
Cardinal System almost 2 years
How do I read the contents of a text file line by line into
String
without using aBufferedReader
?For example, I have a text file that looks like this inside:
Purlplemonkeys greenGorilla
I would want to create two
strings
, then use something like thisFile file = new File(System.getProperty("user.dir") + "\Textfile.txt"); String str = new String(file.nextLine()); String str2 = new String(file.nextLine());
That way it assigns
str
the value of"Purlplemonkeys"
, andstr2
the value of"greenGorilla"
. -
user1231232141214124 over 8 yearsI don't think thats what he was looking for.
line
will be overwritten every time so its kind of pointless to even declare it. Why not justprintln(input.nextLine())
?String line = ...
is kind of redundant. -
Jan over 8 yearsYou don't need to know it's an ArrayList. Coding
List<String> arrayList = new ArrayList<String>();
is cleaner -
Cardinal System over 8 yearsI'm using Eclipse. Next to
arrayList.add(Input.nextLine());
there is an error that saysMultiple markers at this line - Syntax error on token ")", ; expected - implements java.awt.event.ActionListener.actionPerformed - Syntax error on token "(", ; expected
and next toFile file = new File(fileName);
there is an error that saysfileName cannot be resolved to a variable
This also causes errors in other parts of my class. -
Cardinal System over 8 yearsAlso, where do I put the path leading to the txt file?
-
Kevin Mee over 8 yearsYou put the filepath where
fileName
is. Like this:File file = new File("ThisIsYourFilePath.txt");
-
Cardinal System about 6 yearsMy personal favorite approach :)
-
Saikat about 6 yearsThe method readAllLines(Path) in the type Files is not applicable for the arguments (File)