Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中搜索CSV文件_Java_File_Search_Csv - Fatal编程技术网

在Java中搜索CSV文件

在Java中搜索CSV文件,java,file,search,csv,Java,File,Search,Csv,我试图在一个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; i

我试图在一个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;
}
}
使用:

导入java.io.File

导入java.io.FileNotFoundException

另外,在函数声明中尝试使用

public static boolean checkValidID() throws FileNotFoundException
主功能也是如此。
如果文件存在且名称正确,则应处理该文件。

请仔细检查该文件是否确实存在。检查新文件(“C:\\Users\\Packard Bell\\Desktop\\ProjectOOD\\IDandCourse.csv”)。存在()检查文件名。有些人隐藏扩展名并错误地键入“filename.csv.csv”,而没有意识到他们的文件已经有扩展名。