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.


>> REGISTER NOW TO START POSTING


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

Results 1 to 7 of 7

Thread: create new file, ask user to write in it, save finally print content of file

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    26
    My Mood
    Inspired
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default create new file, ask user to write in it, save finally print content of file

    1. creates a file.
    2. ask user to write into that file
    3. save the file
    4. print content of file.

    is my current exercise
    so far i have gotten the code to create a file

    what should i use to ask user to write into that file and save?

    package assignment7;
     
    import java.io.*;
     
    public class Exercise2 
    {
    	public static void main  ( String [  ]  args )  
        {  
         String filePath="newfile.txt"; 
         File newFile = new File ( filePath ) ; 
         try 
          {  
           if ( newFile.createNewFile (  )  )  
            {  
             System.out.println ( "New File Created." ) ; 
            }  
           else 
            {  
             System.out.println ( "File already exists." ) ; 
            }  
          }  
         catch  ( IOException ioe )  
          {  
           System.out.println ( "IOException = " + ioe.getMessage (  )  ) ; 
          }  
        }
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: create new file, ask user to write in it, save finally print content of file

    Absent any specific instructions (are you sure?), ask the user for his/her name and write that to the file.

  3. The Following User Says Thank You to GregBrannon For This Useful Post:

    ATB (March 15th, 2014)

  4. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: create new file, ask user to write in it, save finally print content of file

    what should i use to ask user to write into that file and save?
    Since its a console program, use println() to ask the questions and the methods of the Scanner class to read the user's response. Do a Search on the forum for plenty of examples of usages of Scanner class methods.
    If you don't understand my answer, don't ignore it, ask a question.

  5. The Following User Says Thank You to Norm For This Useful Post:

    ATB (March 15th, 2014)

  6. #4
    Junior Member
    Join Date
    Mar 2014
    Posts
    26
    My Mood
    Inspired
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: create new file, ask user to write in it, save finally print content of file

    I have attempted a new code and got a little further.
    I do still have to figure out how to save to the created file.

    package assignment7;
     
    import java.io.*;
    import java.util.*;
    public class Exercise2 
    {
    	public static void main  ( String [  ]  args )  throws IOException
        {  
    		Scanner scan = new Scanner(System.in);
     
     
    		File f;
    		 f=new File("myfile.txt");
     
    		 if(!f.exists());
    		  {
    			  f.createNewFile();
    			  System.out.println("New file \"myfile.txt\" has been created to the current directory.");
    		  }
        	System.out.print("Enter your age as an integer: ");
        	int age = scan.nextInt();
     
        	System.out.println("Your age is " + age);
        }
    }

  7. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: create new file, ask user to write in it, save finally print content of file

    To write in a file you need to use a class and its methods. For example the PrintWriter class class has methods for writing a String and other data types to a file.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #6
    Junior Member
    Join Date
    Mar 2014
    Posts
    26
    My Mood
    Inspired
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: create new file, ask user to write in it, save finally print content of file

    so how do i save to the file? cause the current code already askes the user for age and prints it out?

  9. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: create new file, ask user to write in it, save finally print content of file

    Create an instance of PrintWriter and use its methods to write data to the file. When finished writing to the file, close it.

    --- Update ---

    Also posted at: create new file, ask user to write in it, save finally print content of file
    If you don't understand my answer, don't ignore it, ask a question.

  10. The Following User Says Thank You to Norm For This Useful Post:

    ATB (April 6th, 2014)

Similar Threads

  1. Replies: 7
    Last Post: March 10th, 2014, 04:28 PM
  2. How to save user input in a file.
    By Ansaf in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 20th, 2013, 03:04 PM
  3. Replies: 1
    Last Post: July 26th, 2013, 03:25 AM
  4. Need help to save Canvas content to a file
    By piulitza in forum AWT / Java Swing
    Replies: 3
    Last Post: December 27th, 2011, 11:20 AM
  5. How to save statistics of a game in a binary file instead of txt file?
    By FretDancer69 in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: June 19th, 2009, 05:05 AM