【发布时间】:2013-11-21 16:41:22
【问题描述】:
我正在尝试在 csv 文件中使用特定的数字字符串,即使文件存在,我也会不断收到 FileNotFound 异常。我似乎无法解决问题 示例 CSV 文件
12141895, LM051 12148963, lm402 12418954, LM876
用户输入:12141895
期望的结果:真
import javax.swing.*;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;
import javax.swing.*;
import java.io.*;
import java.awt.*;
import java.awt.List;
public class tester
{
public static void main (String [] args ) throws IOException
{
boolean cool = checkValidID();
System.out.println(cool);
}
public static boolean checkValidID() throws IOException
{
boolean results = false;
Scanner scan = new Scanner(new File("C:\\Users\\Packard Bell\\Desktop\\ProjectOOD\\IDandCourse.csv"));
String s;
int indexfound=-1;
String words[] = new String[500];
String word = JOptionPane.showInputDialog("Enter your student ID");
while (scan.hasNextLine())
{
s = scan.nextLine();
if(s.indexOf(word)>-1)
indexfound++;
}
if (indexfound>-1)
{
results = true;
}
else
{
results = true;
}
return results;
}
}
【问题讨论】:
-
请仔细检查文件是否真的存在..
-
检查 new File("C:\\Users\\Packard Bell\\Desktop\\ProjectOOD\\IDandCourse.csv").exists()
-
检查您的文件名。有些人隐藏扩展名并错误地键入“filename.csv.csv”而没有意识到他们的文件已经具有扩展名。