我无法确定我试图构建FileInputStream的错误。我对编程比较陌生

时间:2014-03-23 20:20:11

标签: java encryption fileinputstream eofexception

我正在为我的CS java类做一个家庭作业,我收到下面列出的错误。第二条水平线下方是源代码。有一个额外的java程序文件,用于运行Crypto.java,名为Lab13.java,我不太了解Logic的异常。代码和我澄清的问题在底部。


    Crypto.java:9: error: no suitable constructor found for FileInputStream(String[])
      FileInputStream inStream = new FileInputStream(existing);
                             ^
    constructor FileInputStream.FileInputStream(FileDescriptor) is not applicable
      (actual argument String[] cannot be converted to FileDescriptor by method invocation conversion)
    constructor FileInputStream.FileInputStream(File) is not applicable
      (actual argument String[] cannot be converted to File by method invocation conversion)
    constructor FileInputStream.FileInputStream(String) is not applicable
      (actual argument String[] cannot be converted to String by method invocation conversion)
Crypto.java:12: error: no suitable constructor found for FileOutputStream(String[])
      FileOutputStream outStream = new FileOutputStream(encrypted);
                               ^
        constructor FileOutputStream.FileOutputStream(FileDescriptor) is not applicable
          (actual argument String[] cannot be converted to FileDescriptor by method invocation conversion)
    constructor FileOutputStream.FileOutputStream(File,boolean) is not applicable
      (actual and formal argument lists differ in length)
    constructor FileOutputStream.FileOutputStream(File) is not applicable
      (actual argument String[] cannot be converted to File by method invocation conversion)
    constructor FileOutputStream.FileOutputStream(String,boolean) is not applicable
      (actual and formal argument lists differ in length)
    constructor FileOutputStream.FileOutputStream(String) is not applicable
      (actual argument String[] cannot be converted to String by method invocation conversion)
2 errors

以下是源代码:

    import java.io.*;
    public class Crypto
    {
        public static void encryptFile(String existing[], String encrypted[]) throws IOException
       {
          boolean eof = false;

      FileInputStream inStream = new FileInputStream(existing);
      DataInputStream inFile = new DataInputStream(inStream);

      FileOutputStream outStream = new FileOutputStream(encrypted);
      DataOutputStream outFile = new DataOutputStream(outStream);

      while (!eof)
      {
         try
         {
            byte input = inFile.readByte();
            int c = (input * 137) % 256;
            outFile.writeByte(c);
         }
         catch(EOFException e)
         {
            eof = true;
         }
      }

      outFile.close();
   }
}

就像我说的那样,下一部分用于运行Crypto。当它捕获IOException时它到底在做什么?它是否会显示EOF异常错误消息,因为它正在执行Crypto.encryptFile的try语句中捕获它?

   import java.io.*;
    public class Lab13
    {
       public static void main (String[] args)
       {
          try 
          {
             Crypto.encryptFile("MyLetters.txt", "Encrypted.txt");
             System.out.println("done.");
          }
          catch (IOException e)
          {
             System.out.println("Error- " + e.getMessage());
          }
       }
    }

2 个答案:

答案 0 :(得分:0)

以这种方式读取文件

String path="C://simple.txt";
BufferedReader br = new BufferedReader(new FileReader(path));

我想你无法获得路径。

答案 1 :(得分:0)

FileInputStream没有构造函数接受字符串数组(String [])。据推测,您希望传递一个字符串(使用文件的路径名)。 encryptFile方法的参数是字符串数组,而不是字符串。删除方法签名中的方括号,你应该没问题。