Here you can find the source of appendToFile(File destFile, InputStream source)
public static void appendToFile(File destFile, InputStream source)
//package com.java2s; /******************************************************************************* * Copyright (c) 2007 cnfree.//from ww w.j a v a2 s. com * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * cnfree - initial API and implementation *******************************************************************************/ import java.io.BufferedReader; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.util.ArrayList; import java.util.List; public class Main { public static void appendToFile(File file, String string) { try { PrintWriter out = new PrintWriter(new OutputStreamWriter(new FileOutputStream(file, true), "GBK")); out.println(); out.println(string); out.close(); } catch (IOException e) { e.printStackTrace(); } } public static void appendToFile(File destFile, InputStream source) { try { int chars_read = 0; BufferedReader in = new BufferedReader(new InputStreamReader(source, "GBK")); List datas = new ArrayList(); while (in.ready()) { char[] data = new char[1024]; chars_read += in.read(data, 0, 1024); datas.add(data); } in.close(); source.close(); char[] v = new char[chars_read]; char[] data = new char[datas.size() * 1024]; for (int i = 0; i < datas.size(); i++) { System.arraycopy((char[]) datas.get(i), 0, data, i * 1024, 1024); } System.arraycopy(data, 0, v, 0, chars_read); String temp = new String(v); PrintWriter out = new PrintWriter(new OutputStreamWriter(new FileOutputStream(destFile, true), "GBK")); out.println(); out.println(temp); out.close(); } catch (IOException e) { e.printStackTrace(); } } }