import java.io.*;
public class test {
public static void main(String[] args) {
// TODO Auto-generated method stub
int b;
byte buffer[]=new byte[2500];
        try
         {
         FileInputStream readfile=new FileInputStream("test.txt");
         b=readfile.read(buffer,0,2500);
         try
          {
         String str=new String(buffer,0,b,"Default");
         System.out.println(str);
          }
         catch(UnsupportedEncodingException uio){System.out.println("uio error");}
         }
        catch(IOException ioe){System.out.println("ioe error");}
}
}
怎么结果却是ioe error?在线等答

解决方案 »

  1.   

    代码没什么问题,很可能是没有找到文件,你把System.out.println("ioe error");换成ioe.printStackTrace();会看到真正的错误原因
      

  2.   

    是和谁同一目录?
    要和project同一目录哦
      

  3.   

    要在project目录下,或者打包后的jar文件同一目录下
    如果是和.java源文件在一个目录的话,就得用别的方法了
      

  4.   

    修改成
    new FileInputStream("src/test.txt"); 
    试下
      

  5.   

    String str=new String(buffer,0,b,"Default"); \\错
    Default 该成 GB2312或者GBK或者其他任何可以识别的
    没有Default 这个名字的字符集
      

  6.   

    前面就抛出异常了,根本没执行到new String那里
      

  7.   

    抱歉看错了import java.io.*; 
    public class test { 
    public static void main(String[] args) { 
    // TODO Auto-generated method stub 
    int b; 
    byte buffer[]=new byte[2500]; 
            try 
             { 
             FileInputStream readfile=new FileInputStream("test.txt"); 
             b=readfile.read(buffer,0,2500); 
             try 
              { 
             String str=new String(buffer,0,b,"Default"); 
             System.out.println(str); 
              } 
             catch(UnsupportedEncodingException uio){System.out.println("uio error");} 
             } 
            catch(IOException ioe){ioe.printStackTrace();} //这样就知道是不是找不到文件了,应该是找不到文件的问题} 
      

  8.   

    String str=new String(buffer,0,b,"Default"); 这个是教材上的例子而已。运行没有问题。我放绝对路径就可以。但你如果是和.java源文件在一个目录的话,就得用别的方法可能是这个原因。隔了一年没摸java的东西,也不知道它去那里了。给你们加点分吧。谢谢