Results 1 to 10 of 10
  1. #1
    binweifong is offline Member
    Join Date
    Dec 2010
    Posts
    17
    Rep Power
    0

    Default 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?

  2. #2
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    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-

  3. #3
    couling is offline Member
    Join Date
    Nov 2010
    Posts
    54
    Rep Power
    0

    Default

    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.

  4. #4
    binweifong is offline Member
    Join Date
    Dec 2010
    Posts
    17
    Rep Power
    0

    Default

    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);

    }

  5. #5
    binweifong is offline Member
    Join Date
    Dec 2010
    Posts
    17
    Rep Power
    0

    Default

    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?

  6. #6
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    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());
            }
    You're trying to write out the whole list there. You need to read one item at a time and write it out. This is a step closer:
    Java Code:
            for(int c = 0; c < list.length; c++)
            {
                data.[COLOR="Blue"]println[/COLOR](list[COLOR="Blue"][c][/COLOR].toString());
            }
    But that assumes that you have written your toString() method to output the data in the correct format. It's difficult to be more specific without seeing your class and your text file.

    -Gary-

  7. #7
    binweifong is offline Member
    Join Date
    Dec 2010
    Posts
    17
    Rep Power
    0

    Default

    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 -.-

  8. #8
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    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();
    	}
    
    }
    That's probably giving away too much, but there are plenty of examples like this out on the web already, so it's not like I'm Wikileaks here. Anyway, this felt like a real-world project, and not like homework. You should be able to adapt this to what you need. Let me know if you have questions.

    -Gary-

  9. #9
    binweifong is offline Member
    Join Date
    Dec 2010
    Posts
    17
    Rep Power
    0

    Default

    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

  10. #10
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    Well, I don't know what more I can do for you. The code I posted works. Read it, study it, adapt it.

    -Gary-

Similar Threads

  1. Program compiles but wont run to text file...
    By marylanddem in forum New To Java
    Replies: 2
    Last Post: 12-05-2010, 05:05 PM
  2. Replies: 6
    Last Post: 12-01-2010, 06:31 AM
  3. Neec a help in this program (printing RGB into text file)
    By Hafsa Hosani in forum New To Java
    Replies: 2
    Last Post: 03-24-2009, 01:35 PM
  4. Problem with reading text from a .txt file
    By Gigi in forum New To Java
    Replies: 40
    Last Post: 01-22-2009, 04:22 AM
  5. <URGENT> problem after linking .jnlp
    By bongia in forum New To Java
    Replies: 14
    Last Post: 11-18-2007, 06:57 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •