因此,此处显示的代码的目标是使我可以创建一个通用类(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) –