Results 1 to 12 of 12
  1. #1
    idbirohit is offline Member
    Join Date
    Oct 2010
    Posts
    8
    Rep Power
    0

    Lightbulb write from cmd to a file

    Dear Friends,

    i want to write the content into a file from cmd.im able to create the file and read content from cmd but not able to write the content into the file



    thanks in advance

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,002
    Rep Power
    20

    Default

    So, what's your code then?

  3. #3
    idbirohit is offline Member
    Join Date
    Oct 2010
    Posts
    8
    Rep Power
    0

    Lightbulb can not write to a file from cmd

    Tolls thanx for replying
    Here is the code ...of what im trying to do

    Java Code:
    import java.util.Scanner;
    import java.io.*;
    public class Scann
    {
    public static void main(String [] args) throws Exception
    {
    	System.out.println("Enter 0 to exit:");
        System.out.println("Enter your name:");
        Scanner sc = null;
        while(true)
        {
    		File objFile = new File("Hello.txt");
    		FileWriter objWrite =  new FileWriter(objFile);
    		//Writer objWriter = new BufferedWriter(new FileWriter (objFile));
    		//FileOutputStream os = new FileOutputStream(objFile);
            sc = new Scanner(System.in);
            String name = sc.nextLine();
            if(!name.equals("0"))
            {      
           if(sc.hasNextLine())
            {
     	        objWrite.write(name);
            }
            }
            else
            {
                System.exit(0);
            }
            
        }
    }
    }
    Last edited by Eranga; 11-25-2010 at 05:15 AM. Reason: code tags added

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,002
    Rep Power
    20

    Default

    Java Code:
            File objFile = new File("Hello.txt");
            FileWriter objWrite =  new FileWriter(objFile);
            sc = new Scanner(System.in);
            String name = sc.nextLine();
            if(!name.equals("0"))
            {      
                if(sc.hasNextLine())
                {
     	        objWrite.write(name);
                }
            }
            else
            {
                System.exit(0);
            }
    OK, so in the above code (which is run each time around the loop) you are opening a file, creating a new Scanner, reading a line from the command line (in), then (assuming it's not "0") seeing if there's another line to read in. If so, you write out the first line you read (name).

    Everytime round the loop.

    So first off, do you think you need to open the file each time?
    Second do you think that check on hasNextLine() is correct, after you've already read in a line?
    Third, do you think you need to create a new Scanner each time?

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by Tolls View Post
    OK, so in the above code (which is run each time around the loop) you are opening a file, creating a new Scanner, reading a line from the command line (in), then (assuming it's not "0") seeing if there's another line to read in. If so, you write out the first line you read (name).
    I hope that OP have an idea what's a loop (infinite loop) does. :rolleyes:

  7. #7
    idbirohit is offline Member
    Join Date
    Oct 2010
    Posts
    8
    Rep Power
    0

    Lightbulb how to write the text from cmd to a file

    yeah Tolls I agree with u that don't have to open the file ,and also no need to create the object of the scanner again and again. When Im printing name then it is printing what ever content im entering from the cmd. but not able to write that into the file.so how to write the content into the file. ?
    Last edited by idbirohit; 11-25-2010 at 03:47 PM.

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,002
    Rep Power
    20

    Default

    So, what does the code look like now?

  9. #9
    idbirohit is offline Member
    Join Date
    Oct 2010
    Posts
    8
    Rep Power
    0

    Default

    code is same i have just removed that code from there


    System.out.println("Enter 0 to exit:");
    System.out.println("Enter your name:");
    Scanner sc = null;

    File objFile = new File("Hello.txt");
    FileWriter objWrite = new FileWriter(objFile);

    sc = new Scanner(System.in);
    String name = sc.nextLine();
    if(!name.equals("0"))
    {

    objWrite.write(name);

    }
    else
    {
    System.exit(0);
    }

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,002
    Rep Power
    20

    Default

    Do you actually close the file writer?

  11. #11
    idbirohit is offline Member
    Join Date
    Oct 2010
    Posts
    8
    Rep Power
    0

    Default

    thanks Tolls
    :)
    it is working now. I was not closing the file.

  12. #12
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Please use code tags next time when you are posting code segments. Unformated codes are really hard to read. :)

    And also if you've solve the problem, then please mark the thread solved from tools menu.

Similar Threads

  1. Write to File
    By sehudson in forum AWT / Swing
    Replies: 11
    Last Post: 06-23-2010, 03:30 AM
  2. Replies: 8
    Last Post: 05-12-2010, 07:19 PM
  3. Write to a file in a jar
    By Ms.Ranjan in forum New To Java
    Replies: 4
    Last Post: 06-12-2009, 09:27 PM
  4. Replies: 5
    Last Post: 02-05-2009, 10:28 AM
  5. [SOLVED] File Write in File
    By Eku in forum New To Java
    Replies: 4
    Last Post: 10-27-2008, 04:01 AM

Posting Permissions

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