Results 1 to 13 of 13
  1. #1
    technoreject is offline Member
    Join Date
    Jun 2011
    Posts
    9
    Rep Power
    0

    Smile Searching Text Files Then Removing (My First Post!)

    I am having a problem with deleting people from the list, to do this I was trying to make an array for every line in the file, then delete the file, and remake it excluding the name that was deleted.
    But what's happening is the file is either not being deleted at all, or being remade with the same lines as before times two and another line named "null".
    (I am trying to make a "friends list", I have adding and reading through the friends list already made, as you add people the text file will go down a line, then it will read through it line-by-line)

    This is something like my code:
    if(s1.startsWith(".dll ")){
    String stn = s1.substring(5);
    try{
    String file = "C:/Friends.txt";
    java.io.FileReader fr = new FileReader( file ) ;
    java.io.BufferedReader reader = new BufferedReader( fr ) ;
    String line = null ;
    int xyz = 1;
    String aString[];
    aString = new String[100];
    while( ( line = reader.readLine() ) != null )
    {
    aString[xyz] = line;
    xyz++;
    }
    File bkup = new File("C:/Friends.txt");
    bkup.delete();
    BufferedWriter out = new BufferedWriter(new FileWriter("C:/Friends.txt", true));
    for(int xxxx = 1; xxxx <= xyz; xxxx++){
    if(aString[xxxx] != stn){
    out.append(aString[xxxx]);
    out.newLine();
    }
    }
    out.close();
    }catch(Exception e){
    }

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default

    Java Code:
    }catch(Exception e){
    }
    Never ever do this. You have no idea if an exception was thrown or not. At the very least do:
    Java Code:
    }catch(Exception e){
        e.printStackTrace();
    }

  3. #3
    technoreject is offline Member
    Join Date
    Jun 2011
    Posts
    9
    Rep Power
    0

    Default

    Alright, thanks. Do you know what my problem is?

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default

    Run your code again and see if there is an exception. If there is copy and paste the exact and full message.

  5. #5
    technoreject is offline Member
    Join Date
    Jun 2011
    Posts
    9
    Rep Power
    0

    Default

    No exceptions.

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default

    A couple of things...

    You cannot delete the file as you already have it open. But you don't even have to worry about deleting it. Just create the FileWriter not in append mode and it will simply overwrite what is already in the file.

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    This is something like my code:
    Consider posting a SSCCE - a small amount of runnable code that illustrates your problem.

    It is unlikely that the SSCCE will be your actual code (unless yours is a very small program!) but it should compile and run. And it should result in the erroneous output.

    (Another small matter is that it would help if you use the "code" tags. You put [code] at the start and [/code] at the end. That way the code will retain its formatting when it is displayed in a web page.)

    ---------

    I haven't really looked at the something-like code, but the following is almost certainly wrong:

    Java Code:
    aString[xxxx] != stn
    Compare String instances (and instances of other things) with the equals() method. This method defines an appropriate way for instances to be compared with one another.

    Java Code:
    if(!aString[ndx].equals(stn)) {

  8. #8
    technoreject is offline Member
    Join Date
    Jun 2011
    Posts
    9
    Rep Power
    0

    Default

    I changed the out.append to out.write, and deleted the deleting of the file, but I don't think that's what you mean't. Can you explain more?

    else if(s1.startsWith(".dll ")){
    String stn = s1.substring(5);
    try{
    String file = "C:/Friends.txt";
    java.io.FileReader fr = new FileReader( file ) ;
    java.io.BufferedReader reader = new BufferedReader( fr ) ;
    String line = null ;
    int xyz = 1;
    String aString[];
    aString = new String[100];
    while( ( line = reader.readLine() ) != null )
    {
    aString[xyz] = line;
    xyz++;
    }
    BufferedWriter out = new BufferedWriter(new FileWriter("C:/Friends.txt", true));
    for(int xxxx = 1; xxxx <= xyz; xxxx++){
    if(aString[xxxx] != stn){
    out.write(aString[xxxx]);
    out.newLine();
    }
    }
    out.close();
    }catch(Exception e){
    e.printStackTrace();
    }
    }

  9. #9
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default

    Java Code:
    new FileWriter("C:/Friends.txt", true));
    The boolean second parameter creates a FileWriter in append mode. Create a FileWriter without the boolean and it will not append.

  10. #10
    technoreject is offline Member
    Join Date
    Jun 2011
    Posts
    9
    Rep Power
    0

    Default

    Hmm it's still not working :S

    String stn = s1.substring(5);
    try{
    String file = "C:/Friends.txt";
    java.io.FileReader fr = new FileReader( file ) ;
    java.io.BufferedReader reader = new BufferedReader( fr ) ;
    String line = null ;
    int xyz = 1;
    String aString[];
    aString = new String[100];
    while( ( line = reader.readLine() ) != null )
    {
    aString[xyz] = line;
    xyz++;
    }
    BufferedWriter out = new BufferedWriter(new FileWriter("C:/Friends.txt"));
    for(int xxxx = 1; xxxx <= xyz; xxxx++){
    if(aString[xxxx] != stn){
    out.append(aString[xxxx]);
    out.newLine();
    }
    }
    out.close();
    }catch(Exception e){
    e.printStackTrace();
    }


    I even tried replacing out.append with out.write, what it's doing now is adding a new line of "null" to the text, not deleting anything.

  11. #11
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default

    Did you read reply #7 by pbrockway?

  12. #12
    technoreject is offline Member
    Join Date
    Jun 2011
    Posts
    9
    Rep Power
    0

    Default

    No, I missed it, but now with

    for(int xxxx = 1; xxxx <= xyz; xxxx++){
    if(!aString[xxxx].equals(stn)) {
    out.append(aString[xxxx]);
    out.newLine();
    }
    }
    out.close();

    it deletes everything in the Friends.txt
    I added in "Jim" and "Bob" then did ".dll Jim" and it deleted everything inside of the txt file.

  13. #13
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default

    Post your new full code between code tags. Place [code] before and [/code] after your code.

Similar Threads

  1. Searching through folder to find text inside all files
    By dazzabiggs in forum New To Java
    Replies: 3
    Last Post: 05-03-2011, 01:20 PM
  2. Replies: 0
    Last Post: 05-01-2011, 07:23 PM
  3. Searching directories for folders and .txt files
    By XDrew574X in forum New To Java
    Replies: 1
    Last Post: 03-29-2011, 09:41 PM
  4. java searching from many files
    By manish250 in forum Advanced Java
    Replies: 0
    Last Post: 12-31-2010, 07:50 AM
  5. Help removing pronouns from text
    By jessie in forum New To Java
    Replies: 11
    Last Post: 11-19-2010, 05:45 AM

Tags for this Thread

Posting Permissions

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