Click here to Skip to main content
15,925,723 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
At run time ,the exception is FileNotFoundException
even when i created the text file at same directory.


how can i solve this problem ?


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

public class caesarC{

    public static void main(String [] args)

        {
            String l="abcdefghijklmnopqrstuvwxyz";
            int k;
        //  String p;


            try{
                Scanner in = new Scanner(System.in);

                String s,output="";
                System.out.print("plz enter key !!");
                k=in.nextInt();
               BufferedReader reader =new BufferedReader(new FileReader("ss.txt"));

            while((s=reader.readLine())!=null)
            {
            output+=s;
            String cipher = Encrypt(output,k,l);
            System.out.print("file to encrypt: "+cipher);
            }



            }

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

            }

    public static String Encrypt(String str,int key,String l)
        {
            String enc="";

            for (int i=0;i<str.length();i++)
                {
                    char c = str.charAt(i);

                    if(c==' ')
                        { enc+=' ';
                            continue;
                            }
                    int ind=l.indexOf(c);
                    int npos=(ind+(key*i+1))%26;
                    enc +=l.charAt(npos);
                    }
                    return enc;
            }



}
Posted

Give the relative path like

BufferedReader reader =new BufferedReader(new FileReader(".\ss.txt"));

and if it does not work then you should first try to create a file just above the reader code and then write something and close it. after that call this FileReader command. It will make clear the reason behind File not found exception.
 
Share this answer
 
Comments
TorstenH. 1-Mar-12 4:11am    
good answer! Don't know who downvoted here, there is no reason for that.
File not found.

I assume there is no file "ss.txt".

Java
new FileReader(new File("ss.txt"));


that would create a file if there is non.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900