Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Input and Output files?

  1. #1
    Join Date
    Aug 2012
    Thanked 0 Times in 0 Posts

    Default Input and Output files?

    Hey guys, I'm getting into the whole Input/Output thing in Java and I wrote this example code that I found in my book. I don't know how to use it because I've never tried working with these types of programs before. I've tried writing the name of the file I'm trying to input and I've also tried writing the location of the file I'm trying to input but nothing has seemed to work. Could anyone tell me what I need to write so that it works? (I'm using Windows 7).
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.PrintWriter;
    import java.util.Scanner;
     * This program applies line numbers to a file.
    public class LineNumberer 
    	public static void main(String[] args) throws FileNotFoundException
    		// Prompt the input and the output file names
    		Scanner console = new Scanner(System.in);
    		System.out.println("Input file: ");
    		String inputFileName = console.next();
    		System.out.println("Output file: ");
    		String outputFileName = console.next();
    		// Construct the Scanner and PrintWriter objects for reading and writing.
    		File inputFile = new File(inputFileName);
    		Scanner in = new Scanner(inputFile);
    		PrintWriter out = new PrintWriter(outputFileName);
    		int lineNumber = 1;
    		// Read the input and write the output.
    		while (in.hasNextLine())
    			String line = in.nextLine();
    			out.println("/" + lineNumber + " */" + line);

  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Washington, DC
    My Mood
    Thanked 636 Times in 540 Posts

    Default Re: Input and Output files?

    Have you stepped through this with a debugger or at least added some print statements to figure out what's going on? Your next step is to figure out where this stops working. Can you get the input from the user properly? Do the files exist where you think they do? Does the input file contain text?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Join Date
    Jul 2012
    Thanked 7 Times in 7 Posts

    Default Re: Input and Output files?

    you need to send the user input to a stream to make it readable all you've done at this point is set up the file to be used.... use FileInputStream and pass the user input as the parameter

Similar Threads

  1. Input/Output help if possible
    By rossonomous in forum What's Wrong With My Code?
    Replies: 6
    Last Post: December 13th, 2011, 01:05 PM
  2. Input output file help
    By peteyfresh12 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 2nd, 2011, 07:44 AM
  3. Automate files input
    By JMaste in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: December 3rd, 2010, 08:46 AM
  4. Creating Methods for Input/Output Files
    By PapaGL0VE in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: November 17th, 2010, 10:07 AM
  5. Input/Output file help
    By Plural in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 25th, 2010, 08:34 PM