Program to Sort all contents/words of file in java


Let’s say c:/myFile.txt looks like this >
you are learning file io from javaMadeSoEasy

Write a Program to Sort all contents/words of file and write it back in file in java, such that after execution of program file must look like this -

are file from io javaMadeSoEasy learning you



Logic >
1) Read file in String (i.e. in fileDataInString).
2) split string around space and store it in array.
3) Sort the array.
4) Now, write sorted content in file.

Program to Sort all contents/words of file and write it back in file in java >

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
/** JavaMadeSoEasy.com */
public class SortWordsInFile {
   public static void main(String[] args) {
         
          String fileName ="c:/myFile.txt";
         
          String fileDataInString = new String();
         
          try {
                 //1) Read file in String (i.e. in fileDataInString)
                 FileReader fr = new FileReader(fileName);
                 BufferedReader br = new BufferedReader(fr);
                 int data;
                 while((data = br.read())!=-1){
                       fileDataInString = fileDataInString+String.valueOf(((char)data));
                 }
                 br.close();
                
                 //By now, String fileDataInString contains all the data of file
                
                
                 //2) split string around space and store it in array
                 String[] l=fileDataInString.split(" ");
                 //3) Sort the array
                 Arrays.sort(l);
                 System.out.print("Display sorted array > ");
                 //Display sorted array
                 for(String str: l){
                       System.out.print(str+" ");
                 }
                
                 //4) Now, write sorted content in file
                 FileWriter fw = new FileWriter(fileName);
                 BufferedWriter bw = new BufferedWriter(fw);
                 for(String str: l){
                       bw.write(str);
                       bw.write(" ");
                 }
                
                 bw.close();
                 System.out.println("\nSorted content has been written in file");
                
                
          } catch (FileNotFoundException e) {
                 e.printStackTrace();
          } catch (IOException e) {
                 e.printStackTrace();
          }
         
   }
}
/*OUTPUT
Display sorted array > are file from io javaMadeSoEasy learning you
Sorted content has been written in file
*/



RELATED LINKS>

Create File using createNewFile() method in java file IO


Copy a file in java 2 in ways


Program to Write BYTE data type in file using DataOutputStream and Read BYTE from file using DataInputStream - java file IO


Find current directory or current path in java


No comments:

Post a Comment