Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

File I/O

 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Wink, wink, nudge, nudge, say no more, it's a tiny ad:
The trailboss has a kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic