【问题标题】:How to read or replace multiple lines of a file all at once? [duplicate]如何一次读取或替换文件的多行? [复制]
【发布时间】:2013-06-22 00:01:40
【问题描述】:

我目前正在编写一个加密程序,该程序使用 64 位加密对文本文档进行加密。它的工作方式是接受一个字符串,并加密该字符串。我目前正在寻找一种方法让程序将文件的所有内容存储在字符串中,加密字符串,然后用加密的字符串覆盖文件。但是,使用

while((bufferedReader.readLine()) != null) {
...
}

它只读取和加密第一行,其余部分保持不变。

但是,使用:

            List<String> lines = Files.readAllLines(Paths.get(selectedFile.toString()),
                Charset.defaultCharset());
        for (String line : lines) {
        ...
        }

只有最后一行被加密。老实说,我已经不知道该怎么做了,因为我的想法已经不多了。

这是我当前的代码(它也只附加到文件中,因为我正在尝试新的东西。):

    public static void Encrypt() throws Exception {

    try {

        FileWriter fw = new FileWriter(selectedFile.getAbsoluteFile(), true);
        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));
            bw.write(AESencrp.encrypt(line));
        }

        bw.close();

    } catch (IOException e) {
        e.printStackTrace();
    }
}

【问题讨论】:

    标签: java file filewriter bufferedwriter


    【解决方案1】:

    BufferedReader#readLine 将返回从阅读器读取的文本行。在您的示例中,您忽略了返回值。

    相反,你应该做类似...

    String text = null;
    while((text = bufferedReader.readLine()) != null) {
        // Process the text variable
    }
    

    【讨论】:

    • 这是我尝试的第一件事,但我想不出一种方法让文件编写器只替换它当前正在读取的整行。例如,FileWriter 将追加并仅添加加密,或仅在 FileWriter 完全删除文件的其余部分之前加密第一行。这就是为什么我希望有一种方法可以同时读取整个文档并将整个内容存储在一个字符串中。
    • 您需要写入一个临时文件,一旦完成并且流关闭,您将删除旧文件并在其位置重命名新文件。同时读写一个文件非常困难
    【解决方案2】:

    我不认为逐行加密是一个好主意。我会这样做

    Cipher cipher = ...
    Path path = Paths.get(file);
    File tmp = File.createTempFile("tmp", "");
    try (CipherOutputStream cout = new CipherOutputStream(new FileOutputStream(tmp), cipher)) {
        Files.copy(path, cout);
    }
    Files.move(tmp.toPath(), path, StandardCopyOption.REPLACE_EXISTING);
    

    并像这样读取加密文本

    Scanner sc = new Scanner(new CipherInputStream(new FileInputStream(file), cipher));
    while(sc.hasNextLine()) {
        ...
    

    【讨论】:

      【解决方案3】:

      尝试下一个:

      public static void Encrypt() throws Exception {
          try {
              Path path = Paths.get(selectedFile.toURI());
              Charset charset = Charset.defaultCharset();
      
              // Read file
              List<String> lines = Files.readAllLines(path, charset);
      
              // Encrypt line
              lines.set(0, AESencrp.encrypt(lines.get(0)));
      
              // Write file
              Files.write(path, lines, charset);
      
          } catch (IOException e) {
              e.printStackTrace();
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2012-10-13
        • 2013-04-21
        • 2014-12-05
        • 2020-10-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-15
        相关资源
        最近更新 更多