简体   繁体   English

从Java中的文本文件删除特定行

[英]Deleting a specific line from a text file in Java

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. 我已经建立了一个程序来向文本文件添加用户名和等级,但是我似乎无法弄清楚如何从列表中删除特定用户,例如,如果我只想删除用户2及其等级,然后离开其他两个,但是重要的是,之后不要留空行。

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;

You cannot delete data from the middle of a file (without leaving nulls ). 您不能从文件中间删除数据(不保留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. 一个简单的解决方案是将所有内容加载到内存中,删除该行,然后再次转储该collection

An alternative solution would be to: 一种替代解决方案是:

  1. Open a FileChannel from a RandomAccessFile RandomAccessFile打开FileChannel
  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); 从该位置文件截断文件file.setLength(linePosition); and then dump the collection at the end of the file. 然后将collection转储到文件末尾。

If your data doesn't fit in memory then you can use a temp file instead of a collection. 如果您的数据不适合存储在内存中,则可以使用temp file而不是集合。 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. 创建一个temp-file File.createTempFile(...) ,逐行读取剩余数据并写入temp,截断原始文件,逐行读取temp并写入原始文件。

OR, guess what, use a database. 或者,猜猜怎么用数据库。

There seems to be an issue in your for loop. 您的for循环中似乎有一个问题。 It is looping between 0 and 1, so I think the output you posted is incorrect. 它在0和1之间循环,所以我认为您发布的输出不正确。 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());
        }
 }

将文件读入某个Collection中,删除所需的用户,然后使用修改后的Collection重写该文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM