Java tutorial
//package com.java2s; /* * This file is part of OpenVPN-Settings. * * Copyright 2009-2012 Friedrich Schuffelhut * * OpenVPN-Settings is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * OpenVPN-Settings is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with OpenVPN-Settings. If not, see <http://www.gnu.org/licenses/>. * * Report bugs or new features at: http://code.google.com/p/android-openvpn-settings/ * Contact the author at: android.openvpn@schaeuffelhut.de */ import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.Reader; import java.io.Writer; import java.net.Socket; import android.content.res.AssetFileDescriptor; import android.content.res.AssetManager; import android.net.LocalSocket; import android.util.Log; public class Main { public final static void copyAsset(String tag, AssetManager assets, String assetName, File target) { if (target.exists()) { Log.d(tag, String.format("asset %s already installed at %s", assetName, target)); } else { Log.d(tag, String.format("copying asset %s to %s", assetName, target)); InputStream is = null; OutputStream os = null; try { is = assets.open(assetName); os = new FileOutputStream(target); byte[] buffer = new byte[1024]; int count; while ((count = is.read(buffer)) != -1) os.write(buffer, 0, count); } catch (IOException e) { Log.e(tag, "copy", e); } finally { closeQuietly(is); closeQuietly(os); } } } public final static void closeQuietly(InputStream is) { try { if (is != null) is.close(); } catch (Exception e) { Log.e("OpenVPN", "closing InputStream", e); } } public final static void closeQuietly(AssetFileDescriptor afd) { try { if (afd != null) afd.close(); } catch (Exception e) { Log.e("OpenVPN", "closing AssetFileDescriptor", e); } } public final static void closeQuietly(Reader r) { try { if (r != null) r.close(); } catch (Exception e) { Log.e("OpenVPN", "closing InputStream", e); } } public final static void closeQuietly(OutputStream os) { try { if (os != null) os.close(); } catch (Exception e) { Log.e("OpenVPN", "closing OutputStream", e); } } public final static void closeQuietly(Writer writer) { try { if (writer != null) writer.close(); } catch (Exception e) { Log.e("OpenVPN", "closing Writer", e); } } public final static void closeQuietly(Socket s) { try { if (s != null) s.close(); } catch (Exception e) { Log.e("OpenVPN", "closing Socket", e); } } public final static void closeQuietly(LocalSocket s) { try { if (s != null) s.close(); } catch (Exception e) { Log.e("OpenVPN", "closing LocalSocket", e); } } }