如何删除txt文件中的bufferedreader行

时间:2022-03-21 20:20:32

If i have a txt file like:

如果我有一个txt文件,如:

      aaaa
      bbbb
      cccc

and i dont know what is the text but i want to delete line 2 that it will be like:

我不知道文本是什么,但我想删除第2行,它将是:

      aaaa
      cccc

how can i do it

我该怎么做

2 个解决方案

#1


1  

You can't do that in a simple way unfortunately... You need to use java.io.InputStream to read data and java.io.OutputStream to write data to a file. So the only way to "delete" a line from an existing file is to read the original and write a new one without the line. That is odd I know, but the good thing is that you can stream it all without loading the whole file content in memory. Here is a sample.

不幸的是,你不能以简单的方式做到这一点......你需要使用java.io.InputStream来读取数据,并使用java.io.OutputStream将数据写入文件。因此,从现有文件中“删除”一行的唯一方法是读取原始文件并编写一个没有该行的新文件。我知道这很奇怪,但好处是你可以在不加载内存中的整个文件内容的情况下将其全部流式传输。这是一个样本。

import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;

public class FileStream {

    public static void main(String ...args){

        try(
            //Creates a print writer for new file, will truncate existing with same name
            PrintWriter pw = new PrintWriter(
                    Files.newOutputStream(Paths.get("out.txt"), 
                            StandardOpenOption.CREATE, 
                            StandardOpenOption.TRUNCATE_EXISTING))
        ) 
        {
            //Stream in each line of existing file
            Files.lines(Paths.get("in.txt"))
            //Filter out the lines you don't want
            .filter(line -> !"bbbb".equals(line))
            //Print the other to new file
            .forEach(pw::println);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

#2


0  

Try this code

试试这个代码

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class FileSaver {


public static void main(String[] args)
{
    try {
        new FileSaver().read();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

File file = new File("C:/Users/Desktop/abc.txt"); // File from you read
File file1 = new File("C:/Users/Desktop/out.txt"); // File where you write

int line_num_todelete =2;

public void read() throws IOException
{
    BufferedReader fileReader=null;
    BufferedWriter fileWriter=null;
    try {
         fileReader = new BufferedReader(new FileReader(file));
         fileWriter = new BufferedWriter(new FileWriter(file1));
         String text = "";

         int count =1;
        while((text=fileReader.readLine())!=null)
        {
            //System.out.println(text);
            if(count!=line_num_todelete)
            {
              fileWriter.write(text);
              fileWriter.newLine();
            }
            count++;
        }

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    finally{
         fileReader.close();
         fileWriter.close();
    }
}

}

#1


1  

You can't do that in a simple way unfortunately... You need to use java.io.InputStream to read data and java.io.OutputStream to write data to a file. So the only way to "delete" a line from an existing file is to read the original and write a new one without the line. That is odd I know, but the good thing is that you can stream it all without loading the whole file content in memory. Here is a sample.

不幸的是,你不能以简单的方式做到这一点......你需要使用java.io.InputStream来读取数据,并使用java.io.OutputStream将数据写入文件。因此,从现有文件中“删除”一行的唯一方法是读取原始文件并编写一个没有该行的新文件。我知道这很奇怪,但好处是你可以在不加载内存中的整个文件内容的情况下将其全部流式传输。这是一个样本。

import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;

public class FileStream {

    public static void main(String ...args){

        try(
            //Creates a print writer for new file, will truncate existing with same name
            PrintWriter pw = new PrintWriter(
                    Files.newOutputStream(Paths.get("out.txt"), 
                            StandardOpenOption.CREATE, 
                            StandardOpenOption.TRUNCATE_EXISTING))
        ) 
        {
            //Stream in each line of existing file
            Files.lines(Paths.get("in.txt"))
            //Filter out the lines you don't want
            .filter(line -> !"bbbb".equals(line))
            //Print the other to new file
            .forEach(pw::println);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

#2


0  

Try this code

试试这个代码

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class FileSaver {


public static void main(String[] args)
{
    try {
        new FileSaver().read();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

File file = new File("C:/Users/Desktop/abc.txt"); // File from you read
File file1 = new File("C:/Users/Desktop/out.txt"); // File where you write

int line_num_todelete =2;

public void read() throws IOException
{
    BufferedReader fileReader=null;
    BufferedWriter fileWriter=null;
    try {
         fileReader = new BufferedReader(new FileReader(file));
         fileWriter = new BufferedWriter(new FileWriter(file1));
         String text = "";

         int count =1;
        while((text=fileReader.readLine())!=null)
        {
            //System.out.println(text);
            if(count!=line_num_todelete)
            {
              fileWriter.write(text);
              fileWriter.newLine();
            }
            count++;
        }

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    finally{
         fileReader.close();
         fileWriter.close();
    }
}

}