获得信息的两种方式,将文件转换成inputstream

  1. public static byte[] readInputStream(InputStream inStream) throws Exception{  
  2.         ByteArrayOutputStream outSteam = new ByteArrayOutputStream();  
  3.         byte[] buffer = new byte[4096];  
  4.         int len = 0;  
  5.         while( (len = inStream.read(buffer)) != -1 ){  
  6.             outSteam.write(buffer, 0, len);  
  7.         }  
  8.         outSteam.close();  
  9.         inStream.close();  
  10.         return byte[] data=outSteam.toByteArray();  

  11. 如果有返回string则:
    public string ...
  12. return new String(data);



HttpURLConnection urlConn = (HttpURLConnection) url   
                            .openConnection();// 打开连接,此处只是创建一个实力,并没有真正的连接   
                    urlConn.setDoInput(true);   
                    urlConn.setDoOutput(true);   
                    urlConn.connect();// 连接   
                    InputStream input = urlConn.getInputStream();   
                    InputStreamReader inputReader = new InputStreamReader(input);   
                    BufferedReader reader = new BufferedReader(inputReader);   
                    String inputLine = null;   
                    StringBuffer sb = new StringBuffer();   
                    while ((inputLine = reader.readLine()) != null) {   
                        sb.append(inputLine).append("\n");   
                    }   

                   reader.close();   
                    inputReader.close();   
                    input.close();   
                    urlConn.disconnect();   
                    if(sb !=null){   
                        tv.setText(sb.toString());   
                    }else{   
                        tv.setText("读取的内容:NULL");   
       

       将文件转换成inputstream    
    public static InputStream read_user(String filename,Context context) throws Exception{
        FileInputStream inStream = context.openFileInput(filename);
        byte[] buffer = new byte[1024];
        int len = 0;
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        while( (len = inStream.read(buffer))!= -1){
            outStream.write(buffer, 0, len);
        }
        byte[] data = outStream.toByteArray();//得到文件的二进制数据
        Log.i(TAG, new String(data).toString());
        InputStream is = new ByteArrayInputStream(data);
        outStream.close();
        inStream.close();    
        return is;
    }


public void loadFormSDcard(String filename){

 String content=null;

try{

 File file=new File("/sdcard/filename");

int length=file.length();

byte[] buff=new byte[length];

FileInputStream fis=new FileInputStream(file);

fis.read(buff);

fis.close();

content=new String(buff,"UTF-8");

}catch(){

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值