input.txt
january
february
march
april
may
june
july
auguest
september
october
november
december
january
february
march
april
may
june
july
auguest
september
october
november
december
package com.usr.io;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class SortTheContentsInTxtFile {
public static void main(String[] args) {
File fin = new File("C:\\input.txt");
File fout = new File("C:\\output.txt");
try {
FileInputStream fis = new FileInputStream(fin);
FileOutputStream fos = new FileOutputStream(fout);
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));
String line = "";
List<String> list = new ArrayList<String>();
while ((line = br.readLine()) != null) {
list.add(line);
}
Collections.sort(list);
for (String temp : list) {
System.out.println(temp);
bw.write(temp);
bw.newLine();
}
br.close();
bw.close();
}
catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
output.txt
april
auguest
december
february
january
july
june
march
may
november
october
september
auguest
december
february
january
july
june
march
may
november
october
september