Thursday, 30 May 2013

Java program to reverse the content of a text file

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.Scanner;

import javax.swing.JFileChooser;

/**
 * This class reads a user chosen file, reads its words
 * reverses the order in which the words are stored in
 * the file i.e. the last word becomes first and the first
 * word becomes last.
 * @author Dixit Bhatta
 * **/
public class revFile {

public static void main(String[] args) throws IOException{
ArrayList<String> rev = new ArrayList<String>();//creating new ArrayList
String absolutePath = null; //to store path of the file
try {
File file = getInputFileNameFromUser();
//store the path of file to modify it later
absolutePath = file.getAbsolutePath();
Scanner filein = new Scanner(file); //open file
while(filein.hasNext()) {
String frame = filein.next();
rev.add(frame);// add the strings i.e. words to the ArrayList.
}//end of while

filein.close();//close the file

} catch (FileNotFoundException e) {//handle exception in case of error
System.err.println("FileNotFoundException: " + e.getMessage());
}

//display the number of words if it the file is not empty
if(!rev.isEmpty()){
int size = rev.size();
System.out.println("The number of words in file is: " + size);
}
Collections.reverse(rev);//reverse the arraylist
//use buffered writer and print writer to write into the file
BufferedWriter write = new BufferedWriter(new FileWriter(absolutePath));
PrintWriter out = new PrintWriter(write);
//writing the strings in the file using an iterator
Iterator<String> iter = rev.iterator();
while (iter.hasNext())
out.write(iter.next() + " ");
out.close();
}

/**
* Lets the user select an input file using a standard file
* selection dialog box. If the user cancels the dialog
* without selecting a file, the return value is null.
*/
static File getInputFileNameFromUser() {
JFileChooser fileDialog = new JFileChooser();
fileDialog.setDialogTitle("Select File for Input");
int option = fileDialog.showOpenDialog(null);
if (option != JFileChooser.APPROVE_OPTION)
return null;
else
return fileDialog.getSelectedFile();
}//end of choosing file


}

©Dixit Bhatta 2013

17 comments:


  1. Thanks for the informative article. This is one of the best resources I have found in quite some time. Nicely written and great info. I really cannot thank you enough for sharing.
    rpa training in bangalore
    best rpa training in bangalore
    RPA training in bangalore
    rpa course in bangalore
    rpa training in chennai
    rpa online training

    ReplyDelete
  2. Whoa! I’m enjoying the template/theme of this website. It’s simple, yet effective. A lot of times it’s very hard to get that “perfect balance” between superb usability and visual appeal. I must say you’ve done a very good job with this.

    AWS certification Training Online Bangalore
    AWS Certification Training Online Pune
    AWS certification Training Online Chennai
    AWS Online Certification and Training

    ReplyDelete
  3. Your good knowledge and kindness in playing with all the pieces were very useful. I don’t know what I would have done if I had not encountered such a step like this.
    python Online training in chennai
    python Online training in bangalore
    python interview question and answers

    ReplyDelete
  4. Thanks For Sharing Your Information Please Keep UpDating Us The Information Shared Is Very Valuable Time Went On Just Reading The Article Python Online Training Devops Online Training
    Aws Online Training DataScience Online Training
    Hadoop Online Training

    ReplyDelete
  5. There's no doubt that the future education will see a lot of advancements. One such example is AWS Training In Hyderabad from Kelly Technologies...

    More: https://www.kellytechno.com/Hyderabad/Course/amazon-web-services-training

    ReplyDelete
  6. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article.
    360DigiTMG business analytics course
    360DigiTMG best data science institute in hyderabad
    360DigiTMG data analytics training

    ReplyDelete
  7. After reading your article I was amazed. I know that you explain it very well. And I hope that other readers will also experience how I feel after reading your article.
    360digiTMG.com artificial intelligence online course

    ReplyDelete
  8. PrimeVideo offers a variety of services, and this includes MyTV. There are many ways in which you can save money by watching TV on your computer instead of paying full price for cable. PrimeTV can help you save hundreds of dollars per year, and they offer many plans so you can find one that fits your lifestyle Visit www .primevideo.com mytv

    ReplyDelete
  9. if ur interested in learning AWS course please visit our website
    AWS Training in Hyderabad

    ReplyDelete
  10. The AWS certification course has become the need of the hour for freshers, IT professionals, or young entrepreneurs. AWS is one of the largest global cloud platforms that aids in hosting and managing company services on the internet. It was conceived in the year 2006 to service the clients in the best way possible by offering customized IT infrastructure. Due to its robustness, Digital Nest added AWS training in Hyderabad under the umbrella of other courses.

    ReplyDelete
  11. Really awesome post, informative and knowledgeable content. Keep sharing more stuff with us. Thank you.
    Data Science Course Training in Hyderabad

    ReplyDelete
  12. Virtual event and extended reality (XR) tech were well represented at the trade show; the latter had a dedicated exhibition named XR Experience, the in-person sequel to the last edition’s SXSW Online XR. event technology Both technologies are likely propelled by the ramped-up investments in the metaverse and complementary technologies. how to write a debriefing report, event scheduling software, event swag bag, define experiential marketing, online registration site, presentation bio, how to have an online vendor event, thank you note for event and catchy invitation phrases

    ReplyDelete
  13. Thanks for sharing this info about Best CA Academy in Hyderabad
    Best CA Academy in Hyderabad

    ReplyDelete
  14. Thank You for Sharing Your Knowledge Please continue to update us Worth the time spent even just reading the article for the information shared.
    Best Colleges For MEC In Hyderabad

    ReplyDelete
  15. Great post! As someone interested in cloud computing, Start a AWS journey.

    ReplyDelete

Was this post helpful? Ask any questions you have, I will try to answer them for you.