This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
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");
"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
posted
0
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
posted
0
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://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.