执行时找不到文件

时间:2011-06-25 17:34:18

标签: java-ee

import java.io.*;

public class NumberOfLine{
    public static void main(String[] args) {
        try{
            System.out.println();
            System.out.println("Getting line number of a paritcular file example!");
                System.out.println("***************************************************************");
                System.out.println();


        //  FileInputStream fstream = new FileInputStream("F://Inputfile.txt");
        //  DataInputStream in = new DataInputStream(fstream);
        //  System.out.println();
        //  BufferedReader bf = new BufferedReader(new InputStreamReader(in));
        //  String strLine;

            FileInputStream fstream = new FileInputStream("F://Inputfile.txt");
            BufferedReader bf = new BufferedReader(new InputStreamReader(fstream));
            DataInputStream in = new DataInputStream(fstream);


//      System.out.println("Please enter file name (location) with extension:");


            String str = bf.readLine();
            File file = new File(str);
            if (file.exists()){
                FileReader fr = new FileReader(file);
                LineNumberReader ln = new LineNumberReader(fr);
                int count = 0;
                while (ln.readLine() != null){
                    count++;
                }
                System.out.println("Total line no: " + count);
                ln.close();
            }
            else{
                System.out.println("File does not exists!");
            }
        }
        catch(IOException e){
            e.printStackTrace();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您的代码说要读取其路径作为InputFile.txt的第一行给出的文件。这是你的意图吗?如果是这样,请尝试更改InputFile.txt的第一行以获得绝对路径。如果可行,请尝试重新修改原始路径。