文件已存在时FileNotFoundexception

时间:2013-12-11 00:54:25

标签: java

这段代码无法找到缓冲读者应该从中读取的文件,而且我在eclipse项目的src文件夹中有文件,但它仍然没有从文件中读取,所以任何人都有关于问题是什么的任何想法。

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.math.*;
import java.util.ArrayList;


public class Encrypt {
public static ArrayList<String> data = new ArrayList<String>();
public static BigInteger [] keys = new BigInteger[3];
    public static BigInteger n;
public static double e; 
public static BigInteger d;
public static String line;
public static String result;
    public static String [] temp;
public static BigInteger tempVar;
public static BigInteger tempResult;
public static int tempVar2;
public static void encryption(ArrayList<String> data) throws IOException{
    for (int i = 0; i<data.size(); i++){
    if(data.get(i)!= null){

        temp = new String[data.get(i).split(" ").length];
        temp = data.get(i).split(" ");
        for(int j = 0; j<temp.length;j++){
            for (int k = 0; k< temp[j].length(); k++){
                tempVar2 = (int)temp[j].charAt(k);
                        tempVar=BigInteger.valueOf((long)Math.pow(tempVar2,e));
                    tempResult = (tempVar.remainder(n));
                result =""+ tempResult;
                LogEncrypt(result);
            }

        }
    }
}

}
public static void read() throws IOException{
 try {
     BufferedReader br = new BufferedReader(new FileReader("plainText.txt"));
     System.out.println(br.ready());
     while ((line = br.readLine()) != null) {
         data.add(br.readLine());
     }
     System.out.println("done with text");
 } catch (FileNotFoundException e) {
     System.out.println("please add the text file");
     e.printStackTrace();
 }
 try {
        BufferedReader ba = new BufferedReader(new FileReader("Key.txt"));
        System.out.println(ba.ready());
        int i =0;
        while ((line = ba.readLine()) != null) {
         keys[i] = new BigInteger(ba.readLine());
         i++;
        }
        n = keys[0];
        e = keys[1].doubleValue();
        d = keys[2];
        System.out.println("done with key");   

    } catch (FileNotFoundException e) {
        System.out.println("please add the key file");
        e.printStackTrace();
    }

}
public static void LogEncrypt(String result) throws IOException { 
BufferedWriter out = new BufferedWriter(new FileWriter("output.txt"));
try {

        out.write(result);
         out.newLine();
} catch(IOException e1) {
    System.out.println("Error during reading/writing");
} finally {
    out.close();
}


}
public static void main(String[]args) throws IOException{
read();
encryption(data);
}
}

1 个答案:

答案 0 :(得分:1)

将文件放在src之外,或者至少将“src /”添加到文件位置