Results 1 to 10 of 10
- 12-08-2010, 12:44 PM #1
Member
- Join Date
- Dec 2010
- Posts
- 17
- Rep Power
- 0
problem of linking text file and java program
1. i have one text list of students with their address, country and contact number (stored in notepad.txt)
2. ive convert every and each of those records in arraylist by passing method
3. User have their choice to ammend any of these records
4. the PROBLEM is after they have ammend this record, i sucessfully update the array list, BUT WHAT SHOULD I DO TO UPDATE ALSO THE TEXTLIST? i dont know how to locate what they have ammend, for example, there are a few students with details recorded, user might only ammend one student record, how should i locate the position of the record?
- 12-08-2010, 12:47 PM #2
Senior Member
- Join Date
- Mar 2010
- Posts
- 952
- Rep Power
- 10
Show us a sample of your text file, and your code for reading it. If you're reading the whole text file into an ArrayList, then probably the simplest thing to do is save a backup copy, and then write out a whole new text file in the same format.
-Gary-
- 12-08-2010, 01:39 PM #3
Member
- Join Date
- Nov 2010
- Posts
- 54
- Rep Power
- 0
There are a number of different ways you can do this:
It seems that you are looking to make small changes to a file without changing the entire file. For this to work each student record will need to be the same number of bytes. If this is the case then you could just store the position of each record as part of the object stored in the array list.
If you don't want to store the position in the file then you will need to read through the file searching for the record you wish to change.
If the records are not all the same length then you will need to re-write the whole file anyway as you can't insert into the middle of a file, you can only overwrite.
The simplest solution would be to simply write out an entirely new file based on the contents of an array list.----Signature ----
Please use [CODE] tags and indent correctly. It really helps when reading your code.
- 12-08-2010, 02:05 PM #4
Member
- Join Date
- Dec 2010
- Posts
- 17
- Rep Power
- 0
oh, but im sorry for not mentioning that my arraylist have been converted into array in the middle of the coding. now it is an Array OF Object, i get what you mean calvin, but how should i transfer this array of object to textfile? can you show me a way? cause google didnt satisfy me :| i tried >>
String strFilePath("StudList.txt")
try
{
FileOutputStream fos = new FileOutputStream(strFilePath);
DataOutputStream data = new DataOutputStream (fos);
for(int c = 0; c < list.length; c++)
{
data.write(list.toString());
}
data.flush();
data.close();
fos.close();
}
catch(FileNotFoundException ex)
{
System.out.println("FileNotFoundException : " + ex);
}
catch(IOException ioe)
{
System.out.println("IOException : " + ioe);
}
- 12-08-2010, 02:07 PM #5
Member
- Join Date
- Dec 2010
- Posts
- 17
- Rep Power
- 0
but it gives me error saying that >> cannot find symbol method write(java.lang.string)
ive even tried writeObject, is that any modification or ammend needed?
- 12-08-2010, 02:40 PM #6
Senior Member
- Join Date
- Mar 2010
- Posts
- 952
- Rep Power
- 10
You have some of the basics (you probably want a BufferedWriter wrapped around a FileWriter rather than a DataOutputStream wrapped around a FileOutputStream), but this isn't going to work:
Java Code:for(int c = 0; c < list.length; c++) { data.write(list.toString()); }
Java Code:for(int c = 0; c < list.length; c++) { data.[COLOR="Blue"]println[/COLOR](list[COLOR="Blue"][c][/COLOR].toString()); }
-Gary-
- 12-08-2010, 02:54 PM #7
Member
- Join Date
- Dec 2010
- Posts
- 17
- Rep Power
- 0
Alvin
012-3099809
1,Jalan Bahagia
Malaysia
Fong Wei Bin
012-5726472
2.Jalan Perdana
Australia
Vincent
016-2856738
3,Jalan Wira
Malaysia
Leslie
010-2746283
4,Jalan Kancil
Malaysia
Jack
012-3628745
5,Jalan pandat
Indonesia
this is what i wrote in my notepad
ive changed it to filewriter and bufferedwriter, even used the println and having the [c] next to list (my mistake:P), but still it says cannot find symbol println, im wondering whats wrong, its hard for me to post my code here because it's very very very long. the code that i post previously is the one which i wrote to write the array of OBJECTS back to the studlist.txt, why why why -.-
- 12-08-2010, 03:46 PM #8
Senior Member
- Join Date
- Mar 2010
- Posts
- 952
- Rep Power
- 10
I steered you a bit wrong, I'm afraid. You want a PrintWriter, not a BufferedWriter.
OK, let's look at a file that I hope is similar enough. The filename will be family.txt.
Java Code:Kim 734-555-1234 Wyandotte Michigan Cherie 678-555-9876 Lawrenceville Georgia Rick 479-555-9827 Rogers Arkansas Gary 213-555-0993 Los Angeles California
Java Code:package org.javaforums.sample.familyexample; public class Person { private String name; private String phone; private String city; private String state; public Person(String name, String phone, String city, String state) { this.name = name; this.phone = phone; this.city = city; this.state = state; } public String getName() { return name; } public String getPhone() { return phone; } public String getCity() { return city; } public String getState() { return state; } public void setName(String name) { this.name = name; } public void setPhone(String phone) { this.phone = phone; } public void setCity(String city) { this.city = city; } public void setState(String state) { this.state = state; } }
Java Code:package org.javaforums.sample.familyexample; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.List; public class Family { private List<Person> members = new ArrayList<Person>(); public void listMembers() { for (Person member : members) { System.out.print("Name: " + member.getName()); System.out.print(" Phone: " + member.getPhone()); System.out.print(" City: " + member.getCity()); System.out.println(" State: " + member.getState()); } } public void readFromFile(String filename) { BufferedReader br = null; members.clear(); try { br = new BufferedReader(new FileReader(filename)); try { String name, phone, city, state; while ((name = br.readLine()) != null) { phone = br.readLine(); city = br.readLine(); state = br.readLine(); members.add(new Person(name, phone, city, state)); } } finally { br.close(); } } catch (IOException e) { // Do something more intelligent than this with the Exception // or don't bother catching it -- just declare it in the method // header. e.printStackTrace(); } } public boolean writeToFile(String filename, String backupname) { boolean saved = false; PrintWriter pw = null; try { File f = new File(filename); f.renameTo(new File(backupname)); pw = new PrintWriter(new FileWriter(filename)); try { for (Person member : members) { pw.println(member.getName()); pw.println(member.getPhone()); pw.println(member.getCity()); pw.println(member.getState()); } saved = true; } finally { pw.close(); } } catch (IOException e) { // Really not much point catching the exception if you're just doing // this. Do something better. e.printStackTrace(); } return saved; } public void run() { readFromFile("family.txt"); listMembers(); System.out.println("Adding Amy..."); members.add(new Person("Amy", "630-555-2345", "Chicago", "Illinois")); listMembers(); if (writeToFile("family.txt", "family.bak")) { System.out.println("File successfully written."); } else { System.err.println("Problem writing file."); } } public static void main(String[] args) { new Family().run(); } }
-Gary-
- 12-08-2010, 04:53 PM #9
Member
- Join Date
- Dec 2010
- Posts
- 17
- Rep Power
- 0
Alvin
012-3099809
1,Jalan Bahagia
Malaysia
Fong Wei Bin
012-5726472
2.Jalan Perdana
Australia
Vincent
016-2856738
3,Jalan Wira
Malaysia
Leslie
010-2746283
4,Jalan Kancil
Malaysia
Jack
012-3628745
5,Jalan pandat
Indonesia
still wont work ): this is my textfile, and the code i posted is the one i try to copy the array of objects to the file
- 12-08-2010, 05:06 PM #10
Senior Member
- Join Date
- Mar 2010
- Posts
- 952
- Rep Power
- 10
Similar Threads
-
Program compiles but wont run to text file...
By marylanddem in forum New To JavaReplies: 2Last Post: 12-05-2010, 05:05 PM -
Write a program that sorts data from a text file and sort them in a file
By danmgz45 in forum New To JavaReplies: 6Last Post: 12-01-2010, 06:31 AM -
Neec a help in this program (printing RGB into text file)
By Hafsa Hosani in forum New To JavaReplies: 2Last Post: 03-24-2009, 01:35 PM -
Problem with reading text from a .txt file
By Gigi in forum New To JavaReplies: 40Last Post: 01-22-2009, 04:22 AM -
<URGENT> problem after linking .jnlp
By bongia in forum New To JavaReplies: 14Last Post: 11-18-2007, 06:57 PM
Bookmarks