当前位置: 技术问答>java相关
java.io.FileNotFoundException
来源: 互联网 发布时间:2015-01-21
本文导语: 错误: java.io.FileNotFoundException: fileinput.java (文件名、目录名或卷标语法不正确。) 源文件:fileinput.java import java.io.*; public class fileinput { public static void main(String args[]) {byte b1[]=new byte[255]; byte b3[]=new byte[...
错误:
java.io.FileNotFoundException: fileinput.java
(文件名、目录名或卷标语法不正确。)
源文件:fileinput.java
import java.io.*;
public class fileinput
{
public static void main(String args[])
{byte b1[]=new byte[255];
byte b3[]=new byte[2056];
try
{System.out.println("请输入源文件名称:n");
int by1=System.in.read(b1,0,255);
String sname=new String(b1,0,by1);
FileInputStream fileinput=new FileInputStream(sname);
int bytes1=fileinput.read(b3,0,2056);
String sfile=new String(b3,0,bytes1);
System.out.println("n源文件:n"+sfile);
}
catch(Exception e)
{System.out.println(e.toString());
}
}
}
java.io.FileNotFoundException: fileinput.java
(文件名、目录名或卷标语法不正确。)
源文件:fileinput.java
import java.io.*;
public class fileinput
{
public static void main(String args[])
{byte b1[]=new byte[255];
byte b3[]=new byte[2056];
try
{System.out.println("请输入源文件名称:n");
int by1=System.in.read(b1,0,255);
String sname=new String(b1,0,by1);
FileInputStream fileinput=new FileInputStream(sname);
int bytes1=fileinput.read(b3,0,2056);
String sfile=new String(b3,0,bytes1);
System.out.println("n源文件:n"+sfile);
}
catch(Exception e)
{System.out.println(e.toString());
}
}
}
|
问题在这里:
FileInputStream fileinput=new FileInputStream(sname.trim());
文件名输入时:
d:\fileinput.java
FileInputStream fileinput=new FileInputStream(sname.trim());
文件名输入时:
d:\fileinput.java
|
你输入文件路径和文件名后,系统自动加上了换行和回车符,所以by1的长度也比实际长度多了2,你可以试试:
int by1=System.in.read(b1,0,255);
System.out.println(by1);
by1 = by1 - 2;
String sname = new String(b1,0,by1);
int by1=System.in.read(b1,0,255);
System.out.println(by1);
by1 = by1 - 2;
String sname = new String(b1,0,by1);
|
import java.io.*;
public class fileinput
{
public static void main(String args[])
{
byte b1[]=new byte[255];
byte b3[]=new byte[2056];
try
{
System.out.println("请输入源文件名称:n");
// int by1=System.in.read(b1,0,255);
// String sname=new String(b1,0,by1);
String sname = "c:\hxf\aaa.txt"; // 注意!!!
FileInputStream fileinput=new FileInputStream(sname);
int bytes1=fileinput.read(b3,0,2056);
String sfile=new String(b3,0,bytes1);
System.out.println("n源文件:n"+sfile);
}
catch(Exception e)
{
System.out.println(e.toString());
}
}
最好用FileDialog 得到路径
}
public class fileinput
{
public static void main(String args[])
{
byte b1[]=new byte[255];
byte b3[]=new byte[2056];
try
{
System.out.println("请输入源文件名称:n");
// int by1=System.in.read(b1,0,255);
// String sname=new String(b1,0,by1);
String sname = "c:\hxf\aaa.txt"; // 注意!!!
FileInputStream fileinput=new FileInputStream(sname);
int bytes1=fileinput.read(b3,0,2056);
String sfile=new String(b3,0,bytes1);
System.out.println("n源文件:n"+sfile);
}
catch(Exception e)
{
System.out.println(e.toString());
}
}
最好用FileDialog 得到路径
}
|
问题在于没有指定要打开文件的目录,如果要查找的文件和CLASS文件是在同一个目下,可以用System.getProperty("user.dir");取得当前路径,然后再进行操作