Detailed explanation of 10 common tool class example codes in Android rapid development series

  • 2021-10-16 02:44:44
  • OfStack

Open everyone's projects, there will be a large number of basic auxiliary classes. Today, I hereby sort out 10 basic tool classes that will be used in each project for rapid development ~ ~ Thank you for sending me the brothers/sisters of tool classes in the project ~

1. Logging tool class L. java


package com.zhy.utils;

import android.util.Log;
/**
 * Log Unified 1 Management class 
 * 
 * 
 * 
 */
public class L
{
 private L()
 {
 /* cannot be instantiated */
 throw new UnsupportedOperationException("cannot be instantiated");
 }
 public static boolean isDebug = true;//  Do you need to print bug , can be found in application Adj. onCreate Function inside the initialization 
 private static final String TAG = "way";
 //  Below 4 One is the default tag Function of 
 public static void i(String msg)
 {
 if (isDebug)
 Log.i(TAG, msg);
 }
 public static void d(String msg)
 {
 if (isDebug)
 Log.d(TAG, msg);
 }
 public static void e(String msg)
 {
 if (isDebug)
 Log.e(TAG, msg);
 }
 public static void v(String msg)
 {
 if (isDebug)
 Log.v(TAG, msg);
 }
 //  The following is the incoming customization tag Function of 
 public static void i(String tag, String msg)
 {
 if (isDebug)
 Log.i(tag, msg);
 }
 public static void d(String tag, String msg)
 {
 if (isDebug)
 Log.i(tag, msg);
 }
 public static void e(String tag, String msg)
 {
 if (isDebug)
 Log.i(tag, msg);
 }
 public static void v(String tag, String msg)
 {
 if (isDebug)
 Log.i(tag, msg);
 }
}

The class seen on the Internet should be the name of the original author on the annotation, which is a simple class; There are also a lot of online logging to SDCard, but I have never recorded, so the introduction of the simplest, you can evaluate whether it needs to expand ~ ~

2. Toast System 1 Management Class


package com.zhy.utils;

import android.content.Context;
import android.widget.Toast;
/**
 * Toast Unified 1 Management class 
 * 
 */
public class T
{
 private T()
 {
 /* cannot be instantiated */
 throw new UnsupportedOperationException("cannot be instantiated");
 }
 public static boolean isShow = true;
 /**
 *  Short time display Toast
 * 
 * @param context
 * @param message
 */
 public static void showShort(Context context, CharSequence message)
 {
 if (isShow)
 Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
 }
 /**
 *  Short time display Toast
 * 
 * @param context
 * @param message
 */
 public static void showShort(Context context, int message)
 {
 if (isShow)
 Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
 }
 /**
 *  Display for a long time Toast
 * 
 * @param context
 * @param message
 */
 public static void showLong(Context context, CharSequence message)
 {
 if (isShow)
 Toast.makeText(context, message, Toast.LENGTH_LONG).show();
 }
 /**
 *  Display for a long time Toast
 * 
 * @param context
 * @param message
 */
 public static void showLong(Context context, int message)
 {
 if (isShow)
 Toast.makeText(context, message, Toast.LENGTH_LONG).show();
 }
 /**
 *  Custom display Toast Time 
 * 
 * @param context
 * @param message
 * @param duration
 */
 public static void show(Context context, CharSequence message, int duration)
 {
 if (isShow)
 Toast.makeText(context, message, duration).show();
 }
 /**
 *  Custom display Toast Time 
 * 
 * @param context
 * @param message
 * @param duration
 */
 public static void show(Context context, int message, int duration)
 {
 if (isShow)
 Toast.makeText(context, message, duration).show();
 }
}

It is also a very simple package, which can save ~ ~

3. SharedPreferences encapsulation class SPUtils


