2013-05-26 227 views
1

我有一个扫描仪读取.csv文件。
该文件位于相同的目录和.java文件中,但似乎无法找到该文件。
我能做些什么来解决这个问题?Java扫描仪文件

Scanner scanner = new Scanner(new File("database.csv")); 

编辑:抱歉忘了提及我需要使用扫描仪软件包,因为在下一行我使用了分隔符。

Scanner scanner = new Scanner(new File("database.csv")); 
scanner.useDelimiter(",|\r|\n"); 

而且我在IntelliJIDEA

所以在这里工作

是完整的代码

import java.io.File; 
import java.io.FileNotFoundException; 
import java.util.Scanner; 
import java.util.*; 

public class City 
{ 
public String name; // The name of the city 
public String cont; // The continent of the city 
public int relTime; // Time relative to Hobart (eg. -14 for New York) 
public boolean dst; // Does the city use DST? 
public boolean valid; // Does the city exist? 
Date currDate; 

City(){}; // Default constructor 
City(String name, String cont, int relTime) 
{ 
    this.name = name; 
    this.cont = cont; 
    this.relTime = relTime; 
    valid = verify(); 

    if(valid) 
    { 
     currDate = new Date(System.currentTimeMillis() + (3600000 * relTime)); 
    } 
} 

City(String name, String cont, int relTime, int dstStartDay, int dstEndDay) 
{ 
    this.name = name; 
    this.cont = cont; 
    this.relTime = relTime; 
    valid = verify(); 

    if(valid) 
    { 
     currDate = new Date(System.currentTimeMillis() + (3600000 * relTime)); 
     // Is DST in effect? 
     if(currDate.after(new Date(currDate.getYear(), 3, dstStartDay, 2, 0)) && 
       currDate.before(new Date(currDate.getYear(), 11, dstEndDay, 2, 0))) 
     { 
      // It is... so 
      relTime--; 
     } 
    } 
} 

private boolean verify() 
{ 
    valid = false; 

    try 
    { 
     Scanner scanner = new Scanner(new File("\\src\\database.csv")); 
     scanner.useDelimiter(",|\r|\n"); 
     while(scanner.hasNext()) 
     { 
      String curr = scanner.next(); 
      String next = new String(); 
      if(scanner.hasNext()) 
       next = scanner.next(); 
      if(curr.contains(cont) && next.contains(name)) 
       return true; 
     } 
     scanner.close(); 
    } 
    catch(FileNotFoundException e) 
    { 
     e.printStackTrace(); 
    } 

    return false; 
} 
} 
+0

是在同一个目录下的.class文件呢? –

回答

3

当你把csv文件与源代码放在一起,你可以直接不是新的文件,你可以试试,

InputStream resourceAsStream = this.getClass().getResourceAsStream("database.csv"); 
    Scanner scanner = new Scanner(resourceAsStream); 
    scanner.useDelimiter(",|\r|\n"); 
+1

这工作完美,除了我使用URL数据库= this.getClass()。getResource(“database.csv”);谢谢:) –

+0

奇怪,我会发誓,第一行直接是我的答案的一部分,我甚至没有看到upvote。 –

2

在创建或读取一个相对文件,路径是相对于指定user.dir。在eclipse中,这通常是你的项目的根源。

可以打印出user.dir如下:

System.out.println(System.getProperty("user.dir")); 

这是程序正在寻找database.csv文件。将文件添加到此目录或使用绝对路径。

0

从项目的根文件夹开始添加文件的整个路径。

例如。

测试是我在Eclipse中的项目名称。所以,我应该写

File csvFile = new File("src\\database.csv");

+0

我试过这样做,但没有任何区别。仍然没有编译 –

0

你不应该保留,但同时含有类文件的目录中的文件。如果这样做,则不应将它们作为文件访问,而应作为资源访问,并且应将其复制到包含编译的.class文件的文件夹或.jar。如果该文件仅被.jar中的一个类使用,则应该使用this.getClass().getResource("database.csv");

缺点是你不能来资源。如果你想这样做,我强烈建议不要使用数据库文件的源文件夹。而是使用系统内的可配置位置(例如当前工作文件夹)。