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

    Default remove variables/line in a text file

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

  2. #2
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Rep Power

    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 (Java Platform SE 6)

    ArrayList (Java Platform SE 6)


    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 03: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
    Rep Power


    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, 11: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, 04:54 PM
  3. [SOLVED] Remove All Line from File
    By Mir in forum New To Java
    Replies: 41
    Last Post: 07-17-2008, 10: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, 03: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