package com.zhy.utils;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;
import android.content.Context;
import android.content.SharedPreferences;
public class SPUtils
{
 /**
 *  File name saved in mobile phone 
 */
 public static final String FILE_NAME = "share_data";
 /**
 *  The method of saving data, we need to get the specific type of saving data, and then call different saving methods according to the type 
 * 
 * @param context
 * @param key
 * @param object
 */
 public static void put(Context context, String key, Object object)
 {
 SharedPreferences sp = context.getSharedPreferences(FILE_NAME,
 Context.MODE_PRIVATE);
 SharedPreferences.Editor editor = sp.edit();
 if (object instanceof String)
 {
 editor.putString(key, (String) object);
 } else if (object instanceof Integer)
 {
 editor.putInt(key, (Integer) object);
 } else if (object instanceof Boolean)
 {
 editor.putBoolean(key, (Boolean) object);
 } else if (object instanceof Float)
 {
 editor.putFloat(key, (Float) object);
 } else if (object instanceof Long)
 {
 editor.putLong(key, (Long) object);
 } else
 {
 editor.putString(key, object.toString());
 }
 SharedPreferencesCompat.apply(editor);
 }
 /**
 *  To get the method to save the data, we get the specific type of saved data according to the default value, and then call the relative method to get the value 
 * 
 * @param context
 * @param key
 * @param defaultObject
 * @return
 */
 public static Object get(Context context, String key, Object defaultObject)
 {
 SharedPreferences sp = context.getSharedPreferences(FILE_NAME,
 Context.MODE_PRIVATE);
 if (defaultObject instanceof String)
 {
 return sp.getString(key, (String) defaultObject);
 } else if (defaultObject instanceof Integer)
 {
 return sp.getInt(key, (Integer) defaultObject);
 } else if (defaultObject instanceof Boolean)
 {
 return sp.getBoolean(key, (Boolean) defaultObject);
 } else if (defaultObject instanceof Float)
 {
 return sp.getFloat(key, (Float) defaultObject);
 } else if (defaultObject instanceof Long)
 {
 return sp.getLong(key, (Long) defaultObject);
 }
 return null;
 }
 /**
 *  Remove a key The value already corresponds to the value 
 * @param context
 * @param key
 */
 public static void remove(Context context, String key)
 {
 SharedPreferences sp = context.getSharedPreferences(FILE_NAME,
 Context.MODE_PRIVATE);
 SharedPreferences.Editor editor = sp.edit();
 editor.remove(key);
 SharedPreferencesCompat.apply(editor);
 }
 /**
 *  Clear all data 
 * @param context
 */
 public static void clear(Context context)
 {
 SharedPreferences sp = context.getSharedPreferences(FILE_NAME,
 Context.MODE_PRIVATE);
 SharedPreferences.Editor editor = sp.edit();
 editor.clear();
 SharedPreferencesCompat.apply(editor);
 }
 /**
 *  Query a key Does it already exist 
 * @param context
 * @param key
 * @return
 */
 public static boolean contains(Context context, String key)
 {
 SharedPreferences sp = context.getSharedPreferences(FILE_NAME,
 Context.MODE_PRIVATE);
 return sp.contains(key);
 }
 /**
 *  Returns all key-value pairs 
 * 
 * @param context
 * @return
 */
 public static Map<String, ?> getAll(Context context)
 {
 SharedPreferences sp = context.getSharedPreferences(FILE_NAME,
 Context.MODE_PRIVATE);
 return sp.getAll();
 }
 /**
 *  Create 1 A solution SharedPreferencesCompat.apply Method's 1 Compatible classes 
 * 
 * @author zhy
 * 
 */
 private static class SharedPreferencesCompat
 {
 private static final Method sApplyMethod = findApplyMethod();
 /**
 *  Reflection lookup apply Method of 
 * 
 * @return
 */
 @SuppressWarnings({ "unchecked", "rawtypes" })
 private static Method findApplyMethod()
 {
 try
 {
 Class clz = SharedPreferences.Editor.class;
 return clz.getMethod("apply");
 } catch (NoSuchMethodException e)
 {
 }
 return null;
 }
 /**
 *  If found, use the apply Execute, otherwise use the commit
 * 
 * @param editor
 */
 public static void apply(SharedPreferences.Editor editor)
 {
 try
 {
 if (sApplyMethod != null)
 {
  sApplyMethod.invoke(editor);
  return;
 }
 } catch (IllegalArgumentException e)
 {
 } catch (IllegalAccessException e)
 {
 } catch (InvocationTargetException e)
 {
 }
 editor.commit();
 }
 }
}

