1

I am setting up a rank system where each member has a username and a rank. The program reads the username and rank from a text file and assigns it to the user. One username and rank per line, such as:

user1 1
user2 2
user3 3

I have set up a program to add usernames and ranks to the text file, however I cannot seem to figure out how to delete a specific user from the list, such as if I wanted to only delete user 2 and his/her rank and leave the other two, however it is important that afterwards there isn't a blank line left behind.

Just for reference here is the code for how I write it to the file in the first place:

             try {
                 BufferedWriter out = new BufferedWriter(new FileWriter("stafflist.txt", true));
                 for (int i = 0; i < 1; i++) {
                 out.newLine();
                 out.write(target.getUsername() + " " + target.getRights());
                 }
             out.close();
             SerializableFilesManager.savePlayer(target);
                    if (loggedIn) {
                    target.getPackets().sendGameMessage(modString + Utils.formatPlayerNameForDisplay(member.getUsername()) + "!", true);}
             member.getPackets().sendGameMessage(successString + Utils.formatMemberNameForDisplay(target.getUsername()) + " to a Moderator.",true);
             loggedIn = false;
                } catch (IOException e) {
                System.out.println("GiveMod - Can't find stafflist.txt");
                }
             return true;
  • maybe this helps [Find a line in a file and remove it](http://stackoverflow.com/questions/1377279/find-a-line-in-a-file-and-remove-it) – Alexander Campos Nov 13 '15 at 20:19
  • @Alexander Campos - Thank you for that, I had already found that and it does work for my situation, however my program does not like the deleting and renaming of the temporary list. I need something that will modify and save my current list rather than creating a new one. – Dakota Spencer Nov 13 '15 at 20:26
  • To be honest, Databases are much better at this kind of thing than flat files are. – Powerlord Nov 13 '15 at 20:58

3 Answers3

1

You cannot delete data from the middle of a file (without leaving nulls). You need to rewrite at least what underneath it. A Simple solution would be loading everything in memory, remove that line and dump the collection again.

An alternative solution would be to:

  1. Open a FileChannel from a RandomAccessFile
  2. read the file line by line and keep the file-pointer of the line head. fileChannel.position();file.readLine(); load what comes after that into a collection. truncate the file from that position file.setLength(linePosition); and then dump the collection at the end of the file.

If your data doesn't fit in memory then you can use a temp file instead of a collection. Create a temp-file File.createTempFile(...), read the remaining data line by line and write to temp, truncate the original file ,read temp line by and write to original.

OR, guess what, use a database.

Sleiman Jneidi
  • 22,907
  • 14
  • 56
  • 77
0

There seems to be an issue in your for loop. It is looping between 0 and 1, so I think the output you posted is incorrect. Anyway, if you want to only print out certain lines you can filter it as follows:

 for (int i = 0; i < 1; i++) {
        if(!target.getUsername().equals("user2")){
             out.newLine();
             out.write(target.getUsername() + " " + target.getRights());
        }
 }
aengus
  • 605
  • 6
  • 13
  • I'm sorry, The code I posted was just for reference, it is the method I use to write the data to the text file. I am in need of a way to delete the text afterwards. So just pretend that code isnt there and I have a text file with user1 1 user2 2 etc, and I need to delete just one of them, a specific one, while also removing the blank line afterwards. – Dakota Spencer Nov 13 '15 at 20:23
  • In that case I would refer to Alexander Campos link to a similar problem – aengus Nov 13 '15 at 20:25
  • In addition, what do you do about the rank for the remaining users? For example, if you remove user1, does user2's rank still stay 2 and user3's rank stays at 3, so that no user now has rank 1? – FredK Nov 13 '15 at 20:27
  • If you are just deleting a line from the text file, then it wont affect the other lines, so this shouldn't be an issue. – aengus Nov 13 '15 at 20:29
  • The point I was making is that deleting a user WILL (or ought to) affect the rank for all of the other users. If not, and you later want to find "Who has rank #1" you won't find anyone even though there are still users in the list. – FredK Nov 13 '15 at 20:32
0

Read the file into some Collection, remove desired users and rewrite the file using the modified Collection.

Teemu
  • 438
  • 3
  • 11