`

Java的一些类型转换

 
阅读更多
//String转double
String x = "154.5";
String y = "123.4";
double dx = Double.parseDouble(x);
double dy = Double.parseDouble(y);//String转换为double


//String转int
String x = "15";
        String y = "12";
        int dx = Integer.parseInt(x);
        int dy = Integer.valueOf(y).intValue();  //String转换为int
        Log.i(TAG , ""+ dx+ dy);




//double到Long
Long lUpdateMilis = updateMilis.longValue();//double转long




//inputStream转换成String(此方法效率高些)
 public static String inputStreamToString(InputStream in) throws IOException {
  StringBuffer out = new StringBuffer();
  byte[] b = new byte[4096];
  for (int n; (n = in.read(b)) != -1;) {
   out.append(new String(b, 0, n));
  }
  return out.toString();
 } 
 //inputStream转换成String
 public static String inputStream2String(InputStream is) throws IOException {


  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  int i = -1;
  while ((i = is.read()) != -1) {
   baos.write(i);
  }
  return baos.toString();
 }



分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics