Keane_T 2015-04-22 14:24 采纳率: 0%
浏览 2930

Java filenotfoundexception问题,路径找不到。。。

提示我路径未找到,文件名是自己键入的,代码如下:
public class CSVParser extends ConsoleProgram {

public void run(){
    ArrayList<String> result = new ArrayList<String>();
    String filename = openfile("Enter the filename :");
    int columnIndex = readInt("Enter the column index :");
    result = extractColumn(filename,columnIndex);
    println(result);    
}
//读取文件,若文件不存在则提示,重新键入文件名
private String openfile(String prompt){
    BufferedReader rd = null;
    while(rd==null){
        try{
            String name = readLine(prompt);
            rd = new BufferedReader(new FileReader(name));          
        }catch(IOException e){
            println("bad file.The file doesn't exist.");
        }
    }
    return getName();       

}
//根据文件名和所要获取的列数输出该列所有内容
private ArrayList extractColumn(String filename, int columnIndex) {
ArrayList result = new ArrayList();
try {

        BufferedReader rd = new BufferedReader(new FileReader(filename));   
        String line="";
        while((line = rd.readLine()) != null){              
            String str = (fieldsIn(line).get(columnIndex-1));           
            result.add(str);            
            }rd.close();
}catch (IOException e){ 
    e.printStackTrace();
    throw new RuntimeException(e);
        }               
    return result;
}

}

  • 写回答

2条回答 默认 最新

  • 毕小宝 领域专家: 后端开发技术领域 2015-04-23 00:44
    关注

    工具是最公正的,有异常还是说明程序本身有问题。你检查下输入的文件路径是否正确。文件路径中统一用“/"。
    祝好!

    评论

报告相同问题?