The use of SharedPreference is recommended for packaging, and put, get, remove, clear and other methods are announced to the outside world; Note 1: All commit operations are replaced by SharedPreferencesCompat. apply. The purpose is to use apply instead of commit as much as possible. First of all, why, because commit method is synchronous, and most of our commit operations are in UI threads. After all, it is IO operations, which are asynchronous as much as possible; So we use apply instead, and apply writes asynchronously. But apply is equivalent to commit, which is new API. For better compatibility, we have made adaptation; SharedPreferencesCompat can also provide a reference for everyone to create compatible classes ~ ~

4. Unit conversion class DensityUtils


package com.zhy.utils;

import android.content.Context;
import android.util.TypedValue;
/**
 *  Helper classes for common unit conversions 
 * 
 * 
 * 
 */
public class DensityUtils
{
 private DensityUtils()
 {
 /* cannot be instantiated */
 throw new UnsupportedOperationException("cannot be instantiated");
 }
 /**
 * dp Turn px
 * 
 * @param context
 * @param val
 * @return
 */
 public static int dp2px(Context context, float dpVal)
 {
 return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
 dpVal, context.getResources().getDisplayMetrics());
 }
 /**
 * sp Turn px
 * 
 * @param context
 * @param val
 * @return
 */
 public static int sp2px(Context context, float spVal)
 {
 return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,
 spVal, context.getResources().getDisplayMetrics());
 }
 /**
 * px Turn dp
 * 
 * @param context
 * @param pxVal
 * @return
 */
 public static float px2dp(Context context, float pxVal)
 {
 final float scale = context.getResources().getDisplayMetrics().density;
 return (pxVal / scale);
 }
 /**
 * px Turn sp
 * 
 * @param fontScale
 * @param pxVal
 * @return
 */
 public static float px2sp(Context context, float pxVal)
 {
 return (pxVal / context.getResources().getDisplayMetrics().scaledDensity);
 }
}

5. SD card related auxiliary class SDCardUtils


package com.zhy.utils;

import java.io.File;
import android.os.Environment;
import android.os.StatFs;
/**
 * SD Card-related auxiliary classes 
 * 
 * 
 * 
 */
public class SDCardUtils
{
 private SDCardUtils()
 {
 /* cannot be instantiated */
 throw new UnsupportedOperationException("cannot be instantiated");
 }
 /**
 *  Judge SDCard Available 
 * 
 * @return
 */
 public static boolean isSDCardEnable()
 {
 return Environment.getExternalStorageState().equals(
 Environment.MEDIA_MOUNTED);
 }
 /**
 *  Get SD Card path 
 * 
 * @return
 */
 public static String getSDCardPath()
 {
 return Environment.getExternalStorageDirectory().getAbsolutePath()
 + File.separator;
 }
 /**
 *  Get SD Remaining capacity of card   Unit byte
 * 
 * @return
 */
 public static long getSDCardAllSize()
 {
 if (isSDCardEnable())
 {
 StatFs stat = new StatFs(getSDCardPath());
 //  Gets the number of free data blocks 
 long availableBlocks = (long) stat.getAvailableBlocks() - 4;
 //  Gets the size of a single data block ( byte ) 
 long freeBlocks = stat.getAvailableBlocks();
 return freeBlocks * availableBlocks;
 }
 return 0;
 }
 /**
 *  Gets the number of bytes, in units, of available capacity remaining in the space where the specified path is located byte
 * 
 * @param filePath
 * @return  Capacity byte  SDCard Free space, free space for internal storage 
 */
 public static long getFreeBytes(String filePath)
 {
 //  If it is sd The path under the card is obtained sd Available capacity of card 
 if (filePath.startsWith(getSDCardPath()))
 {
 filePath = getSDCardPath();
 } else
 {//  If it is the path of internal storage, get the available capacity of memory storage 
 filePath = Environment.getDataDirectory().getAbsolutePath();
 }
 StatFs stat = new StatFs(filePath);
 long availableBlocks = (long) stat.getAvailableBlocks() - 4;
 return stat.getBlockSize() * availableBlocks;
 }
 /**
 *  Get the system storage path 
 * 
 * @return
 */
 public static String getRootDirectoryPath()
 {
 return Environment.getRootDirectory().getAbsolutePath();
 }
}

