/** * @deprecated Use instead {@link org.apache.commons.io.IOUtils#writeLines(java.util.Collection, String, java.io.OutputStream)} */ @Deprecated public static void writeLines(Collection lines, String lineEnding, OutputStream output) throws IOException { org.apache.commons.io.IOUtils.writeLines(lines, lineEnding, output); }
/** * @deprecated Use instead {@link org.apache.commons.io.IOUtils#writeLines(java.util.Collection, String, java.io.Writer)} */ @Deprecated public static void writeLines(Collection lines, String lineEnding, Writer writer) throws IOException { org.apache.commons.io.IOUtils.writeLines(lines, lineEnding, writer); }
/** * @deprecated Use instead {@link org.apache.commons.io.IOUtils#writeLines(java.util.Collection, String, java.io.OutputStream, String)} */ @Deprecated public static void writeLines(Collection lines, String lineEnding, OutputStream output, String encoding) throws IOException { org.apache.commons.io.IOUtils.writeLines(lines, lineEnding, output, encoding); }
/** * Writes the <code>toString()</code> value of each item in a collection to * an <code>OutputStream</code> line by line, using the default character * encoding of the platform and the specified line ending. * * @param lines the lines to write, null entries produce blank lines * @param lineEnding the line separator to use, null is system default * @param output the <code>OutputStream</code> to write to, not null, not closed * @throws NullPointerException if the output is null * @throws IOException if an I/O error occurs * @since 1.1 * @deprecated 2.5 use {@link #writeLines(Collection, String, OutputStream, Charset)} instead */ @Deprecated public static void writeLines(final Collection<?> lines, final String lineEnding, final OutputStream output) throws IOException { writeLines(lines, lineEnding, output, Charset.defaultCharset()); }
/** * Writes the <code>toString()</code> value of each item in a collection to * the specified <code>File</code> line by line. * The specified character encoding and the line ending will be used. * * @param file the file to write to * @param encoding the encoding to use, {@code null} means platform default * @param lines the lines to write, {@code null} entries produce blank lines * @param lineEnding the line separator to use, {@code null} is system default * @param append if {@code true}, then the lines will be added to the * end of the file rather than overwriting * @throws IOException in case of an I/O error * @throws java.io.UnsupportedEncodingException if the encoding is not supported by the VM * @since 2.1 */ public static void writeLines(final File file, final String encoding, final Collection<?> lines, final String lineEnding, final boolean append) throws IOException { try (OutputStream out = new BufferedOutputStream(openOutputStream(file, append))) { IOUtils.writeLines(lines, lineEnding, out, encoding); } }
/** * Writes the <code>toString()</code> value of each item in a collection to * an <code>OutputStream</code> line by line, using the specified character * encoding and the specified line ending. * <p> * Character encoding names can be found at * <a href="http://www.iana.org/assignments/character-sets">IANA</a>. * * @param lines the lines to write, null entries produce blank lines * @param lineEnding the line separator to use, null is system default * @param output the <code>OutputStream</code> to write to, not null, not closed * @param encoding the encoding to use, null means platform default * @throws NullPointerException if the output is null * @throws IOException if an I/O error occurs * @throws java.nio.charset.UnsupportedCharsetException thrown instead of {@link java.io * .UnsupportedEncodingException} in version 2.2 if the * encoding is not supported. * @since 1.1 */ public static void writeLines(final Collection<?> lines, final String lineEnding, final OutputStream output, final String encoding) throws IOException { writeLines(lines, lineEnding, output, Charsets.toCharset(encoding)); }
IOUtils.writeLines(outLines, "\n", os, "UTF-8"); os.close(); System.out.println("UPDATED " + f);
@Test public void testWriteLines_Writer_nullStream() throws Exception { final Object[] data = new Object[]{"hello", "world"}; final List<Object> list = Arrays.asList(data); try { IOUtils.writeLines(list, "*", (Writer) null); fail(); } catch (final NullPointerException ignore) { } }
@Test public void testWriteLines_OutputStream_Encoding_nullStream() throws Exception { final Object[] data = new Object[]{"hello", "world"}; final List<Object> list = Arrays.asList(data); try { IOUtils.writeLines(list, "*", null, "US-ASCII"); fail(); } catch (final NullPointerException ignore) { } }
@Test public void testWriteLines_OutputStream_nullStream() throws Exception { final Object[] data = new Object[]{"hello", "world"}; final List<Object> list = Arrays.asList(data); try { IOUtils.writeLines(list, "*", (OutputStream) null); fail(); } catch (final NullPointerException ignore) { } }
@Test public void testWriteLines_Writer_nullData() throws Exception { final ByteArrayOutputStream baout = new ByteArrayOutputStream(); @SuppressWarnings("resource") // deliberately not closed final YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout, true, true); final Writer writer = new OutputStreamWriter(baout, "US-ASCII"); IOUtils.writeLines(null, "*", writer); out.off(); writer.flush(); assertEquals("Sizes differ", 0, baout.size()); }
@Test public void testWriteLines_Writer_nullSeparator() throws Exception { final Object[] data = new Object[]{"hello", "world"}; final List<Object> list = Arrays.asList(data); final ByteArrayOutputStream baout = new ByteArrayOutputStream(); @SuppressWarnings("resource") // deliberately not closed final YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout, true, true); final Writer writer = new OutputStreamWriter(baout, "US-ASCII"); IOUtils.writeLines(list, null, writer); out.off(); writer.flush(); final String expected = "hello" + IOUtils.LINE_SEPARATOR + "world" + IOUtils.LINE_SEPARATOR; final String actual = baout.toString(); assertEquals(expected, actual); }
@Test public void testWriteLines_Writer() throws Exception { final Object[] data = new Object[]{ "hello", new StringBuffer("world"), "", "this is", null, "some text"}; final List<Object> list = Arrays.asList(data); final ByteArrayOutputStream baout = new ByteArrayOutputStream(); @SuppressWarnings("resource") // deliberately not closed final YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout, true, true); final Writer writer = new OutputStreamWriter(baout, "US-ASCII"); IOUtils.writeLines(list, "*", writer); out.off(); writer.flush(); final String expected = "hello*world**this is**some text*"; final String actual = baout.toString(); assertEquals(expected, actual); }
@Test public void testWriteLines_OutputStream_Encoding_nullEncoding() throws Exception { final Object[] data = new Object[]{ "hello", new StringBuffer("world"), "", "this is", null, "some text"}; final List<Object> list = Arrays.asList(data); final ByteArrayOutputStream baout = new ByteArrayOutputStream(); final YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout, false, true); IOUtils.writeLines(list, "*", out, (String) null); out.off(); out.flush(); final String expected = "hello*world**this is**some text*"; final String actual = baout.toString(); assertEquals(expected, actual); }
@Test public void testWriteLines_OutputStream() throws Exception { final Object[] data = new Object[]{ "hello", new StringBuffer("world"), "", "this is", null, "some text"}; final List<Object> list = Arrays.asList(data); final ByteArrayOutputStream baout = new ByteArrayOutputStream(); final YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout, false, true); IOUtils.writeLines(list, "*", out); out.off(); out.flush(); final String expected = "hello*world**this is**some text*"; final String actual = baout.toString(); assertEquals(expected, actual); }
@Test public void testWriteLines_OutputStream_Encoding() throws Exception { final Object[] data = new Object[]{ "hello\u8364", new StringBuffer("world"), "", "this is", null, "some text"}; final List<Object> list = Arrays.asList(data); final ByteArrayOutputStream baout = new ByteArrayOutputStream(); final YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout, false, true); IOUtils.writeLines(list, "*", out, "UTF-8"); out.off(); out.flush(); final String expected = "hello\u8364*world**this is**some text*"; final String actual = baout.toString("UTF-8"); assertEquals(expected, actual); }
@Test public void testWriteLines_OutputStream_Encoding_nullSeparator() throws Exception { final Object[] data = new Object[]{"hello", "world"}; final List<Object> list = Arrays.asList(data); final ByteArrayOutputStream baout = new ByteArrayOutputStream(); final YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout, false, true); IOUtils.writeLines(list, null, out, "US-ASCII"); out.off(); out.flush(); final String expected = "hello" + IOUtils.LINE_SEPARATOR + "world" + IOUtils.LINE_SEPARATOR; final String actual = baout.toString(); assertEquals(expected, actual); }
@Test public void testWriteLines_OutputStream_nullSeparator() throws Exception { final Object[] data = new Object[]{"hello", "world"}; final List<Object> list = Arrays.asList(data); final ByteArrayOutputStream baout = new ByteArrayOutputStream(); final YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout, false, true); IOUtils.writeLines(list, null, out); out.off(); out.flush(); final String expected = "hello" + IOUtils.LINE_SEPARATOR + "world" + IOUtils.LINE_SEPARATOR; final String actual = baout.toString(); assertEquals(expected, actual); }
@Test public void testWriteLines_OutputStream_nullData() throws Exception { final ByteArrayOutputStream baout = new ByteArrayOutputStream(); final YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout, false, true); IOUtils.writeLines(null, "*", out); out.off(); out.flush(); assertEquals("Sizes differ", 0, baout.size()); }
@Test public void testWriteLines_OutputStream_Encoding_nullData() throws Exception { final ByteArrayOutputStream baout = new ByteArrayOutputStream(); final YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout, false, true); IOUtils.writeLines(null, "*", out, "US-ASCII"); out.off(); out.flush(); assertEquals("Sizes differ", 0, baout.size()); }