Hel*_*nar 1 java eclipse path classpath
您好我在Eclipse下编写了这样的函数:
public static ArrayList<String> getMails(){
    ArrayList<String> mails = new ArrayList<String>(); 
      try{
            FileInputStream fstream = new FileInputStream("mails.txt");
            DataInputStream in = new DataInputStream(fstream);
                BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String strLine;
            while ((strLine = br.readLine()) != null)   {
              mails.add(strLine.trim());
            }
            in.close();
            }catch (Exception e){//Catch exception if any
              System.err.println("Error: " + e.getMessage());
            }
    return mails;
}
mails.txt文件位于workspace/projectname下,我想将此项保存在workspace/projectname/bin /目录下,作为相对路径,因此每当我将workspace/projectname // bin目录复制到其他位置或计算机时,让它工作.但是,当我尝试这个时,我得到"FileNotFound"异常.我怎样才能解决这个问题 ?谢谢
如果将文本文件保存在类所在的源目录(不是bin目录)中(上面摘录的那个),那么该文件将在构建期间自动复制到bin目录中.您将其作为资源读取,而不是作为文件读取:
final InputStream in = MyClass.class.getResourceAsStream("mails.txt");
final Reader isr = new InputStreamReader(in, "ISO-8859-1"); //or whatever
final BufferedReader br = new BufferedReader(isr);
try {
    // ...
} finally {
    br.close();
}
| 归档时间: | 
 | 
| 查看次数: | 315 次 | 
| 最近记录: |