How to read Text File In Java?

Sanjay Singh
2 min readJan 8, 2021

Using BufferedReader object to read the input text file.\

Step 1 : Create BufferedReader object to read the input text file.
BufferedReader reader = new BufferedReader(new FileReader(“Pass The File Location Here”));
Step 2 : Create one ArrayList object which will hold all the lines or records of input text file.
ArrayList<String> lines = new ArrayList<String>();
Step 3 : Read all the lines of input text file one by one and add the into ArrayList lines.

Reading all the lines of input file one by one and
String currentLine = br.readLine();
while (currentLine != null){
lines.add(currentLine);
currentLine = br.readLine();}
Step 4 : Sort the ArrayList lines using Collections.sort() method.
Collections.sort(lines);

Read txt file and sort accordenly

56
Suresh
Mahesh
Abhi
81
Vikas
Bhavani
Nalini
62
62
output
[56, 62, 81, Abhi, Bhavani, Mahesh, Nalini, Suresh, Vikas]

package com.text;
public class ApplicationText {
public static void main(String[] args) throws IOException {
ArrayList<String> lines = new ArrayList<String>();
/*READ TEXT FILE IN JAVA
1- Read text file
2-and short the element*/
BufferedReader reader = new BufferedReader(new FileReader(“C:\\Users\\sanjsingh\\Desktop\\new.txt”));
String currentLine = reader.readLine();
while (currentLine != null) {
lines.add(currentLine);
currentLine = reader.readLine();}
Collections.sort(lines);
System.out.println(lines);}}

How To Sort A Text File Containing Multiple Columns In Java?
input.txt :
Suresh 56
Mahesh 89
Shyam 81
Vikas 92
Shloka 84
Nalini 62
Abhi 71
Bhavani 68
output
1-sort on the basic of name
2-sort on the basic of value

Solution
if you are reding multiple Columns then you need to bundle all Columns in single object.
Like above example I have 2 field so bundle into single object

Step 1- for bundle

public class Student{
private String name;
private int marks;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getMarks() {
return marks;
}
public void setMarks(int marks) {
this.marks = marks;
}
public Student(String name, int marks) {
super();
this.name = name;
this.marks = marks;
}
@Override
public String toString() {
return “Student [name=” + name + “, marks=” + marks + “]”;
}
Step2 for sorting
public class CustomComprator implements Comparator<Student> {

@Override
public int compare(Student o1, Student o2) {

return o1.getName().compareTo(o2.getName());
}

}
step 3- Reading
public class SortTextFileContainingMultipleColumns {
public static void main(String[] args) throws IOException {
List<Student> studentRecords = new ArrayList<Student>();
try {BufferedReader reader = new BufferedReader(new FileReader(“C:\\Users\\sanjsingh\\Desktop\\2new.txt”));
String currentLine = reader.readLine();
while (currentLine != null) {
String[] studetls = currentLine.split(“ “);
String name = studetls[0];
int marks = Integer.valueOf(studetls[1]);
studentRecords.add(new Student(name, marks));
currentLine = reader.readLine();
}Collections.sort(studentRecords, new CustomComprator());
System.out.println(studentRecords);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}}}

o/p List sort on the basic of name…
[Student [name=Abhi, marks=71], Student [name=Bhavani, marks=68], Student [name=Mahesh, marks=89], Student [name=Nalini, marks=62], Student [name=Shloka, marks=84], Student [name=Shyam, marks=81], Student [name=Suresh, marks=56], Student [name=Vikas, marks=92]]

--

--

Sanjay Singh

Java||Spring-Boot|| Micro services Application developer|| INDIA