aspose file tools*
The moose likes Beginning Java and the fly likes Read from file and display nonduplicate words in ascending order. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Read from file and display nonduplicate words in ascending order." Watch "Read from file and display nonduplicate words in ascending order." New topic
Author

Read from file and display nonduplicate words in ascending order.

Andrey Petrov
Greenhorn

Joined: Sep 25, 2010
Posts: 3
I am asked to write a program to read from a text file and display nonduplicate words in ascending order. The text file is passed as a command-line argument. Basically, I've managed to write a program to read a predefined String and display all nonduplicates. All is left to do is to read from a file. This is something I am not able to comprehend. Thanks a lot. Here is my code:

Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Hi and welcome to the JavaRanch.

Reading from a file is not that hard. I would recommend reading the basic I/O tutorial from Oracle. It has been updated to Java 7 but most topics are still very useful. If you have questions after that we'll be happy to help you.

By the way. In your for-each loop you can use String instead of Object and remove the toString()


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Joe Coffren
Greenhorn

Joined: Sep 25, 2010
Posts: 2
Here is a helper program I found on the internet that can read a write to a file:

import java.io.*;

public class ReadWriteTextFile {

/**
* Fetch the entire contents of a text file, and return it in a String.
* This style of implementation does not throw Exceptions to the caller.
*
* @param aFile is a file which already exists and can be read.
*/
static public String getContents(File aFile) {
//...checks on aFile are elided
StringBuffer contents = new StringBuffer();

try {
//use buffering, reading one line at a time
//FileReader always assumes default encoding is OK!
BufferedReader input = new BufferedReader(new FileReader(aFile));
try {
String line = null; //not declared within while loop
/*
* readLine is a bit quirky :
* it returns the content of a line MINUS the newline.
* it returns null only for the END of the stream.
* it returns an empty String if two newlines appear in a row.
*/
while (( line = input.readLine()) != null){
contents.append(line);
contents.append(System.getProperty("line.separator"));
}
}
finally {
input.close();
}
}
catch (IOException ex){
ex.printStackTrace();
}

return contents.toString();
}

/**
* Change the contents of text file in its entirety, overwriting any
* existing text.
*
* This style of implementation throws all exceptions to the caller.
*
* @param aFile is an existing file which can be written to.
* @throws IllegalArgumentException if param does not comply.
* @throws FileNotFoundException if the file does not exist.
* @throws IOException if problem encountered during write.
*/
static public void setContents(File aFile, String aContents)
throws FileNotFoundException, IOException {
if (aFile == null) {
throw new IllegalArgumentException("File should not be null.");
}
if (!aFile.exists()) {
throw new FileNotFoundException ("File does not exist: " + aFile);
}
if (!aFile.isFile()) {
throw new IllegalArgumentException("Should not be a directory: " + aFile);
}
if (!aFile.canWrite()) {
throw new IllegalArgumentException("File cannot be written: " + aFile);
}

//use buffering
Writer output = new BufferedWriter(new FileWriter(aFile));
try {
//FileWriter always assumes default encoding is OK!
output.write( aContents );
}
finally {
output.close();
}
}

/** Simple test harness. */
public static void main (String[] args) throws IOException {
File testFile = new File("C:\\Users\\misterfuff\\Documents\\myfile.txt");
System.out.println("Original file contents: " + getContents(testFile));
setContents(testFile, "The content of this file has been overwritten...");
System.out.println("New file contents: " + getContents(testFile));
}
}
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Hi Joe and also to you welcome to the JavaRanch.

Thank you for your efforts and posting them but it's usually not a good idea to provide a complete answer. If you provide an abstract answer or some pointers then the topic starter will learn much more. In short DontBeACodeMill. And please UseCodeTags when posting code. It makes it much easier to read.
Joe Coffren
Greenhorn

Joined: Sep 25, 2010
Posts: 2
sorry understand.
Andrey Petrov
Greenhorn

Joined: Sep 25, 2010
Posts: 3
I need someone to explain the whole concept of read in a file and, say, split the words and store in an ArrayList, or some other way. I just don't know the file input part of this. If someone could give me a small example on how to use it in my situation, I'd really appreciate it.
amit punekar
Ranch Hand

Joined: May 14, 2004
Posts: 516
Hi,
Referring to the program that reads a file, you can do something like this
1) Read the text line by line using readLine() method which is already shown in the program
2) The String returned needs to be parsed.
3) Once you parse the string you would get every word in that line of text, start reading these tokens
4) While reading the tokens you can add them to Set directly as you have already done in your program which ensures you have unique strings.

Hope this helps,
Amit
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Read from file and display nonduplicate words in ascending order.