6. Screen-related auxiliary class ScreenUtils


package com.zhy.utils;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.WindowManager;
/**
 *  Get screen-related helper classes 
 * 
 * 
 * 
 */
public class ScreenUtils
{
 private ScreenUtils()
 {
 /* cannot be instantiated */
 throw new UnsupportedOperationException("cannot be instantiated");
 }
 /**
 *  Get screen height 
 * 
 * @param context
 * @return
 */
 public static int getScreenWidth(Context context)
 {
 WindowManager wm = (WindowManager) context
 .getSystemService(Context.WINDOW_SERVICE);
 DisplayMetrics outMetrics = new DisplayMetrics();
 wm.getDefaultDisplay().getMetrics(outMetrics);
 return outMetrics.widthPixels;
 }
 /**
 *  Get screen width 
 * 
 * @param context
 * @return
 */
 public static int getScreenHeight(Context context)
 {
 WindowManager wm = (WindowManager) context
 .getSystemService(Context.WINDOW_SERVICE);
 DisplayMetrics outMetrics = new DisplayMetrics();
 wm.getDefaultDisplay().getMetrics(outMetrics);
 return outMetrics.heightPixels;
 }
 /**
 *  Get the height of the status bar 
 * 
 * @param context
 * @return
 */
 public static int getStatusHeight(Context context)
 {
 int statusHeight = -1;
 try
 {
 Class<?> clazz = Class.forName("com.android.internal.R$dimen");
 Object object = clazz.newInstance();
 int height = Integer.parseInt(clazz.getField("status_bar_height")
  .get(object).toString());
 statusHeight = context.getResources().getDimensionPixelSize(height);
 } catch (Exception e)
 {
 e.printStackTrace();
 }
 return statusHeight;
 }
 /**
 *  Get a screenshot of the current screen, including the status bar 
 * 
 * @param activity
 * @return
 */
 public static Bitmap snapShotWithStatusBar(Activity activity)
 {
 View view = activity.getWindow().getDecorView();
 view.setDrawingCacheEnabled(true);
 view.buildDrawingCache();
 Bitmap bmp = view.getDrawingCache();
 int width = getScreenWidth(activity);
 int height = getScreenHeight(activity);
 Bitmap bp = null;
 bp = Bitmap.createBitmap(bmp, 0, 0, width, height);
 view.destroyDrawingCache();
 return bp;
 }
 /**
 *  Get a screenshot of the current screen without the status bar 
 * 
 * @param activity
 * @return
 */
 public static Bitmap snapShotWithoutStatusBar(Activity activity)
 {
 View view = activity.getWindow().getDecorView();
 view.setDrawingCacheEnabled(true);
 view.buildDrawingCache();
 Bitmap bmp = view.getDrawingCache();
 Rect frame = new Rect();
 activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
 int statusBarHeight = frame.top;
 int width = getScreenWidth(activity);
 int height = getScreenHeight(activity);
 Bitmap bp = null;
 bp = Bitmap.createBitmap(bmp, 0, statusBarHeight, width, height
 - statusBarHeight);
 view.destroyDrawingCache();
 return bp;
 }
}

7. App related auxiliary classes


package com.zhy.utils;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
/**
 *  Follow App Related helper classes 
 * 
 * 
 * 
 */
public class AppUtils
{
 private AppUtils()
 {
 /* cannot be instantiated */
 throw new UnsupportedOperationException("cannot be instantiated");
 }
 /**
 *  Get the application name 
 */
 public static String getAppName(Context context)
 {
 try
 {
 PackageManager packageManager = context.getPackageManager();
 PackageInfo packageInfo = packageManager.getPackageInfo(
  context.getPackageName(), 0);
 int labelRes = packageInfo.applicationInfo.labelRes;
 return context.getResources().getString(labelRes);
 } catch (NameNotFoundException e)
 {
 e.printStackTrace();
 }
 return null;
 }
 /**
 * [ Get application version name information ]
 * 
 * @param context
 * @return  The currently applied version name 
 */
 public static String getVersionName(Context context)
 {
 try
 {
 PackageManager packageManager = context.getPackageManager();
 PackageInfo packageInfo = packageManager.getPackageInfo(
  context.getPackageName(), 0);
 return packageInfo.versionName;
 } catch (NameNotFoundException e)
 {
 e.printStackTrace();
 }
 return null;
 }
}

