我在Myeclipse里面输入下面代码报错
import java.io.*;
public class file1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
byte buf[]=new byte[1000];
try
{
FileInputStream fis=new FileInputStream("file1.txt");
int rd=fis.read(buf,0,1000);
String str=new String(buf,0,rd);
System.out.println(str);
fis.close();
}C:\Users\IBM_ADMIN\workspace\frame\bin
catch(IOException e)
{
System.out.println("File read Error");
}
}
}
file1.txt放到 C:\Users\IBM_ADMIN\workspace\frame\bin这里了,跟file1.class在一个目录下,有问题吗?
import java.io.*;
public class file1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
byte buf[]=new byte[1000];
try
{
FileInputStream fis=new FileInputStream("file1.txt");
int rd=fis.read(buf,0,1000);
String str=new String(buf,0,rd);
System.out.println(str);
fis.close();
}C:\Users\IBM_ADMIN\workspace\frame\bin
catch(IOException e)
{
System.out.println("File read Error");
}
}
}
file1.txt放到 C:\Users\IBM_ADMIN\workspace\frame\bin这里了,跟file1.class在一个目录下,有问题吗?
System.out.println(str);
fis.close();
}C:\Users\IBM_ADMIN\workspace\frame\bincatch(IOException e)
{
System.out.println("File read Error");
}
这是什么
System.out.println("File read Error") 改成 e.printStackTrace();
把错误发过来。
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at file2frm.<init>(file2.java:35)
at file2.main(file2.java:12)
看来是路径放的不对,在myeclipse里面默认的路径是哪啊?
import java.io.IOException;public class ReadTxtFile {
public static void main(String[] args) {
// TODO Auto-generated method stub
byte buf[]=new byte[1000];
try
{
FileInputStream fis=new FileInputStream("\\file1.txt");
int rd=fis.read(buf,0,1000);
String str=new String(buf,0,rd);
System.out.println(str);
fis.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
把file.txt和你的java类放在一起,或者指定file1.txt的路径:如FileInputStream fis=new FileInputStream("D:\\file1.txt");