2013-05-26 143 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");

缺點是你不能來資源。如果你想這樣做,我強烈建議不要使用數據庫文件的源文件夾。而是使用系統內的可配置位置(例如當前工作文件夾)。