8. Soft keyboard related auxiliary class KeyBoardUtils


package com.zhy.utils;

import android.content.Context;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
/**
 *  Turn the soft keyboard on or off 
 * 
 * @author zhy
 * 
 */
public class KeyBoardUtils
{
 /**
 *  Clock-in soft keyboard 
 * 
 * @param mEditText
 *    Input box 
 * @param mContext
 *    Context 
 */
 public static void openKeybord(EditText mEditText, Context mContext)
 {
 InputMethodManager imm = (InputMethodManager) mContext
 .getSystemService(Context.INPUT_METHOD_SERVICE);
 imm.showSoftInput(mEditText, InputMethodManager.RESULT_SHOWN);
 imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,
 InputMethodManager.HIDE_IMPLICIT_ONLY);
 }
 /**
 *  Turn off the soft keyboard 
 * 
 * @param mEditText
 *    Input box 
 * @param mContext
 *    Context 
 */
 public static void closeKeybord(EditText mEditText, Context mContext)
 {
 InputMethodManager imm = (InputMethodManager) mContext
 .getSystemService(Context.INPUT_METHOD_SERVICE);
 imm.hideSoftInputFromWindow(mEditText.getWindowToken(), 0);
 }
}

9. Network-related auxiliary class NetUtils


package com.zhy.utils;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
/**
 *  Network-related tool classes 
 * 
 * 
 * 
 */
public class NetUtils
{
 private NetUtils()
 {
 /* cannot be instantiated */
 throw new UnsupportedOperationException("cannot be instantiated");
 }
 /**
 *  Determine whether the network is connected or not 
 * 
 * @param context
 * @return
 */
 public static boolean isConnected(Context context)
 {
 ConnectivityManager connectivity = (ConnectivityManager) context
 .getSystemService(Context.CONNECTIVITY_SERVICE);
 if (null != connectivity)
 {
 NetworkInfo info = connectivity.getActiveNetworkInfo();
 if (null != info && info.isConnected())
 {
 if (info.getState() == NetworkInfo.State.CONNECTED)
 {
  return true;
 }
 }
 }
 return false;
 }
 /**
 *  Determine whether it is wifi Connect 
 */
 public static boolean isWifi(Context context)
 {
 ConnectivityManager cm = (ConnectivityManager) context
 .getSystemService(Context.CONNECTIVITY_SERVICE);
 if (cm == null)
 return false;
 return cm.getActiveNetworkInfo().getType() == ConnectivityManager.TYPE_WIFI;
 }
 /**
 *  Open the network setting interface 
 */
 public static void openSetting(Activity activity)
 {
 Intent intent = new Intent("/");
 ComponentName cm = new ComponentName("com.android.settings",
 "com.android.settings.WirelessSettings");
 intent.setComponent(cm);
 intent.setAction("android.intent.action.VIEW");
 activity.startActivityForResult(intent, 0);
 }
}

10. Http related auxiliary class HttpUtils


package com.zhy.utils;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
/**
 * Http Requested tool class 
 * 
 * @author zhy
 * 
 */
