基本java I/O文本替换,未获得预期输出

基本java I/O文本替换,未获得预期输出,java,io,Java,Io,我有一个包含数据库表数据的文本文件,我正试图删除表的一部分。这是文件中的内容: name= john name= bill name= tom name= bob name= mike 这里是我编译和运行的java代码,但是输出不是我所期望的,并且没有得到满足 import java.io.*; public class FileUtil { public static void main(String args[]) { try { Fi

我有一个包含数据库表数据的文本文件,我正试图删除表的一部分。这是文件中的内容:

name= john
name= bill
name= tom
name= bob
name= mike
这里是我编译和运行的java代码,但是输出不是我所期望的,并且没有得到满足

import java.io.*;
public class FileUtil {

    public static void main(String args[]) {

        try {

            FileInputStream fStream = new FileInputStream("\\test.txt");
            BufferedReader in = new BufferedReader(new InputStreamReader(fStream));


            while (in.ready()) {
                //System.out.println(in.readLine());
                String line = in.readLine();
                String keyword = "name="; //keyword to delete in txt file
                String newLine=line.replaceAll(keyword,""); //delete lines that say name=
                BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("testEdited.txt"));
                out.write(newLine.getBytes());
                out.close();

            }
            in.close();

        } catch (IOException e) {
            System.out.println("File input error");
        }

    }
}
Testeded文件中的输出如下:

迈克

显然,我只想留下5个名字。有人能帮我吗? 谢谢

试试这个:

BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("testEdited.txt",true));
true
会将数据附加到您的文件中。

尝试以下操作:

BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("testEdited.txt",true));

true
将向您的文件追加数据。

您不能使用
BufferedInputStream/bufferedOutput Stream
进行并行读写操作。
如果要同时读取和写入文件,请使用
RandomAccessFile

对于并行读写操作,不能使用
BufferedInputStream/BufferedOutput Stream
。 如果要同时读取和写入文件,请使用随机访问文件。

试试这个

        String line;
        BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("testEdited.txt"));

        while ((line = in.readLine()) != null) {
            String newLine=line.replaceAll("name=","");
            out.write(newLine.getBytes());
        }
        out.close();
        in.close();
无需一直打开和关闭输出文件

同样关于“name=”声明,赋值给变量并仅在紧跟其后的行中使用它没有多大意义。如果需要为共享常量,则将其声明为
(private | public)静态最终字符串foo=“bar”在某个类中

此外,将文件输出流(或文件写入器)封装在适当的缓冲版本中并没有多大好处,操作系统将自动为您缓冲写入,并且它在这方面做得很好

您还应该用读卡器替换流,并在finally块中关闭文件。

试试这个

        String line;
        BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("testEdited.txt"));

        while ((line = in.readLine()) != null) {
            String newLine=line.replaceAll("name=","");
            out.write(newLine.getBytes());
        }
        out.close();
        in.close();
无需一直打开和关闭输出文件

同样关于“name=”声明,赋值给变量并仅在紧跟其后的行中使用它没有多大意义。如果需要为共享常量,则将其声明为
(private | public)静态最终字符串foo=“bar”在某个类中

此外,将文件输出流(或文件写入器)封装在适当的缓冲版本中并没有多大好处,操作系统将自动为您缓冲写入,并且它在这方面做得很好


您还应该用读卡器替换流,并在finally块中关闭文件。

您的
新文件outputstream
将始终从文件开头写入数据。考虑使用另一个构造函数,将<代码>追加< /代码>作为布尔参数。或者在顶部打开一次,在程序结束时关闭一次。哦,这是家庭作业吗?不,我创建这个程序是为了帮助一个不同类的数据挖掘项目,我需要删除不需要的表数据并尝试复习我的java。你的
新文件outputstream
将始终从文件的开头写入数据。考虑使用另一个构造函数,将<代码>追加< /代码>作为布尔参数。或者在顶部打开一次,在程序结束时关闭一次。哦,这是家庭作业吗?不,我创建这个程序是为了帮助一个不同类的数据挖掘项目,我需要删除不需要的表数据并尝试重新学习我的java。dest文件与源文件不同。dest文件与源文件不同。