InputStream文件代码以在任何硬盘驱动器上查找路径

时间:2019-07-08 04:57:23

标签: java.util.scanner fileinputstream hard-drive

我在磁盘C上安装了NetBeans。打开txt的代码有问题,并且无法打开保存在磁盘D上的txt,我不知道是否有代码可以访问另一个硬盘。

使用此路径,代码可以读取文件:

 String path = "C:\\Users\\ch\\Documents\\Test2.txt";

使用此路径,代码无法读取文件:

String pathdiskd = "D:\\Desktop\\Test2.txt";

我正在使用的代码是这样:

 public static LL_Games_Carlos getGames(String path)
 {
    LL_Games_Carlos llg = new LL_Games_Carlos();
    FileInputStream inputStream = null;
    Scanner sc = null;
    String sep = "--------------------------------------------";

    try
    {
        inputStream = new FileInputStream(path);
        sc = new Scanner(inputStream, "UTF-8");
        String line = sc.nextLine();
        String[] s = null;
        String idg = null, ids = null, idt1  = null, idt2 = null, d = null;

        while(sc.hasNextLine())
        {
            while(!line.equals(sep))
            {
                s = line.split("ID Game: ");
                if(s.length>1) idg = s[1];
                line = sc.nextLine();

                s = line.split("ID Stadium: ");
                if(s.length>1) ids = s[1];
                line = sc.nextLine();

                s = line.split("ID Team 1: ");
                if(s.length>1) idt1 = s[1];
                line = sc.nextLine();

                s = line.split("ID Team 2: ");
                if(s.length>1) idt2 = s[1];
                line = sc.nextLine();

                s = line.split("Date Game: ");
                if(s.length>1) d = s[1];
                line = sc.nextLine();
            }
            if(sc.hasNextLine())

                line = sc.nextLine();

            llg.Insert(Integer.parseInt(idg), Integer.parseInt(ids), Integer.parseInt(idt1), Integer.parseInt(idt2), LocalDate.parse(d));
        }
    }

    catch(FileNotFoundException e)
    {
        System.out.println("File not found. MSJ: \n" + e);
    }

    finally
    {
        if(inputStream!=null)
        {
            try
            {
                inputStream.close();
            }

            catch(IOException e)
            {
                System.out.println("IO Exception. MSJ: \n" + e);
            }
        }

        if(sc!=null)
        {
            sc.close();
        }
    }
    return llg;
}

0 个答案:

没有答案