适用于文件的Java输入/输出程序

时间:2016-10-18 13:38:01

标签: java file oop input output

所以这是一个从文件中读取输入的代码

  

date.in.txt

然后通过从文件读取的文本中的k个字符和密钥中的(k%n)字符之间进行XOR运算来加密,其中密钥长度在哪里。然后,它将其输出到

  

date.out.txt

在第二部分中,它反过来解密来自

的文本
  

date.out.txt

并将其输出到

  

date.in.txt

以下是第一部分的示例:

  

date.in:Hello World!你好吗?

     

date.out:43 4 5 2 10 67 54 6 28 9 7 64 73 100 45 12 22 73 15 23 6 65   16 1 16 92

这是反过来的一个:

  

date.out.txt:43 4 5 2 10 67 54 6 28 9 7 64 73 100 45 12 22 73 15 23 6 65   16 1 16 92

     

date.in.txt:Hello World!你好吗?

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) throws FileNotFoundException 
        {Scanner scan=new Scanner(System.in);


        File file = new File("date.in.txt");
        File file2 = new File("date.out.txt");


        String [] s= new String[100];
        String S= new String();
        for(int i=0;i<100;i++)
            s[i]=new String();
        int nr=0;


        int vec[]=new int[100];
        int nrvec=0;


        String cuv;
        System.out.print("Enter key: ");
        cuv=scan.nextLine();
        System.out.print("\n");
        int n=cuv.length();


        try 
            { Scanner input = new Scanner( file );
                while(input.hasNextLine())
                    { s[nr++]=input.nextLine();
                    s[nr++]="\n";} }

        catch (FileNotFoundException ex) 
            { System.out.printf("ERROR: %s!\n", ex); }


        for(int i=0;i<nr;i++)
            for(int j=0;j<s[i].length();j++)
                { vec[nrvec++]=s[i].codePointAt(j); }


        for(int i=0;i<nrvec;i++)
            { int d=cuv.codePointAt(i%n);
            vec[i]=vec[i] ^ d; }


        nrvec--;


         try 
            { PrintWriter output = new PrintWriter(file2);
            for(int i=0;i<nrvec;i++)
                output.print(vec[i]+" ");
            output.close(); } 

         catch (IOException ex) 
            { System.out.printf("ERROR: %s!\n", ex); }


         int ok=1; // SECOND PART
         if(ok==1)
            {System.out.print("Enter key: ");
            cuv=scan.nextLine();
            System.out.print("\n");
            n=cuv.length();

            File file3 = new File("date.out.txt");
            File file4 = new File("date.in.txt");

            nrvec=0;

            try 
                { Scanner input = new Scanner( file3 );
                    while(input.hasNext())
                        { vec[nrvec++]=input.nextInt(); } }

            catch (FileNotFoundException ex) 
                { System.out.printf("ERROR: %s!\n", ex); }

            int e;
            try 
                { PrintWriter output = new PrintWriter(file4);
                for(int i=0;i<nrvec;i++)
                    { e=cuv.codePointAt(i%n);
                    output.print(vec[i]^e +" ");}  // 93
                output.close(); } 

            catch (IOException ex) 
                { System.out.printf("ERROR: %s!\n", ex); }


            }}}

问题是我在第93行遇到错误,说:

  

线程“main”中的异常java.lang.Error:未解析的编译   问题:对于参数类型int,操作符^未定义,   串

     

在Main.main(Main.java:93)

我不知道为什么会这样。

2 个答案:

答案 0 :(得分:3)

将该行写为

 output.print((vec[i]^e) +" ");}  // 93

请注意括号。没有它们,原始代码实际上被视为

 output.print(vec[i]^ (e +" "));}  // 93

自&#34; +&#34;运算符的优先级高于&#34; ^&#34;。

答案 1 :(得分:0)

只需将93处的行更改为:

  

output.print((vec [i] ^ e)+“”);}

你遇到的问题是优先。