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 8 of 8

Thread: I can't give input a file by console , is there any wrong in my code ??

  1. #1
    Junior Member
    Join Date
    Sep 2014
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Unhappy I can't give input a file by console , is there any wrong in my code ??

    this is my code...in my code i cant give input form my console...it takes input but it doesn't save in a target file...Anybody can help me ?

    My code is :-
    package filetester;
     
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.util.Scanner;
     
    public class tester {
     
            public static void main(String[] args) {
                            File inpfile =  new File("C:/Users/abid/Desktop/1.txt");
                            File outfile = new File("C:/Users/abid/Desktop/2.txt"); // i want a output file name 2
                            try {
                                    FileReader readfile = new FileReader(inpfile);
                                    try {
                                            FileWriter writefile = new FileWriter(outfile);
                                            Scanner myScanner = new Scanner(System.in);
                                            while (myScanner.hasNext()) {
                                                    String temp = myScanner.nextLine();
                                                    //System.out.println(temp);
                                                    writefile.write(temp +"\n"); // i want to write named 2 file through console , but it doesnt work ..
                                            }
                                            readfile.close();
                                            writefile.close();
     
     
                                    } catch (FileNotFoundException e) {
                                            // TODO Auto-generated catch block
                                            e.printStackTrace();
                                            System.out.println("no file found");
                                    }
                                    } catch (IOException e) {
                                            // TODO Auto-generated catch block
                                            e.printStackTrace();
                                    }              
     
            }
     
    }
    Last edited by devils; September 29th, 2014 at 02:43 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: I can't give input a file by console , is there any wrong in my code ??

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE GOES HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following 2 Users Say Thank You to Norm For This Useful Post:

    devils (September 28th, 2014), GregBrannon (September 28th, 2014)

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

    Default Re: I can't give input a file by console , is there any wrong in my code ??

    How is the program supposed to work? Some prompts to guide the user or comments in the code that explain what is to happen would be helpful. If you're getting error messages, post them in their entirety.

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

    devils (September 29th, 2014)

  6. #4
    Junior Member
    Join Date
    Sep 2014
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: I can't give input a file by console , is there any wrong in my code ??

    @GregBrannon
    I am a new learner..I just want to write something in a file through console..But i can't..I write this code , it takes input from me but doesn't save in file..How can i do that ?

  7. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: I can't give input a file by console , is there any wrong in my code ??

    Is anything printed when the println() statement is not a comment?
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: I can't give input a file by console , is there any wrong in my code ??

    Find a "write to file" tutorial tailored for your IDE or editor (if possible) that is intended to be run from the console and follow it exactly. Once you work out the details of opening, reading, and writing to files, you should be able to repeat the lessons learned and expand your program to do more things.

  9. #7
    Junior Member
    Join Date
    Sep 2014
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: I can't give input a file by console , is there any wrong in my code ??

    @Norm

    i just comment out that line,,if i include that line in the code then when i give input in my console it print back to me after giving my input..

  10. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: I can't give input a file by console , is there any wrong in my code ??

    If the println() executes for all the lines input to the program, then the write() method is also being executed and the file is written to.
    The code works for me (after I removed the input file references)
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 2
    Last Post: February 28th, 2014, 09:39 AM
  2. can someone give a code example of deleting data to a text file?
    By Nathanlll in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 15th, 2013, 06:40 AM
  3. getting input from the user and give him some chances
    By felixb in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 7th, 2013, 12:06 PM
  4. how to input image file in java code.
    By jerim in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 27th, 2013, 10:52 AM
  5. Replies: 0
    Last Post: October 29th, 2012, 12:17 AM