public class HttpUtils
{
 private static final int TIMEOUT_IN_MILLIONS = 5000;
 public interface CallBack
 {
 void onRequestComplete(String result);
 }
 /**
 *  Asynchronous Get Request 
 * 
 * @param urlStr
 * @param callBack
 */
 public static void doGetAsyn(final String urlStr, final CallBack callBack)
 {
 new Thread()
 {
 public void run()
 {
 try
 {
  String result = doGet(urlStr);
  if (callBack != null)
  {
  callBack.onRequestComplete(result);
  }
 } catch (Exception e)
 {
  e.printStackTrace();
 }
 };
 }.start();
 }
 /**
 *  Asynchronous Post Request 
 * @param urlStr
 * @param params
 * @param callBack
 * @throws Exception
 */
 public static void doPostAsyn(final String urlStr, final String params,
 final CallBack callBack) throws Exception
 {
 new Thread()
 {
 public void run()
 {
 try
 {
  String result = doPost(urlStr, params);
  if (callBack != null)
  {
  callBack.onRequestComplete(result);
  }
 } catch (Exception e)
 {
  e.printStackTrace();
 }
 };
 }.start();
 }
 /**
 * Get Request, get the returned data 
 * 
 * @param urlStr
 * @return
 * @throws Exception
 */
 public static String doGet(String urlStr) 
 {
 URL url = null;
 HttpURLConnection conn = null;
 InputStream is = null;
 ByteArrayOutputStream baos = null;
 try
 {
 url = new URL(urlStr);
 conn = (HttpURLConnection) url.openConnection();
 conn.setReadTimeout(TIMEOUT_IN_MILLIONS);
 conn.setConnectTimeout(TIMEOUT_IN_MILLIONS);
 conn.setRequestMethod("GET");
 conn.setRequestProperty("accept", "*/*");
 conn.setRequestProperty("connection", "Keep-Alive");
 if (conn.getResponseCode() == 200)
 {
 is = conn.getInputStream();
 baos = new ByteArrayOutputStream();
 int len = -1;
 byte[] buf = new byte[128];
 while ((len = is.read(buf)) != -1)
 {
  baos.write(buf, 0, len);
 }
 baos.flush();
 return baos.toString();
 } else
 {
 throw new RuntimeException(" responseCode is not 200 ... ");
 }
 } catch (Exception e)
 {
 e.printStackTrace();
 } finally
 {
 try
 {
 if (is != null)
  is.close();
 } catch (IOException e)
 {
 }
 try
 {
 if (baos != null)
  baos.close();
 } catch (IOException e)
 {
 }
 conn.disconnect();
 }
 
 return null ;
 }
 /**
 *  To the specified  URL  Send POST Request for the method 
 * 
 * @param url
 *    Sending the requested  URL
 * @param param
 *    Request parameter, which should be  name1=value1&name2=value2  The form of. 
 * @return  The response result of the remote resource represented by 
 * @throws Exception
 */
 public static String doPost(String url, String param) 
 {
 PrintWriter out = null;
 BufferedReader in = null;
 String result = "";
 try
 {
 URL realUrl = new URL(url);
 //  Open and URL Connection between 
 HttpURLConnection conn = (HttpURLConnection) realUrl
  .openConnection();
 //  Setting Common Request Properties 
 conn.setRequestProperty("accept", "*/*");
 conn.setRequestProperty("connection", "Keep-Alive");
 conn.setRequestMethod("POST");
 conn.setRequestProperty("Content-Type",
  "application/x-www-form-urlencoded");
 conn.setRequestProperty("charset", "utf-8");
 conn.setUseCaches(false);
 //  Send POST The request must be set with the following two lines 
 conn.setDoOutput(true);
 conn.setDoInput(true);
 conn.setReadTimeout(TIMEOUT_IN_MILLIONS);
 conn.setConnectTimeout(TIMEOUT_IN_MILLIONS);
 if (param != null && !param.trim().equals(""))
 {
 //  Get URLConnection The output stream corresponding to the object 
 out = new PrintWriter(conn.getOutputStream());
 //  Send request parameters 
 out.print(param);
 // flush Buffering of output stream 
 out.flush();
 }
 //  Definition BufferedReader Input stream to read URL Response of 
 in = new BufferedReader(
  new InputStreamReader(conn.getInputStream()));
 String line;
 while ((line = in.readLine()) != null)
 {
 result += line;
 }
 } catch (Exception e)
 {
 e.printStackTrace();
 }
 //  Use finally Block to close the output stream, the input stream 
 finally
 {
 try
 {
 if (out != null)
 {
  out.close();
 }
 if (in != null)
 {
  in.close();
 }
 } catch (IOException ex)
 {
 ex.printStackTrace();
 }
 }
 return result;
 }
}

Summarize


Related articles: