【问题标题】:Java File encryption, can't get it to accept file inputJava文件加密,无法接受文件输入
【发布时间】:2013-11-25 04:28:33
【问题描述】:

我现在正在尝试使用 Java 进行简单的文件加密。没有什么严肃和核心的,只是非常基本的。现在我需要通过向文件中的每个字节添加 5 来对文件进行编码。程序提示用户输入输入文件名和输出文件名,并将输入文件的加密版本保存到输出文件中。

这是我的代码

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

public class EncryptFiles {
    public static void main(String[] args) throws IOException  {

        Scanner input = new Scanner(System.in);

        System.out.print("Enter a file to encrypt: ");

        FileInputStream in = new FileInputStream(input.next());
     //  BufferedInputStream in = new BufferedInputStream(new FileInputStream(new File(input.next())));

        System.out.print("Enter the output file: ");

        FileOutputStream output = new FileOutputStream(input.next());
     //  BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream(new File(input.next())));

        int value;

        while ((value = in.read()) != -1) {
          output.write(value + 5);
        }

        input.close();
        output.close();

    }
}

我已经完成了常规的 FileInputStream 和 BufferedInputStream 并且它们都给了我同样的错误。

> Exception in thread "main" java.io.FileNotFoundException: Me.txt (The
> system cannot find the file specified)    at
> java.io.FileInputStream.open(Native Method)   at
> java.io.FileInputStream.<init>(FileInputStream.java:146)  at
> java.io.FileInputStream.<init>(FileInputStream.java:101)  at
> EncryptFiles.main(EncryptFiles.java:10) Java Result: 1

我将文件 Me.txt 放在 C: 目录中,我已将文件放入我也尝试过将其放入 Java src 文件夹中。我试图输入确切的文件路径,但没有任何效果。我已经这样做了 C:/Users/Richard/Documents/Me.txtC:\Users\Richard\Documents\Me.txt 这样,但无论我如何尝试,我都会遇到同样的错误。

感谢您以后的帮助:)

【问题讨论】:

  • 我也试过了,还是出现同样的错误
  • 刚试了一下,还是报错
  • 对我来说非常好用。 Me.txt 实际上是 Me.txt.txt 吗?默认情况下,Windows 隐藏文件扩展名,因此如果您将文件命名为 Me.txt,它实际上将被命名为 Me.txt.txt
  • 就是这样!!!大声笑非常感谢你:)

标签: java encryption file-io inputstream outputstream


【解决方案1】:

尝试将FileInputStreamFileOutputStream 的实例化方式更改为

FileInputStream in = new FileInputStream(new File(input.nextLine()));

FileOutputStream in = new FileOutputStream(new File(input.nextLine()));

并使用完整路径作为输入,例如C:/Users/Richard/Documents/Me.txt

【讨论】:

    【解决方案2】:

    我会尝试这样的 -

    String fileInPath = input.next().trim();
    System.out.println("Opening file " + fileInPath);
    FileInputStream in = new FileInputStream(fileInPath);
    

    我自己可能会使用更像这样的东西;您应该始终关闭您的资源。

    String fileInPath = input.next().trim();
    System.out.println("Opening file " + fileInPath);
    FileInputStream in = null;
    try {
      File fileIn = new File(fileInPath);
      if (fileIn != null && fileIn.canRead()) {
        in = new FileInputStream(fileIn);
      } else {
        System.out.println("Could not open file " + fileInPath);
      }
    } catch (FileNotFoundException e) {
    } finally {
      if (in != null) {
        try {
          in.close();
        } catch (IOException e) {
        }
      }
    }
    

    【讨论】:

    • 感谢您的帮助,我已经通过有用的 cmets 解决了这个问题 :)
    猜你喜欢
    • 2016-02-15
    • 2014-09-10
    • 1970-01-01
    • 2011-02-18
    • 2012-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多