Java 搞砸了;W(Scanner sf=new Scanner(新文件(";c:\\temp\u Name\\FileName.in";);)代码。(基类)
因此,这里显示的代码的目标是创建一个泛型类(Shell),用于读取/处理文件,该类还包括NumberFormat对象以及StringTokenizer和Scanner对象 这是我的代码Java 搞砸了;W(Scanner sf=new Scanner(新文件(";c:\\temp\u Name\\FileName.in";);)代码。(基类),java,filenotfoundexception,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 Ba
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]);
}
}
导入java.io.File;
导入java.io.IOException;
导入java.util.StringTokenizer;
导入java.util.Scanner;
导入java.text.NumberFormat;
公共类基类{
公共静态void main(字符串args[])引发IOException
{
NumberFormat fmt=NumberFormat.getNumberInstance();
fmt.setMinimumFractionDigits(3);//可能需要更改值
fmt.setMaximumFractionDigits(3);//可能需要更改值
扫描仪sf=新扫描仪(新文件(“c:\\temp\u Name\\FileName.in”);
int maxIndx=-1;//-1,所以当我们在下面递增时,第一个索引是0
String text[]=新字符串[1000];//为了安全起见,在
(sf.hasNext())
{
maxIndx++;
text[maxIndx]=sf.nextLine();
//System.out.println(text[maxIndx]);//删除rem进行测试
}
//maxIndx现在是文本[]的最高索引。如果没有文本行,则等于-1
sf.close();//我们打开了上面的文件,所以完成后关闭它
//System.exit(0);//仅用于测试
对于(int j=0;j您必须确保FileName.in确实存在。但我建议您使用Java 7路径而不是文件。它在线程“main”Java.io.FileNotFoundException:c:\temp\u Name\FileName.in(系统找不到指定的路径)Java.io.FileInputStream.open(本机方法)Java.io.FileInputStream。(FileInputStream.java:138)位于java.util.Scanner。(Scanner.java:656)位于BaseClass.main(BaseClass.java:14)java结果:1构建成功(总时间:0秒)显然,您在该路径上引用的文件不存在。我没有运行Windows,因此无法证明这一点,但也许可以尝试利用C驱动器。请浏览到文件路径“C:\temp\u Name\”。您在那里看到您的文件了吗?文件名是否完全相同?@Mena:您的直觉可能是正确的,但Windows不是区分大小写的平台。
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) –