找不到文件异常? [英] File not found exception ?

查看:153
本文介绍了找不到文件异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在运行时,异常是FileNotFoundException
即使在同一目录下创建文本文件也是如此.


我该如何解决这个问题?


At run time ,the exception is FileNotFoundException
even when i created the text file at same directory.


how can i solve this problem ?


 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;
            }



}

推荐答案

给出相对路径,如

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

如果它不起作用,则应首先尝试在阅读器代码上方创建一个文件,然后编写一些内容并将其关闭.之后,调用此FileReader命令.这将弄清文件未找到异常背后的原因.
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.


文件未找到.

我假设没有文件"ss.txt".

File not found.

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

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



如果没有则创建一个文件.



that would create a file if there is non.


这篇关于找不到文件异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