我有如下两个疑问:
疑问一:
比如 11 12 133 1444
用字节流存入数组里
数组中存放的是【1】【1】【空格】【1】【2】【空格】【1】……
如何让数组中是【11】【12】【133】【1444】疑问2:
            FileInputStream fis=new FileInputStream(file);
            byte buffer[]=new byte[1024];
            fis.read(buffer);
我用的是以上代码存入数组,那么在求质数的时候,需要用到循环,如何判断是否已经达到最后一个数字了呢(用的静态数组)求指导,这道题如何用更简洁的方法做出来(限字节流输入输出)

解决方案 »

  1.   

    我很疑惑你既然是数字运算为什么不直接用int来保存,用字节流也就是一次读取4个字节然后转化为int类型。按你的逻辑的话只能先把数字一个一个先转string再在string数组中插入空格,然后把string转为char,再把char转byte存储。
      

  2.   

    将byte数组buffer转成string,用split分割
      

  3.   

    我很疑惑你既然是数字运算为什么不直接用int来保存,用字节流也就是一次读取4个字节然后转化为int类型。按你的逻辑的话只能先把数字一个一个先转string再在string数组中插入空格,然后把string转为char,再把char转byte存储。
    你好,请问一下用int型存储的话,能够有方法直接讲出去空格的数字直接存入数组中吗,我现在只会  先全部存入数组中,再转为字符串,再用split分割,再存入数组,请问一下有更高效的方法吗
      

  4.   


    如果存储时int转byte[4]的话,可以一次读入byte[4],写个方法转int后直接存入数组,也可以用randomaccessfile或datainputsteam直接readint读出int数据,2数据间舍弃一个byte空格符。
      

  5.   

    既然你还是用字符串来操作的话,我觉得你的问题跟用不用字节流没关系,可以直接传化为bufferreader来操作跟简单点。
    FileInputStream fis=new FileInputStream ("test.txt");
    InputStreamReader isr=new InputStreamReader(fis);
    BufferedReader bfr=new BufferedReader(isr);
      

  6.   

    我很疑惑你既然是数字运算为什么不直接用int来保存,用字节流也就是一次读取4个字节然后转化为int类型。按你的逻辑的话只能先把数字一个一个先转string再在string数组中插入空格,然后把string转为char,再把char转byte存储。为啥要String转成char在转成byte,直接转成byte不就行了么
      

  7.   

    确实可以直接转。public class test18 { public static void main(String[] args) {
    // TODO Auto-generated method stub
    try {
    FileOutputStream fos=new FileOutputStream("test.txt");
    int[] i= {11,12,133,1444};
    String[] s=new String[i.length];
    for(int j=0;j<i.length;j++) {
    s[j]=String.valueOf(i[j])+" ";
    }
    for(int j=0;j<i.length;j++) {
    fos.write(s[j].getBytes());
    }
    fos.close();
    } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }}
      

  8.   

    配上读取程序public class test19 { public static void main(String[] args) {
    // TODO Auto-generated method stub
    String str;
    try {
    FileInputStream fis=new FileInputStream("test.txt");
    InputStreamReader isr=new InputStreamReader(fis);
    BufferedReader bfr=new BufferedReader(isr);
    while((str=bfr.readLine())!=null) {
    String[] s=str.split(" ");
    int[] t=new int[s.length];
    for(int i=0;i<s.length;i++) {
    t[i]=Integer.valueOf(s[i]);
    System.out.println(t[i]);
    }
    }
    bfr.close();
    isr.close();
    fis.close();
    } catch (NumberFormatException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }}
    显示结果11
    12
    133
    1444