比如在目录的文件夹下有个文件haha.txt,现在我通过计算知道string haha了,如何根据这个string来读取文件呢?

解决方案 »

  1.   

    try {
        
                FileReader fr = new FileReader(path); //path 为haha.txt的绝路径
                BufferedReader br = new BufferedReader(fr);
                /*逐行读取*/
                String line = br.readLine();
                while (line != null) {
                    System.out.println(line);
                    line = br.readLine();
                }
                br.close();
                fr.close();
            } catch (FileNotFoundException ex) {
                ex.printStackTrace();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
      

  2.   

    path 为 haha.txt 的绝对路径 
    如果 haha.txt 在 c:\mytest 下
    绝对路径表示为: path="c:\\mytest\\haha.txt";
      
      

  3.   

    问题是你如何确定当前目录下没有叫"haha.jpg"或者其它后缀的?这个能解决就不会有问题.
    否则你必须指定后缀名来确认具体哪个文件.