Results 1 to 12 of 12
- 07-18-2011, 07:02 AM #1
Member
- Join Date
- May 2011
- Posts
- 39
- Rep Power
- 0
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."); } } }
- 07-18-2011, 07:06 AM #2
Neither do we unless you tell us what errors you get. Copy and paste the full and exact message.
- 07-18-2011, 07:08 AM #3
Member
- Join Date
- May 2011
- Posts
- 39
- Rep Power
- 0
I'm sorry.
Java Code:symbol : method write(java.lang.String) location: class AccountRecord[] accounts.write("Accounts.txt"); ^ 1 error Process completed.
- 07-18-2011, 07:12 AM #4
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?
- 07-18-2011, 07:25 AM #5
Member
- Join Date
- May 2011
- Posts
- 39
- Rep Power
- 0
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.
- 07-18-2011, 07:32 AM #6
It works perfectly fine in code examples because they would have created an output stream. Where in your code have you done that?
- 07-18-2011, 08:56 AM #7
Member
- Join Date
- Jul 2011
- Posts
- 12
- Rep Power
- 0
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
- 07-18-2011, 09:30 AM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
- 07-18-2011, 05:13 PM #9
Member
- Join Date
- May 2011
- Posts
- 39
- Rep Power
- 0
If you don't mind me asking, do I have to import a specific package in order to use the "BufferedWriter"?
- 07-18-2011, 05:15 PM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
- 07-18-2011, 10:34 PM #11
Member
- Join Date
- May 2011
- Posts
- 39
- Rep Power
- 0
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
Here's my error messageJava 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."); } } }
Java Code:F:\Lab11\CreateData.java:30: cannot find symbol symbol : method write(AccountRecord[]) location: class java.io.BufferedWriter in.write(accounts); ^ 1 error
- 07-19-2011, 12:26 AM #12
Similar Threads
-
writing to files from arrays
By xkillswitchx14 in forum New To JavaReplies: 3Last Post: 04-28-2011, 11:11 PM -
writing the files for particular time
By damuammu in forum Advanced JavaReplies: 3Last Post: 03-15-2011, 06:32 PM -
Reading / Writing files
By Learning Java in forum New To JavaReplies: 6Last Post: 08-08-2010, 09:21 PM -
Applets writing to files
By bugger in forum New To JavaReplies: 2Last Post: 11-20-2007, 08:45 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks