由于某种原因使用 FileWriter 和 BufferedWriter 清除文件?

2024-01-11

由于某种原因,当我在程序中创建新的 BufferedWriter 和 FileWriter 时(即使我还没有使用它来写入任何内容),它会清除我选择的文件的所有文本。

selectedFile 由 JFileChooser 确定。

public static File selectedFile;

    public static void Encrypt() throws Exception {

    try {
        //if I comment these two writers out the file is not cleared.
        FileWriter fw = new FileWriter(selectedFile.getAbsoluteFile());
        BufferedWriter bw = new BufferedWriter(fw);

        List<String> lines = Files.readAllLines(Paths.get(selectedFile.toString()),
                Charset.defaultCharset());
        for (String line : lines) {
            System.out.println(line);
            System.out.println(AESencrp.encrypt(line));

            /*file is cleared regardless of whether or not these are commented out or
             * not, as long as I create the new FileWriter and BufferedWriter the file
             * is cleared regardless.*/

            //bw.write(AESencrp.encrypt(line));
            //bw.close();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

AESencrp.encrypt 只是我拥有的一个加密类,它不会影响它。如果我创建一个新的 FileWriter 和 BufferedWriter ,那么这个循环甚至不会运行(至少我不这么认为,因为我没有得到行的加密或打印的文件的原始内容,如果我没有打印的话) t 创建了新的 FileWriter/BufferedWriter。)

        for (String line : lines) {
            System.out.println(line);
            System.out.println(AESencrp.encrypt(line));

            /*file is cleared regardless of whether or not these are commented out or
             * not, as long as I create the new FileWriter and BufferedWriter the file
             * is cleared regardless.*/

            //bw.write(AESencrp.encrypt(line));
            //bw.close();
        }

这是因为构造函数FileWriter如果文件已经存在,您正在使用的将截断该文件。

如果您想附加数据,请使用:

new FileWriter(theFile, true);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

由于某种原因使用 FileWriter 和 BufferedWriter 清除文件? 的相关文章

  • string.split("(?!^)") 解释

    我正在尝试将字符串的字符拆分为字符串数组 我找到了解决方案here https stackoverflow com questions 5235401 split string into array of character strings
  • Java中如何合并两个数组?

    它不是连接而是合并两个数组 使它们成为名称值对的数组 firstarray a aa aaa secondarray b bb bbb result a b aa bb aaa bbb 最好的方法是什么 in Java public sta
  • Spring MVC - 自动查找验证器

    假设我有一个像这样的示例实体类 public class Address 和相应的验证器 Component public AddressValidator implements Validator Override public bool
  • java中%%是什么意思?

    我是一名 PHP 程序员 想知道这行代码的含义 System out printf exp 3f is 3f n x Math exp x 3f 3f n 和逗号 x 是什么意思 它与C类似printf http java sun com
  • 参考接口创建对象

    引用变量可以声明为类类型或接口类型 如果变量声明为接口类型 则它可以引用实现该接口的任何类的任何对象 根据上面的说法我做了一个理解上的代码 正如上面所说声明为接口类型 它可以引用实现该接口的任何类的任何对象 但在我的代码中显示display
  • 在 Java 中生成 LaTeX 输出 [关闭]

    Closed 这个问题正在寻求书籍 工具 软件库等的推荐 不满足堆栈溢出指南 help closed questions 目前不接受答案 是否有用于从 Java 生成 LaTeX 输出的 Java 库 渲染乳胶 JLatex数学 https
  • python 中的子进程调用以使用 JAVA_OPTS 调用 java jar 文件

    示例代码 import subprocess subprocess call java jar temp jar 如何在上面的命令中指定JAVA OPTS 当我使用上述命令时 我收到 java lang OutOfMemoryError 无
  • 加密 mongodb 中的密码字段

    我有以下代码 它插入userName and password进入数据库 但密码以纯文本格式存储 我的意思是 当我查看数据库时 我可以看到插入的密码 我想存储password in encrypted format MongoClient
  • 检查更新时 Maven 无限期挂起

    我正在使用 Maven 构建一个项目 我是新手 并且它挂起 mvn package INFO Scanning for projects INFO INFO Building Presentation Reports INFO task s
  • 无法删除临时文件夹(有时)

    当我启动应用程序时 我创建一个临时文件夹 public static File createTempDir String name throws IOException File tempDir File createTempFile na
  • 如何组合 3 个或更多 CompletionStages?

    如果有 2 个 CompletionStages 我可以将它们与thenCombine method CompletionStage a aCompletionStage getA CompletionStage b bCompletion
  • Java:如果数组大小未知,如何初始化?

    我要求用户输入 1 到 100 之间的一些数字并将它们分配到一个数组中 数组大小未初始化 因为它取决于用户输入数字的次数 我应该如何分配数组长度 如果用户输入 5 6 7 8 9 5 个数字 则 int list becomes int l
  • 为什么 CompletableFuture 的 thenAccept() 不在主线程上运行

    我在 CompletableFuture 的 SupplyAsync 中处理长时间运行的操作 并将结果放入 thenAccept 中 有时 thenAccept 在主线程上执行 但有时它在工作线程上运行 但我只想在主线程上运行 thenAc
  • AWS SQS Batch SendMessageBatchRequest 非常慢

    我的应用程序使用 SendMessageBatchRequest 将每个请求发布 10 条消息到 AWS SQS 每条消息的大小小于250字节 该应用程序预计每天发布约一百万条记录 但要实现这一目标 消息发布的速度非常慢 AmazonSQS
  • Java 中的连接路径

    In Python我可以连接两条路径os path join os path join foo bar gt foo bar 我正在尝试在 Java 中实现相同的目标 而不用担心是否OS is Unix Solaris or Windows
  • 使用 Retrofit 获取原始 HTTP 响应

    我想从我的 API REST 获取原始 http 响应 我尝试过这个界面 POST login FormUrlEncoded Call
  • 尝试通过 Java 8 中的 JDBC-ODBC 连接到 .accdb 文件时出现 ClassNotFoundException

    我正在 Eclipse EE IDE 中的 Java 项目中工作 我必须在其中查询 accdb文件 问题是当我尝试加载驱动程序然后连接到数据库时 它给了我一个异常错误 My code try String filePath myfilepa
  • Java 执行器和长寿命线程

    我继承了一些使用 Executors newFixedThreadPool 4 的代码运行 4 个长寿命线程来完成应用程序的所有工作 这是推荐的吗 我读过Java 并发实践 https rads stackoverflow com amzn
  • Spring Data MongoDB 和批量更新

    我正在使用 Spring Data MongoDB 并且想要执行批量更新 就像此处描述的那样 http docs mongodb org manual reference method Bulk find update Bulk find
  • “mvn”不被识别为内部或外部命令、可操作程序或批处理文件

    可能是之前问过问题 但我发现了非常奇怪的错误 当我跑步时mvn versionbin 中的命令给出了版本和其他信息 但是 当我跑到垃圾箱外面时 它会出现异常 下面提到的结果 C Program Files apache maven 3 0

随机推荐