读取一个file.txt文件中的9,6,8,3,4,1,2,5,0,7整数,并排序  出了点问题
FileInputStream fis = new FileInputStream(new File("E:\\file.txt"));
byte[] b = new byte[1024];
int len = fis.read(b);
String str = new String(b, 0, len);
String[] newstr = str.split(",");
for(int i=0;i<newstr.length;i++){
        String temp = null;
        for(int j=i+1;j<newstr.length;j++){
                if(Integer.parseInt(newstr[i])>(Integer.parseInt(newstr[j]))){
                temp = newstr[i];
                newstr[i] = newstr[j];
                newstr[j] = temp;
                }
        }
}Integer.parseInt(newstr[0])没法转成int,9前面好像还有个东西。
求指教啊

解决方案 »

  1.   

    百度了一下图片会发了
    file里的内容为这个改了下代码打印结果为上面多了个莫名其妙的东西实在搞不定
      

  2.   

    不加|的话是看不出来,但是因为前面多了一个东西,所以在Integer.parseInt(newstr[0])的时候会报NumberFormatException,除了newstr[0],newstr[1-9]都能正常转换。
      

  3.   

    原来是编码的问题,我的TXT文件编码是utf-8的,改成ansi就好了
      

  4.   

    这样就行了:
    import java.io.FileInputStream;
    import java.io.InputStream;public class InputStreamTest1
    {
    public static void main(String[] args) throws Exception
    {
    InputStream is = new FileInputStream("c:/file.txt");

    byte[] buffer = new byte[200];
    int length;

    String temp = null;
    while(-1 != (length = is.read(buffer, 0, 200)))
    {
    String str = new String(buffer,0, length); System.out.println(str);
    }

    is.close();

    }
    }