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 得到路径
}

解决方案 »

  1.   

    问题在于没有指定要打开文件的目录,如果要查找的文件和CLASS文件是在同一个目下,可以用System.getProperty("user.dir");取得当前路径,然后再进行操作
      

  2.   

    问题在这里:
    FileInputStream fileinput=new FileInputStream(sname.trim());
    文件名输入时:
    d:\\fileinput.java
          
      

  3.   

    bobosji(波波司机):谢谢!
    但是不用 trim 而用
    int by1=System.in.read(b1,0,255);
    String sname=new String(b1,0,by1);
    好像也没错吧
      

  4.   

    你输入文件路径和文件名后,系统自动加上了换行和回车符,所以by1的长度也比实际长度多了2,你可以试试:
          int by1=System.in.read(b1,0,255);
          System.out.println(by1);
          by1 = by1 - 2;
          String sname = new String(b1,0,by1);