Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

String to File,

  • 02-12-2012 02: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, Registered Users 2 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, Registered Users 2 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