Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

File I/O

 
Lucky Singh
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Following is a piece of some code which uses files.
My Java source and class files are in the bin directory.
C:/jdk/bin

I was able to compile the file successfully. How do I run the file? What should I type in at the prompt?

public static void main(String args[] )
{

Paice p = new Paice(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
{
while ((text=br.readLine())!= null)
{
line= new StringTokenizer(text);
try
{
...

Thanks,
Lucky
[ April 01, 2005: Message edited by: Lucky Singh ]
 
Marilyn de Queiroz
Sheriff
Posts: 9059
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the name of your class?

You should type something like:

C:\jdk\bin>java MyClass filename1.txt filename2.txt firstArgYouNeedForPaiceConstructor secondArgYouNeedForPaiceConstructor
[ April 01, 2005: Message edited by: Marilyn de Queiroz ]
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The args array that is passed into main contains the arguments that are given from the command-line. Marilyn gives a good example of how this works. Typically each command-line argument is separated by white space. When you type "java MyClass" at the command-line, anything that follows is put into the args array. So if you do "java MyClass file1.txt file2.txt", then args[0] holds the string "file1.txt" and args[1] holds the string "file2.txt". That's really all there is to it.

Hopefully between Marilyn's example and my more general explanation, you can figure out what to do here. Let us know if you have any more questions.

Layne
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This looks an awful lot like this message, only the names have been changed to protect the guilty.
 
Marilyn de Queiroz
Sheriff
Posts: 9059
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[ April 01, 2005: Message edited by: Lucky Singh ]
Posts: 104 | Registered: Jan 2004


After 104 posts, Lucky, I'm surprised that you don't use code tags.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic