Results 1 to 4 of 4

Thread: FileWriter

  1. #1
    javaStooge is offline Senior Member
    Join Date
    Jan 2014
    Posts
    106
    Rep Power
    0

    Default FileWriter

    I'm just trying to append new employee information to a previously created file. When I type yes to add new employee and enter a string, the data does not appear in the file. Why is this?
    Java Code:
    public class Records {
    
        public static void main(String [] args) throws IOException {
            Scanner input = new Scanner(System.in);
            FileWriter fw = new FileWriter("dbs3.java", true);
            BufferedWriter bw = new BufferedWriter(fw);
            PrintWriter pw = new PrintWriter(bw);
            
            System.out.println("NEW EMPLOYEE DATA SHEET");
            System.out.println("Include: First/Last Name, Employee ID, Age, Salary, Hours Worked");
            
            System.out.print("Number of new employees:  ");
            int number = input.nextInt();
            input.nextLine();
            
            for(int i = 1; i <= number; i++) {
                System.out.print("Employee: ");
                String st = input.nextLine();
                pw.println(st);
            }                      
            
            pw.close();
            
            //Read File Contents
            FileReader fr = new FileReader("dbs3.java");
            BufferedReader br = new BufferedReader(fr);
            
            while(br.readLine() != null) {
                System.out.println(br.readLine());
            }
            fr.close();
            
            //Append new data to file from user
            System.out.print("Add a new employee? ('yes' or 'no')  ");
            String addNew = input.next();
            
            if(addNew.equals("yes")) {    
            System.out.println("\nAdd New Employee");
            System.out.print("Number of new employees:  ");
            number = input.nextInt();
            input.nextLine();
            
            for(int i = 1; i <= number; i++) {
                System.out.print("New Employee: ");
                String st = input.nextLine();
                pw.println(st);
            }  
            
            System.out.println();
        } 
            pw.close();
            
        }
    }

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,716
    Rep Power
    18

    Default Re: FileWriter

    Do you get any exceptions when you run the program and answer "yes"?

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default Re: FileWriter

    This appears to be a follow-on from StringTokenizer (Beginning Java forum at JavaRanch)

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: FileWriter

    Before you output the data to a file, output it to the console first. I think you'll be surprised at what you get.

    Java Code:
    for(int i = 1; i <= number; i++) {
      System.out.print("New Employee: ");
      String st = input.nextLine();
    
      System.out.println("THIS IS THE LINE OF DATA: " + st);
    
      pw.println(st);
    }
    Last edited by gimbal2; 03-23-2015 at 09:50 AM.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. FileWriter
    By HeavyTech in forum New To Java
    Replies: 3
    Last Post: 06-07-2014, 10:54 PM
  2. FileWriter issue
    By victorgb in forum New To Java
    Replies: 4
    Last Post: 04-18-2012, 09:31 PM
  3. FileWriter error
    By kthomp271828 in forum Advanced Java
    Replies: 17
    Last Post: 11-29-2011, 06:45 PM
  4. Initializing FileWriter
    By loopsnhoops in forum Advanced Java
    Replies: 1
    Last Post: 05-31-2011, 02:28 AM
  5. FileWriter error
    By bczm8703 in forum JCreator
    Replies: 0
    Last Post: 03-04-2011, 04:10 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
  •