JAVA輸入流 遇到的問題.

        public static List getDoms1(String strUrl){
        strUrl = "http://localhost:8111/test/a.txt";
            URL url = null;
            List list = new ArrayList();
           
            InputStream in = null;
            BufferedInputStream bin;
            InputStreamReader ir = null;
            byte[] data;
            data = new byte[1024];
            StringBuffer sb = new StringBuffer("");
            String str="";
            int n = -1;
            try {
                    url = new URL(strUrl);
                  
                } catch (MalformedURLException e) {
                    // TODO
                     e.printStackTrace();
                }
            try {
               in = url.openStream();
               bin =new BufferedInputStream(in);
              
              
               int i=0;
               while(bin.read(data)!=-1){
                System.out.println(i++);
                sb.append(new String(data,"utf-8"));
                   str = new String(data,"utf-8");  
                   System.out.println(str);
               }
               //.getBytes("GBK"),"utf-8")
            bin.close();
            in.close();//in.disconnect();???
              
            } catch (IOException e) {
                // TODO
                e.printStackTrace();
            }

 

用這個方法的時候,發現從a.txt取出的數據 與 原a.txt不一致,一開始認爲,沒有把a.txt的內容全部讀出來,其實是多讀出了一部分,

原因可能是(沒有認真研究):當最後一次望data數組中寫數據的時候,剩下的這些數據不能填充滿data數組,所以沒有填充滿的部分

還是有上一次填充的數據佔據着.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章