Results 1 to 12 of 12
  1. #1
    teekei is offline Member
    Join Date
    May 2011
    Posts
    39
    Rep Power
    0

    Default Error with Writing Files, don't know why??

    Java Code:
    /**
     * @(#)CreateData.java
     *
     * CreateData application
     *
     * @author
     * @version 1.00 2011/7/17
     */
     import java.io.File;
     import java.io.FileNotFoundException;
     import java.util.IllegalFormatException;
     import java.util.FormatterClosedException;
     import java.io.*;
    
    public class CreateData
    {
    
        public static void main(String[] args) throws FileNotFoundException, IllegalFormatException, FormatterClosedException
        {
          try
          {
    	      AccountRecord[] accounts = new AccountRecord[4];
    	      // create account records
    	      accounts[ 0 ] = new AccountRecord( 100, "Alan", "Jones", 348.17 );
    	      accounts[ 1 ] = new AccountRecord( 300, "Mary", "Smith", 27.19 );
    	      accounts[ 2 ] = new AccountRecord( 500, "Sam", "Sharp", 0.00 );
    	      accounts[ 3 ] = new AccountRecord( 700, "Suzy", "Green", -14.22 );
    
          	  accounts.write("Accounts.txt");
          }
          catch(FileNotFoundException fileNotFound)
          {
          	System.err.println("File not found.");
          }
          catch(SecurityException securityException)
          {
          	System.err.println("Security error detected.");
          }
          catch(IllegalFormatException illegalFormat)
          {
          	System.err.println("Illegal format.");
          }
          catch(FormatterClosedException formatterClosed)
          {
          	System.err.println("Error writing to file.");
          }
        }
    }

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Neither do we unless you tell us what errors you get. Copy and paste the full and exact message.

  3. #3
    teekei is offline Member
    Join Date
    May 2011
    Posts
    39
    Rep Power
    0

    Default

    I'm sorry.
    Java Code:
    symbol  : method write(java.lang.String)
    location: class AccountRecord[]
          	  accounts.write("Accounts.txt");
          	          ^
    1 error
    
    Process completed.

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Did you read the error message?
    Did you try to understand the error message?
    What class is accounts?
    Does that class have a write method?
    Which class does have a write method?

  5. #5
    teekei is offline Member
    Join Date
    May 2011
    Posts
    39
    Rep Power
    0

    Default

    No the class does not have the write method, I want to know if I can use the write method just in main rather than in my class because I've been looking up how to write to a file and I see that the write method works perfectly fine for them so I'm just curious as to why it's not working for me, do I have to import a package in order for it to compile, I'm stuck right now.

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    It works perfectly fine in code examples because they would have created an output stream. Where in your code have you done that?

  7. #7
    amanrv is offline Member
    Join Date
    Jul 2011
    Posts
    12
    Rep Power
    0

    Default

    my friend, you first have to declare the file, then use bufferedwriter, then use the write statement, somewhat like this

    Java Code:
    File file=new File("Accounts.txt");
    BufferedWriter in=new BufferedWriter(new FileWriter(file, true));
    in.write(accounts)   //if its a string

  8. #8
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,655
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by amanrv View Post
    my friend, you first have to declare the file, then use bufferedwriter, then use the write statement, somewhat like this

    Java Code:
    File file=new File("Accounts.txt");
    BufferedWriter in=new BufferedWriter(new FileWriter(file, true));
    in.write(accounts)   //if its a string
    I find it confusing at best to name a writer 'in'.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    teekei is offline Member
    Join Date
    May 2011
    Posts
    39
    Rep Power
    0

    Default

    If you don't mind me asking, do I have to import a specific package in order to use the "BufferedWriter"?

  10. #10
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,655
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by teekei View Post
    If you don't mind me asking, do I have to import a specific package in order to use the "BufferedWriter"?
    What does the API documentation for that class say about it?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    teekei is offline Member
    Join Date
    May 2011
    Posts
    39
    Rep Power
    0

    Default

    So I imported all the necessary packages and modified to code to accommodate the changes but I still am getting a "cannot find symbol method write(AccountRecord[])"

    Here's my code

    Java Code:
     import java.io.File;
     import java.io.FileNotFoundException;
     import java.util.IllegalFormatException;
     import java.util.FormatterClosedException;
     import java.io.BufferedWriter;
     import java.io.FileWriter;
    public class CreateData
    {
    
        public static void main(String[] args) throws FileNotFoundException, IllegalFormatException, FormatterClosedException
        {
          try
          {
    	      AccountRecord[] accounts = new AccountRecord[4];
    	      // create account records
    	      accounts[ 0 ] = new AccountRecord( 100, "Alan", "Jones", 348.17 );
    	      accounts[ 1 ] = new AccountRecord( 300, "Mary", "Smith", 27.19 );
    	      accounts[ 2 ] = new AccountRecord( 500, "Sam", "Sharp", 0.00 );
    	      accounts[ 3 ] = new AccountRecord( 700, "Suzy", "Green", -14.22 );
    
          	  File file=new File("Accounts.txt");
    		  BufferedWriter in=new BufferedWriter(new FileWriter(file, true));
    		  in.write(accounts);
          }
          catch(FileNotFoundException fileNotFound)
          {
          	System.err.println("File not found.");
          }
          catch(SecurityException securityException)
          {
          	System.err.println("Security error detected.");
          }
          catch(IllegalFormatException illegalFormat)
          {
          	System.err.println("Illegal format.");
          }
          catch(FormatterClosedException formatterClosed)
          {
          	System.err.println("Error writing to file.");
          }
        }
    }
    Here's my error message
    Java Code:
    F:\Lab11\CreateData.java:30: cannot find symbol
    symbol  : method write(AccountRecord[])
    location: class java.io.BufferedWriter
    		  in.write(accounts);
    		    ^
    1 error

  12. #12
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Ah yes, the old "copy code and dump into my project without understanding it" trick.

    Go to the Java API for the BufferedWriter class and find a method that takes an Object array as the parameter.

Similar Threads

  1. writing to files from arrays
    By xkillswitchx14 in forum New To Java
    Replies: 3
    Last Post: 04-28-2011, 11:11 PM
  2. writing the files for particular time
    By damuammu in forum Advanced Java
    Replies: 3
    Last Post: 03-15-2011, 06:32 PM
  3. Reading / Writing files
    By Learning Java in forum New To Java
    Replies: 6
    Last Post: 08-08-2010, 09:21 PM
  4. Applets writing to files
    By bugger in forum New To Java
    Replies: 2
    Last Post: 11-20-2007, 08:45 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
  •