Results 1 to 7 of 7
  1. #1
    Glypsen is offline Member
    Join Date
    Feb 2010
    Posts
    5
    Rep Power
    0

    Default Write to a txt. file, via user input? (java with netbeans)

    :confused: I am working on a read/write program that accesses information from said file..

    Reading from it works.
    I have been searching for a way to write to a file using using the input from a user.

    I have ran into mostly code that is set to auto fill with predefined entries. Any help would be much appreciated.

  2. #2
    Glypsen is offline Member
    Join Date
    Feb 2010
    Posts
    5
    Rep Power
    0

    Default

    Any tid bit of code, example, point in the right direction would be awesome..
    I know scanner can read from files but i found nothing on it being able to write to files.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  4. #4
    Glypsen is offline Member
    Join Date
    Feb 2010
    Posts
    5
    Rep Power
    0

    Default

    Havent had much luck thus far, If there is not already at least 1 entry in the txt file it crashes.
    Java Code:
    import java.util.Scanner;
    import java.io.*;
    import java.io.BufferedWriter;
    
    public class ProgramFiles
    {
    
        
        public static void main(String[] args) throws FileNotFoundException, IOException
        {
            File theFile = new File("thisfile.txt");
            Scanner MyFileStalker = new Scanner(theFile);
            String Whatevers = MyFileStalker.next();
            String b = "";
            Scanner Input = new Scanner (System.in);
            BufferedWriter bw;
            bw = new BufferedWriter(new FileWriter("thisfile.txt",true));
    
            do
            {
                System.out.println("enter a string: ");
                b = Input.next();
                bw.write(b);
                
                System.out.println("value of b"+b);
            }
            while (!(MyFileStalker.next().equals("exit")));
            
            
            do
            {
                System.out.println(MyFileStalker.next()); //using this to track   entries atm.
            }
            while ((MyFileStalker.hasNext()!= false) && (!(MyFileStalker.next().equals("exit"))));
            
    
        }
    
    }

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    It looks like you're trying to read out of a file and write to it at the same time. I'd be surprised if the OS will let you do that. What exactly are you trying to have this code do?
    Last edited by Fubarable; 03-01-2010 at 03:49 AM.

  6. #6
    Glypsen is offline Member
    Join Date
    Feb 2010
    Posts
    5
    Rep Power
    0

    Default

    For start a loop that asks the user to input string that writes to the txt file.
    until they type "exit" to finish that portion of data entry.

    The reading portion was an attempt at tracking the file entries....
    And will end up being used to spit information back at the user.

    Names numbers. So i set up a reference for Searches for those entries.

  7. #7
    Glypsen is offline Member
    Join Date
    Feb 2010
    Posts
    5
    Rep Power
    0

Similar Threads

  1. User Input
    By brmcdani in forum New To Java
    Replies: 2
    Last Post: 02-05-2010, 01:59 AM
  2. How do I validate user input from forms with Java?
    By rickywh in forum New To Java
    Replies: 2
    Last Post: 01-30-2010, 06:49 AM
  3. reading from input file and then write on it
    By sara12345 in forum New To Java
    Replies: 9
    Last Post: 01-19-2010, 11:41 AM
  4. how to get input from User
    By Alvaro in forum New To Java
    Replies: 7
    Last Post: 01-15-2010, 11:02 PM
  5. cant take input from user
    By new_1 in forum New To Java
    Replies: 6
    Last Post: 12-25-2007, 07:38 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
  •