Java OutputStreamWriter Write saveTxt(OutputStream os, List list)

Here you can find the source of saveTxt(OutputStream os, List list)

Description

save Txt

License

Open Source License

Declaration

public static <T> void saveTxt(OutputStream os, List<T> list) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2014, 2015 IBM Corporation
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.//from   www  .j  a  va2 s .com
 *******************************************************************************/

import java.io.BufferedWriter;

import java.io.IOException;

import java.io.OutputStream;
import java.io.OutputStreamWriter;

import java.io.UnsupportedEncodingException;

import java.util.List;

public class Main {
    public static final String strCode = "Shift_JIS";

    public static <T> void saveTxt(OutputStream os, T object) {
        try {
            OutputStreamWriter fw = newOutputStreamWriter(os);
            BufferedWriter bw = new BufferedWriter(fw);
            bw.write(object.toString());
            bw.flush();
            bw.close();
        } catch (IOException e) {
            System.out.println(e);
        }
    }

    public static <T> void saveTxt(OutputStream os, T[] states) {
        try {
            OutputStreamWriter fw = newOutputStreamWriter(os);
            BufferedWriter bw = new BufferedWriter(fw);
            for (T state : states) {
                bw.write(state.toString() + "\n");
            }
            bw.flush();
            bw.close();
        } catch (IOException e) {
            System.out.println(e);
        }
    }

    public static <T> void saveTxt(OutputStream os, List<T> list) {
        try {
            OutputStreamWriter fw = newOutputStreamWriter(os);
            BufferedWriter bw = new BufferedWriter(fw);
            for (T element : list) {
                bw.write(element + "\n");
            }
            bw.flush();
            bw.close();
        } catch (IOException e) {
            System.out.println(e);
        }
    }

    protected static OutputStreamWriter newOutputStreamWriter(OutputStream os) {
        try {
            return new OutputStreamWriter(os, strCode);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }
}

Related

  1. saveStringToFile(String filename, String string)
  2. saveStringToFile(String toSave, String fn)
  3. saveStringToPath(String stringToSave, String pathToSaveTo)
  4. saveText(BufferedWriter writer, String string)
  5. saveTextInFile(File file, String content, String encoding)
  6. saveUnicodeStringToFile(final String string, final File file)
  7. saveUtfFileWithBOM(File file, String content)