wood burning stoves 2.0*
The moose likes I/O and Streams and the fly likes IO Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "IO" Watch "IO" New topic
Author

IO

Lucky Singh
Ranch Hand

Joined: Jan 19, 2004
Posts: 125
Hi,
Below is a part of some code that uses files.
*Can you explain the first 20 lines of this code?
* What will be the arguments I enter for executing the program?
I typed in java Hello file1.txt file2.txt file3.txt file4.txt
I got an error.
* What will the files contain before and after execution?

Thanks.
Lucky

public class Hello
{
public static void main(String args[] )
{
System.out.println("inside main method");
Hello p = new Hello(args[2],args[3]);
String text;
StringTokenizer line = new StringTokenizer("");
String output ="";
String fileOut= args[1];
String fileIn = args[0];
try
{
FileWriter fw = new FileWriter(fileOut);
BufferedWriter bw = new BufferedWriter(fw);

FileReader fr = new FileReader(fileIn);
BufferedReader br = new BufferedReader(fr);
try
{
System.out.println("inside try block before readLine");
while ((text=br.readLine())!= null)
{
line= new StringTokenizer(text);
try
{
while (line.hasMoreTokens())
{
// read word from line and stem word
String word = new String();
word = line.nextToken();
bw.write(p.stripAffixes(word)+ " ");
}
bw.newLine();
}
catch(Exception e)
{
System.err.println(e);
}
}
}
catch(Exception e)
{
System.err.println("File Error Durring Reading "+e);
System.exit(0);
}
// try to close file, file is not needed again so if can't close don't exit
try
{
fr.close();
}
catch(Exception e)
{
System.err.println("Error Closing File During Reading " + e);
}
try
{
bw.close();
}
catch(Exception e)
{
System.err.println(e);
}

}
catch(Exception e)
{
System.err.println("File Not Found " + args[0] + " exception " + e);
System.exit(1);
}
}//main
} //class
kaizen liu
Greenhorn

Joined: Mar 15, 2005
Posts: 2
first you constructor can't accept two parameter,because you constructor
is default constructor.so "Hello p = new Hello(args[2],args[3]);" will throw out error
Lucky Singh
Ranch Hand

Joined: Jan 19, 2004
Posts: 125
So what if it accepts 2 parematers in the constructor? Infact, the whole code is compiling correctly. I just need to know how to run it and a general meaning of what main is actually doing.
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Originally posted by Lucky Singh:
I typed in java Hello file1.txt file2.txt file3.txt file4.txt
I got an error.
Can you tell us what the error was, or do we have to guess?

It seems to read one file and write to another file. It breaks up each line it reads into "words" and calls Hello.stripAffixes(word), which you haven't included so I can't tell you what that does.

Since main() passes the 3rd and 4th arguments to the Hello constructor, and Hello strips affixes from words, perhaps those files contain the various affixes, prefixes in one file and suffixes in the other.
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10
Originally posted by Lucky Singh:
So what if it accepts 2 parematers in the constructor? In fact, the whole code is compiling correctly. I just need to know how to run it and a general meaning of what main is actually doing.


The code does not compile for me. I cannot run it if I cannot compile it. Therefore I cannot help you at this time.

Did you leave something critical out (like the constructor)?


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
 
jQuery in Action, 2nd edition
 
subject: IO