Results 1 to 3 of 3
  1. #1
    ddatta8 is offline Member
    Join Date
    Dec 2008
    Posts
    27
    Rep Power
    0

    Default remove variables/line in a text file

    Hi,
    I have the following method where I search for a match and when I locate the variables, I want to delete them from the text file. How do I do it? Please help. My method is given below:
    Java Code:
    public void dropCourse(String studentID, String dropCourseName){
    		try { 
                StreamTokenizer sr = new StreamTokenizer(new FileReader("coursesEnrolled.txt"));
          	    
          	    for (i=0; i<recCount; i++){
          	    	sr.nextToken(); studID[i] = sr.sval;
          	    	sr.nextToken(); courseEnrolled[i] = sr.sval;
          	        if ((studentID.equals(studID[i])) && (dropCourseName.equals(courseEnrolled[i]))){
          	        	//remove studID[i] & courseEnrolled[i]
          	        } 
                } 
           	} 
            catch (IOException e) { 
               	System.out.println("Uh oh, got an IOException error!");
               	e.printStackTrace();
            }
    	}

  2. #2
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default Array or ArrayList ?

    Are you using Arrays or ArrayLists (I'm assuming you're using Arrays)? If your using Arrays, then you can't remove an element from the array, you can only overwrite it (say like with zero). If you use Arraylist, you can remove it (with, of course, the remove() method).

    Array:
    Array (Java Platform SE 6)

    ArrayList:
    ArrayList (Java Platform SE 6)

    Luck,
    CJSL

    EDIT: Sorry... I misunderstood... I thought that the OP wanted to remove the elements from the array, which maybe he/she still wants to do if they're going to remove the data from a text file.
    Last edited by CJSLMAN; 01-04-2009 at 02:05 PM. Reason: ops... I misunderstood...
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  3. #3
    Gudradain is offline Member
    Join Date
    Nov 2008
    Posts
    44
    Rep Power
    0

    Default

    An easy way to do it would be to get the String equivalent of your .txt file

    Then you create your method to search the word in the String and you remove it.

    Then you reload the same file with the new string replacing the old content of the file.

Similar Threads

  1. append variables to a text file
    By ddatta8 in forum New To Java
    Replies: 2
    Last Post: 01-02-2009, 10:17 AM
  2. How to remove 2 last lines in a text file?
    By Marius in forum New To Java
    Replies: 2
    Last Post: 11-30-2008, 03:54 PM
  3. [SOLVED] Remove All Line from File
    By Mir in forum New To Java
    Replies: 41
    Last Post: 07-17-2008, 09:44 AM
  4. Remove duplicate lines from a text file
    By Dirt.Diver in forum New To Java
    Replies: 15
    Last Post: 06-25-2008, 02:08 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
  •