-1

したがって、ここに示すコードの目的は、ファイルの読み取り/処理用の汎用クラス (シェル) を作成できるようにすることです。このクラスには、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) –
4

1 に答える 1

1

FileName.in が実際に存在することを確認する必要があります。ただし、File の代わりに Java 7 Path を使用することをお勧めします。

于 2013-09-07T16:14:32.133 に答える