wood burning stoves*
The moose likes Beginning Java and the fly likes Prompt for file name Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Prompt for file name " Watch "Prompt for file name " New topic
Author

Prompt for file name

Luigi Melanson
Greenhorn

Joined: Nov 25, 2006
Posts: 4
I am trying to alter this code to have it prompt for a file name instead of predefining it in the code ("filein.txt")
Any help would be appreciated,
The code is below:

import java.io.*;
class InputExcercise
{
public static void main(String args[])throws Exception
{
FileReader fr = new FileReader("filein.txt");
FileWriter fw = new FileWriter("fileout.txt");

int ch;
ch = fr.read();
while(ch != -1)
{
if((char)ch >='A' && (char)ch <='Z')ch = ch +('a'-'A');
fw.write((char)ch);
ch = fr.read();
}
fr.close();
fw.close();
}
}
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Luigi Melanson:
I am trying to alter this code to have it prompt for a file name...

Take a look at java.util.Scanner.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Nicholas Jordan
Ranch Hand

Joined: Sep 17, 2006
Posts: 1282
Originally posted by Luigi Melanson:
.... prompt for a file name instead of predefining "filein.txt" in the code
Any help would be appreciated,

I Think I understand what you are wrestling with here. The cite Marc Weber provides is certianly something you need to keep on your list, but the problem is asking the user for a filename then reading that in.

I do not know how to read the standard input, but you have the option of having the user type the name of the file when they start the program:
at the command line when they start the program. This is one way to do it:

This now has a File object that literally uses the name that was typed in. It is not opened or anything, look through http://java.sun.com/j2se/1.3/docs/guide/io/io-maj.html very carefully to get your bearings before trying to do anything with the file. The Scanner Marc mentioned will more than likely read() what the user types in, but you will have to read up on it to use it. To ask the user for a file name, you do this:I prefer my programs not to sit and wait on the user to type in something, it better be there when they invoke the program.

Either way, it looks to me what you are doing is struggling with what to do with the stuff the user types in, as a string problem rather than the problem you describe. I know it well, hung for a waste of time seemingly getting nothing done.

This is called "Re-inventing the wheel." in computer work.


[ December 24, 2006: Message edited by: Nicholas Jordan ]

"The differential equations that describe dynamic interactions of power generators are similar to that of the gravitational interplay among celestial bodies, which is chaotic in nature."
Stephen Foy
Ranch Hand

Joined: Oct 17, 2005
Posts: 143
Use the scanner class to read in a string as a filename.



Make sure you catch the "FileNotFoundException"...


Stephen Foy - Microsoft Application Development Consultant
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Prompt for file name