Results 1 to 5 of 5
  1. #1
    CodeMonkeyAlx is offline Member
    Join Date
    Aug 2012
    Posts
    13
    Rep Power
    0

    Default Puzzled about System Input / Output to a file.

    Hello again, I can never quite grasp how system output works (I know how it works in the typical setting up a scanner and then outputting the results of the scanner on the screen) What puzzles me right now is that I cant seem to grasp how to save information to another file and then bring that file up to view in the terminal(Eclipse's built in terminal). However for the moment I will be happy to just my file working by manually looking for it on the file system. So here is my code:

    Java Code:
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.nio.file.Paths;
    import java.util.Scanner;
    
    
    public class FileIO {
    
    	/**
    	 * @param args
    	 * @throws IOException 
    	 */
    	public static void main(String[] args) throws IOException {
    		// TODO Auto-generated method stub
    		Scanner in = new Scanner(System.in);
    		String hello = in.nextLine();
    		PrintWriter out = new PrintWriter("somefile.txt");
    		out.append(hello);
    	}
    
    }
    Now the issue is that it lets me type in what I am trying to save to the file but its not allowing the file to save so when I press enter and the scanner / program stops I go to look for the file which has been created its is just not saving the info I am typing out. -- How do I fix this or can someone set me on the right track?

  2. #2
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default Re: Puzzled about System Input / Output to a file.

    You aren't closing your writer, so it never knows to flush the text to the document. You store it in your memory, but then leave it there for the garbage collector.

    May I also suggest a few things, such as better variable names. Especially for a SSCCE, if your code was more than 5 lines of relevancy then it would become confusing if all your variables were named hello, hi, whatsup, notbeingcreative, runningoutofideas, ismyprogramfinishedyet, then no one would be able to help you as it would become a giant mess. Instead you should use descriptive variable names. String incomingText makes more sense than String hello.

    Also while dealing with writers and readers, you should be using more specific try and catch blocks. That was you can actually use your thrown errors properly, and it is a good habit to form for later as it will be second nature when you move in to larger programs.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  3. #3
    CodeMonkeyAlx is offline Member
    Join Date
    Aug 2012
    Posts
    13
    Rep Power
    0

    Default Re: Puzzled about System Input / Output to a file.

    Thank you Dark, I will be trying this out when I get back to it! -- Great advice now I just need to put it to practical use. I'll be back with an update ASAP.

  4. #4
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default Re: Puzzled about System Input / Output to a file.

    Quote Originally Posted by CodeMonkeyAlx View Post
    Thank you Dark, I will be trying this out when I get back to it! -- Great advice now I just need to put it to practical use. I'll be back with an update ASAP.
    No problem, there is a really simple version of buffered readers and writers in the thread I posted not much before you. I guess lady luck was on your side. Here is a link, note that my SSCCE is not thread safe but it should show you a little bit about how the general workings of these work.

    Grabbing HTML source code from a URL
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  5. #5
    CodeMonkeyAlx is offline Member
    Join Date
    Aug 2012
    Posts
    13
    Rep Power
    0

    Default Re: Puzzled about System Input / Output to a file.

    Alright I will be sure to look into this! In the mean time here is the fix I came up with -- With a helping hint of course!

    Java Code:
    import java.io.FileNotFoundException;
    import java.io.PrintWriter;
    import java.util.Scanner;
    
    public class customIO {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		Scanner keyboardIn = new Scanner(System.in);
    		// System.out.print("What would you like to name this file: ");
    		// String fileName = keyboardIn.nextLine();
    		System.out.print("What would you like to say in this file: ");
    		String userInput = keyboardIn.nextLine();
    
    		try {
    			PrintWriter writeOut = new PrintWriter(
    					"C:\\Users\\YourUsername\\Desktop\\example.txt");
    			writeOut.append(userInput);
    			writeOut.close();
    		} catch (FileNotFoundException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    
    	}
    
    }
    As mentioned by Dark I went and made a new class after testing around with proper variable descriptors as well as the implemented Try / Catch methods. Finally I properly closed the mentioned printWriter.

    Right now the program does what I want it to It gathers a simple user input string and upon pressing the enter key it saves the string into an external text file which I placed on my desktop. My next goal is to get it to ask if the user wants the program to read the file back to them.

Similar Threads

  1. Confused about File Input and Output Assignment
    By rugger06 in forum New To Java
    Replies: 2
    Last Post: 04-17-2011, 03:10 AM
  2. Input/ output file
    By 3MAD in forum New To Java
    Replies: 1
    Last Post: 04-15-2011, 01:59 PM
  3. File input output
    By edcaru in forum New To Java
    Replies: 5
    Last Post: 12-19-2010, 06:52 PM
  4. Save the output from system.out.println into a file
    By Iskatel in forum Advanced Java
    Replies: 5
    Last Post: 10-05-2010, 03:52 PM
  5. Replies: 8
    Last Post: 05-12-2010, 08:19 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •