简体   繁体   English

为什么这段代码不起作用?

[英]Why this piece of code does not work?

I wrote this piece of code and it is supposed to replace all the characters in a file named "abc.txt" with asterisks. 我写了这段代码,它应该用星号替换名为“abc.txt”的文件中的所有字符。 But when I run this code it simply erases everything in the file. 但是,当我运行此代码时,它只是删除文件中的所有内容。 Please anyone help me figure out what is wrong here. 请有人帮我弄清楚这里有什么问题。 Thanks 谢谢

import java.io.*;
import java.util.*;

class Virus{

    public static void main(String[] args) throws Exception{

        File f = new File("abc.txt");
        FileReader fr = new FileReader(f);
        FileWriter fw = new FileWriter(f);
        int count = 0;
        while(fr.read()!=-1){
            count++;
        }
        while(count-->0){
            fw.write('*');
        }
        fw.flush();     
        fr.close();
        fw.close();
    }
}

You should create file reader and writer in sequence, not at once. 您应该按顺序创建文件阅读器和编写器,而不是一次创建。

FileReader fr = new FileReader(f);
FileWriter fw = new FileWriter(f); // here you are deleting your file content before you had chance to read from it

You should do following: 你应该做以下事情:

public static void main(String[] args) throws Exception{

    File f = new File("abc.txt");
    FileReader fr = new FileReader(f);
    int count = 0;
    while(fr.read()!=-1){
        count++;
    }
    fr.close();

    FileWriter fw = new FileWriter(f);
    while(count-->0){
        fw.write('*');
    }
    fw.flush();     
    fw.close();
}

First you need to the reading of the file and then close the file object. 首先,您需要读取文件,然后关闭文件对象。 Then start writing the content into it. 然后开始将内容写入其中。

By default, it opens the file in write mode. 默认情况下,它以写入模式打开文件。 All the data is lost before you reading anything from it. 在您从中读取任何内容之前,所有数据都将丢失。

class Virus{

  public static void main(String[] args) throws Exception{

    File f = new File("/Users/abafna/coding/src/abc.txt");
    FileReader fr = new FileReader(f);
    int count = 0;
    while(fr.read()!=-1){
      count++;
    }
    fr.close();
    System.out.println(count);
    FileWriter fw = new FileWriter(f);
    while(count-->0){
      fw.write('*');
    }
    fw.flush();
    fw.close();
  }
}
Using FileWriter fw = new FileWriter(f);

This clears the content of your file. 这会清除文件的内容。 This is because the constructor of FileWriter you are using truncates the file if it already exists. 这是因为您使用的FileWriter构造函数会截断该文件(如果该文件已存在)。

If you want to append the data instead, use: 如果要添加数据,请使用:

new FileWriter(theFile, true);

As the others have said you are formatting the file when you create a FileWriter, but there is also no reason for you to read the file anyways. 正如其他人所说,您在创建FileWriter时正在格式化文件,但是也没有理由让您无论如何都要读取该文件。

public static void main(String[] args) throws Exception {
    File f = new File("abc.txt");
    long length = f.length(); //length of file. no need to read it.
    OutputStream out = new BufferedOutputStream(new FileOutputStream(f));
    for (int i = 0; i < length; i++) {
        out.write('*');
    }
    out.close();
}

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

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