DataInputStream in = new DataInputStream(new FileInputStream("f:/temp");
byte[] by=new byte[1024];
int len=0;
String str="";
while((len=in.read(by))>0){
 str=str+new String(by,len);
}

解决方案 »

  1.   

    楼上的那位兄弟说得很对,不过可能实施起来就不对了,不过怎么说呢,毕竟想法是好的^_^。
    我现在在开发一个微型的数据库系统,所以对此比较熟悉,这样的事情我也做过,在此和大家一起分享:FileInputStream fileIn=new FileInputStream(path);
    int ch;
    String fileCon="";
    //这个很重要,千万不可以不初始化,更不可以直接用null来初始化
    while((ch=fileIn.read())>-1)
       fileCont+=(char)ch;
    return fileCon;//这里的内容就是你所要的了。我现在所用的就是这个方法
     
      

  2.   

    这种情况下最好使用StringBuffer来提高效率,很有用的!
      

  3.   

    奇怪的是如果在
    FileInputStream fileIn=new FileInputStream(path);
    int ch;
    String fileCon="";
    在这里如果加一个
    System.out.println("xxxx");
    //这个很重要,千万不可以不初始化,更不可以直接用null来初始化
    while((ch=fileIn.read())>-1)
    fileCont+=(char)ch;在最后filecont中获得的文件内容会缺少第一个字符,怎么回事?
      

  4.   

    一个奇怪的问题。下面代码在运行时会有java.lang.NullPointerException at delprog.delFile2.main(delFile2.java:28)Exception in thread "main" 这样的出错信息。当我不用数组,用普通的变量时运行一切正常。
    RandomAccessFile fileIn=new RandomAccessFile(myfile,"rw");
    String []s=null;
    for (int i=0;i<fileIn.length();i++){
    s[]=fileIn.readLine();
    System.out.println(s[i]);
    }
      

  5.   

    你没有初始化,呵呵……
    定义的不好,可以这样:String s[] = new String(fileIn.length());
    这样试试看,呵呵……