Java File Append Text appendToFile(File destFile, InputStream source)

Here you can find the source of appendToFile(File destFile, InputStream source)

Description

append To File

License

Open Source License

Declaration

public static void appendToFile(File destFile, InputStream source) 

Method Source Code

//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();
        }
    }
}

Related

  1. appendTextToFile(String file, String text)
  2. appendTextToFile(String file_name, String text)
  3. appendTextToFile(String filename, String data)
  4. AppendTextToFile(String filePath, String text)
  5. appendTextToFile(String text, File file)
  6. appendToFile(File file, String data)
  7. appendToFile(File tempFile, String str)
  8. appendToFile(final File file, final String contents)
  9. appendToFile(final String filePath, final String textToAppend)