Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

String to File,

Options
  • 02-12-2012 2:41am
    #1
    Closed Accounts Posts: 2,663 ✭✭✭


    I have being Studying OOP for the last number of weeks, Have being Working with ArrayLists.

    But With my OOP exam coming up, i will need to write a program out using Arrays,

    So my Question is i have a 2 programs that i have one is ArrayList and the other is Arrays how can i get a String into a Text File using Arrays, I can get it working with ArraysLists

    This is my String
        public String toString()
        {
            String result ="\n";
            result += "Name: " + Name + "\n";
            result += "Address: " + Address + "\n"; 
            result += "Phone: " + phone + "\n"; 
            result += "Start Date: " + StartDate + "\n";
            result += "End Date: " + EndDate + "\n";
            result += "Position: " + position + "\n";
            result += "Salary: " + Salary + "\n";
            result += "Sex: " + Sex + "\n";
            result += "Personal Public Service Number: " + PPSNumber + "\n";
            return result;
        }
    


    public void holidays()
        {
            
        double amount;
        int basic = 21; 
        
        for (int i = 0; i < StaffList.length; i++) 
        {
            
             try
             {
                      
                    // Create file 
                FileWriter out = new FileWriter("EmployeeRecords.txt");
                    
                
                    
                    System.out.println(StaffList[i]);
                    
                    amount = StaffList[i].holidays();
                    amount += basic;    
                
                    out.write();
                    out.write ("Total Holidays: " + amount);
                    out.write("\n");
                    out.write ("-----------------------------------");
                       
                    //Close the output stream
                    out.close();
                    
            }//End Try Write 
             
             
                     //Catch exception if any
                     catch (Exception e) 
                     {
                     //Out Put Error Message if Any Exception is Found. 
                    System.err.println("Error: Can Not Create File!  " + e.getMessage()); 
                    } //end Catch 
                  
             
                 //Reading a file
                  File file = new File("EmployeeRecords.txt");
                  int ch;
                  StringBuffer strContent = new StringBuffer("");
                  FileInputStream fin = null;
                  
                  try
                  {
                      fin = new FileInputStream(file);
                      while ((ch = fin.read()) != -1)
                             strContent.append((char) ch);
                      fin.close();
                  } // Try Read 
                  
                  catch (Exception e) 
                  {
                      System.out.println(e);
                  } //End Catch 
                  
                  //Out Print the File Content to Console. 
                  System.out.println(strContent.toString());
                  
    
    
        }// End If Read / Write Method
        
    } //Ends Main Method 
    


    I can get it Working using Array List with the code below..
    i tried replacing a few parts of this code to get it Working for an Array.
        for(int i = 0; i < CIT.MemberList.size(); i++)
            {
                EmployeeMember temp = (EmployeeMember)CIT.MemberList.get(i);
                
                
            
                 try
                 {
                          
                        // Create file 
                        FileWriter out1 = new FileWriter("EmployeeRecords.txt");
                        
                        /*
                         * Takes the EmployeeMember Temp 
                         * and Writes the Object of the ArrayList 
                         * To a Text file using the Following Code. 
                         */
                        
                        out1.write(temp.toString());
                        
                        //Close the output stream
                        out1.close();
    


Comments

  • Registered Users Posts: 1,717 ✭✭✭Raging_Ninja


    Declare the string array. Add the text you want to each position in the string array (perhaps using a split() method if you have it delimited with a certain character). Using a for loop, iterate through each position in the string array writing to the file.


  • Closed Accounts Posts: 2,663 ✭✭✭Cork24


    i have tried


    out.write(StaffList.toString());

    but does not seem to work


  • Registered Users Posts: 8,324 ✭✭✭chrislad


    I'm in a similar situation to you.

    I would try the .get(int x) method to return the object located at whatever index

    eg

    for (int i = 0; i< arrayList.length; i++)
    {

    out.write(arrayList.get(i));

    }

    I'm not 100% on this but I think that would probably work a bit better as the toString method is inherited by the ArrayList class. This is assuming that the objects in your arraylist are strings.


  • Closed Accounts Posts: 2,663 ✭✭✭Cork24


    got it working,


    out.write(StaffList.toString());

    forgotten about the the i when I had it in the for loop,


Advertisement