搞砸了; w; (扫描仪sf =新扫描仪(新文件(“c:\\ temp_Name \\ FileName.in”));)代码。 (BaseClass的)

时间:2013-09-07 16:03:19

标签: java filenotfoundexception

因此,此处显示的代码的目标是使我能够创建一个通用类(Shell)来读取/处理包含NumberFormat对象以及StringTokenizer和Scanner对象的文件。

这是我的代码.....

import java.io.File;
import java.io.IOException;
import java.util.StringTokenizer;
import java.util.Scanner;
import java.text.NumberFormat;

 public class BaseClass {
   public static void main (String args[]) throws IOException 
   {
       NumberFormat fmt = NumberFormat.getNumberInstance ();
       fmt.setMinimumFractionDigits(3); //may need to change value
       fmt.setMaximumFractionDigits(3); //may need to change value

       Scanner sf = new Scanner(new File ("c:\\temp_Name\\FileName.in"));
       int maxIndx = -1; // -1 so when we increment below, the first index is 0 
       String text [] = new String[1000]; // to be safe, declare more that we need while
       (sf.hasNext())
       {
           maxIndx++;
           text[maxIndx] = sf.nextLine();
           //System.out.println(text[maxIndx]); // remove rem for testing
       }
       // maxIndx is now the highest index of text []. Equals -1 if no text lines
       sf.close(); // We opened the file above, so close it when finished
       // System.exit (0); // Use this just for testing
       for (int j = 0; j <= maxIndx; j++)
       {
           StringTokenizer st = new StringTokenizer (text[j] ); 
           Scanner sc = new Scanner(text[j]);
           System.out.println(text[j]);
       }


   }

}

堆栈追踪:

Exception in thread "main" java.io.FileNotFoundException: 
    c:\temp_Name\FileName.in (The system cannot find the path specified) 
at java.io.FileInputStream.open(Native Method) 
at java.io.FileInputStream.<init>(FileInputStream.java:138) 
at java.util.Scanner.<init>(Scanner.java:656) 
at BaseClass.main(BaseClass.java:14) 
Java Result: 1 BUILD SUCCESSFUL (total time: 0 seconds) –

1 个答案:

答案 0 :(得分:1)

您必须确保FileName.in确实存在。但我建议您使用Java 7 Path而不是File。