File Writing

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sumoseth
    New Member
    • Jan 2007
    • 2

    #1

    File Writing

    hi, im relatively new at programming.
    Im using bluej.

    i have manged to write the following data to a file.

    0213 - Sumair Seth
    9801 - John Smith
    7373 - Jason Porter
    1283 - Allison Fairth

    I need to read only the 4 digit string(0213 and 9801) and write it into an array. I need to do this everytime i write a number and a name(in the above format) to the file. Could someone help me with this?... i really need it urgently.
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by sumoseth
    hi, im relatively new at programming.
    Im using bluej.

    i have manged to write the following data to a file.

    0213 - Sumair Seth
    9801 - John Smith
    7373 - Jason Porter
    1283 - Allison Fairth

    I need to read only the 4 digit string(0213 and 9801) and write it into an array. I need to do this everytime i write a number and a name(in the above format) to the file. Could someone help me with this?... i really need it urgently.
    Have you written any code to read the file. Post the code you have written so far.

    Comment

    • sumoseth
      New Member
      • Jan 2007
      • 2

      #3
      yes this is the code to enter the details into the file:

      Code:
       import java.io.*;
      public class RegisterEmployees
      {
      static    InputStreamReader in=new InputStreamReader(System.in);
      static     BufferedReader br = new BufferedReader(in);
          public static void enterEmployeeNames() throws IOException
          {
              File EmpDatabase = new File("C:\\Documents and Settings\\All Users\\Desktop");
              
              BufferedWriter out = new BufferedWriter(new FileWriter("C:\\Documents and Settings\\All Users\\Desktop\\Database.txt", true));
              System.out.println("Kindly Enter Number of Employees whose records need to be Entered");
              int numberofUpdates = Integer.parseInt(br.readLine());
              for(int i=1;i<=numberofUpdates ; i++)
              {
      
              System.out.println("Enter four digit Employee Code");
             String EmpCode = br.readLine();
             
             if(EmpCode.length()<4)
             {
              System.out.println("Code to small please begin again and enter a four digit code");
          enterEmployeeNames();    
          }
          if(EmpCode.length()>4)
             {
              System.out.println("Code to large please begin again and enter a four digit code");
          enterEmployeeNames();    
          }   
          out.write(EmpCode);
             
              out.write(" - ");
              System.out.println("Enter Employee Name");
              out.write(br.readLine());
                      System.out.println("Thank You, Employee Database has been updated");
          }
      out.write("\n");
          out.close();     }
      
          }


      basically the number will always be 4 digits, to make reading it easier. no i havent written any code to read the file as of now

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by sumoseth
        yes this is the code to enter the details into the file:

        Code:
        import java.io.*;
        public class RegisterEmployees
        {
        static InputStreamReader in=new InputStreamReader(System.in);
        static BufferedReader br = new BufferedReader(in);
        public static void enterEmployeeNames() throws IOException
        {
        File EmpDatabase = new File("C:\\Documents and Settings\\All Users\\Desktop");
         
        BufferedWriter out = new BufferedWriter(new FileWriter("C:\\Documents and Settings\\All Users\\Desktop\\Database.txt", true));
        System.out.println("Kindly Enter Number of Employees whose records need to be Entered");
        int numberofUpdates = Integer.parseInt(br.readLine());
        for(int i=1;i<=numberofUpdates ; i++)
        {
         
        System.out.println("Enter four digit Employee Code");
        String EmpCode = br.readLine();
         
        if(EmpCode.length()<4)
        {
        System.out.println("Code to small please begin again and enter a four digit code");
        enterEmployeeNames(); 
        }
        if(EmpCode.length()>4)
        {
        System.out.println("Code to large please begin again and enter a four digit code");
        enterEmployeeNames(); 
        } 
        out.write(EmpCode);
         
        out.write(" - ");
        System.out.println("Enter Employee Name");
        out.write(br.readLine());
        System.out.println("Thank You, Employee Database has been updated");
        }
        out.write("\n");
        out.close(); }
         
        }


        basically the number will always be 4 digits, to make reading it easier. no i havent written any code to read the file as of now
        Write the part for reading the file post that. You will need to use
        Code:
         
        BufferedReader br = new BufferedReader(new FileReader(String nameOfFile));
        Then you read a line using
        Code:
        br.readLine();

        Comment

        • horace1
          Recognized Expert Top Contributor
          • Nov 2006
          • 1510

          #5
          Originally posted by sumoseth

          I need to read only the 4 digit string(0213 and 9801) and write it into an array. I need to do this everytime i write a number and a name(in the above format) to the file. Could someone help me with this?... i really need it urgently.
          Are you talking about another program which is reading this data or do you need to create an array within this program to hold the employer code?
          In the latter case when you write the employer code
          Code:
              out.write(EmpCode);
          you could store the value in a vector, e.g.
          Code:
              Vector<Integer> v = new Vector<Integer>();
             ....
              out.write(EmpCode);
              Integer i=new Integer(EmpCode);
              v.add(i);

          Comment

          Working...