简体   繁体   English

如何从txt删除行?

[英]how can I delete line from txt?

I mean , I want to delete line from my text on android. 我的意思是,我想从android上的文本中删除一行。 How can I delete? 我怎么删除? I do not want to read one txt and create another with removing line. 我不想读取一个txt并使用删除行创建另一个。 I want to delete line from my existing txt. 我想删除现有txt中的行。 thanks. 谢谢。

This is a pretty tricky problem, despite it looking a trivial one. 这是一个非常棘手的问题,尽管看起来很简单。 In case of variable lines length, maybe your only option is reading the file line by line to indentify offset and length of the target line. 如果行长度可变,您可能唯一的选择是逐行读取文件以识别目标行的offsetlength Then copying the following portion of the file starting at offset , eventually truncating the file lenght to its original size minus the the target line's length. 然后从offset开始复制文件的以下部分,最终将文件长度截断为原始大小减去目标行的长度。 I use a RandomAccessFile to access the internal pointer and also read by lines. 我使用RandomAccessFile来访问内部指针,也可以通过行读取。

This program requires two command line arguments: 该程序需要两个命令行参数:

  • args[0] is the filename args[0]是文件名
  • args[1] is the target line number (1-based: first line is #1) args[1]是目标行号(从1开始:第一行是#1)
public class RemoveLine {
    public static void main(String[] args) throws IOException {
        // Use a random access file
        RandomAccessFile file = new RandomAccessFile(args[0], "rw");
        int counter = 0, target = Integer.parseInt(args[1]);
        long offset = 0, length = 0;

        while (file.readLine() != null) {
            counter++;
            if (counter == target)
                break; // Found target line's offset
            offset = file.getFilePointer();
        }

        length = file.getFilePointer() - offset;

        if (target > counter) {
            file.close();
            throw new IOException("No such line!");
        }

        byte[] buffer = new byte[4096];
        int read = -1; // will store byte reads from file.read()
        while ((read = file.read(buffer)) > -1){
            file.seek(file.getFilePointer() - read - length);
            file.write(buffer, 0, read);
            file.seek(file.getFilePointer() + length);
        }
        file.setLength(file.length() - length); //truncate by length
        file.close();
    }
}

Here is the full code , including a JUnit test case. 这是完整的代码 ,包括一个JUnit测试用例。 The advantage of using this solution is that it should be fully scalable with respect to memory, ie since it uses a fixed buffer, its memory requirements are predictable and don't change according to the input file size. 使用此解决方案的优点是它应该在内存方面完全可扩展,即因为它使用固定缓冲区,其内存要求是可预测的,并且不会根据输入文件大小而改变。

尝试将文件存储到String缓冲区中替换您要替换的内容,然后完全替换该文件的内容。

You can delete a line by copying the rest of the data in the file, then flushing the file and finally writing the copied data.Thr following code searches for the string to be deleted and skips the code to copy in a stringBuider . 您可以通过复制文件中的其余数据来删除一行,然后刷新文件,最后写入复制的数据。然后,下面的代码搜索要删除的字符串,并跳过要在stringBuider中复制的代码。 The contents of stringBuilder are copied to the same file after flushing 刷新后,stringBuilder的内容将复制到同一文件中

         try {
                InputStream inputStream = openFileInput(FILENAME);
                FileOutputStream fos = openFileOutput("temp", Context.MODE_APPEND);

                if (inputStream != null) {
                    InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
                    BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
                    String receiveString = "";
                    String deleteString = "<string you want to del>";
                    StringBuilder stringBuilder = new StringBuilder();

                    while ((receiveString = bufferedReader.readLine()) != null) {
                        if (!reciveString.equals(deleteline)) {
                            stringBuilder.append(receiveString);
                        }
                    }
                    fos.flush();

                    fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
                    fos.write(stringBuilder.toString().getBytes());
                    fos.close();
                    inputStream.close();
                   }

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

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