/** * Writes the <code>toString()</code> value of each item in a collection to * the specified <code>File</code> line by line. * The default VM encoding and the default line ending will be used. * * @param file the file to write to * @param lines the lines to write, {@code null} entries produce blank lines * @throws IOException in case of an I/O error * @since 1.3 */ public static void writeLines(final File file, final Collection<?> lines) throws IOException { writeLines(file, null, lines, null, false); }
/** * Writes the <code>toString()</code> value of each item in a collection to * the specified <code>File</code> line by line. * The default VM encoding and the specified line ending will be used. * * @param file the file to write to * @param lines the lines to write, {@code null} entries produce blank lines * @param lineEnding the line separator to use, {@code null} is system default * @throws IOException in case of an I/O error * @since 1.3 */ public static void writeLines(final File file, final Collection<?> lines, final String lineEnding) throws IOException { writeLines(file, null, lines, lineEnding, false); }
/** * Writes the <code>toString()</code> value of each item in a collection to * the specified <code>File</code> line by line. * The default VM encoding and the default line ending will be used. * * @param file the file to write to * @param lines the lines to write, {@code null} entries produce blank lines * @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 * @since 2.1 */ public static void writeLines(final File file, final Collection<?> lines, final boolean append) throws IOException { writeLines(file, null, lines, null, append); }
/** * Writes the <code>toString()</code> value of each item in a collection to * the specified <code>File</code> line by line. * The default VM encoding and the specified line ending will be used. * * @param file the file to write to * @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 * @since 2.1 */ public static void writeLines(final File file, final Collection<?> lines, final String lineEnding, final boolean append) throws IOException { writeLines(file, null, lines, lineEnding, append); }
/** * Writes the <code>toString()</code> value of each item in a collection to * the specified <code>File</code> line by line, optionally appending. * The specified character encoding and the default 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 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 boolean append) throws IOException { writeLines(file, encoding, lines, null, append); }
/** * Saves the configuration info to the disk. */ public synchronized void save() { if (BulkChange.contains(this)) { return; } File file = getConfigFile(); try { List<String> allSignatures = new ArrayList<>(whitelistSignaturesFromUserControlledList); blacklistSignaturesFromUserControlledList.stream() .map(signature -> "!" + signature) .forEach(allSignatures::add); FileUtils.writeLines(file, allSignatures); } catch (IOException e) { LOGGER.log(Level.WARNING, "Failed to save " + file.getAbsolutePath(), e); } }
/** * Creates a test file with a specified number of lines. * * @param file target file * @param lineCount number of lines to create * * @throws IOException If an I/O error occurs */ private List<String> createLinesFile(final File file, final int lineCount) throws IOException { final List<String> lines = createStringLines(lineCount); FileUtils.writeLines(file, lines); return lines; }
/** * Creates a test file with a specified number of lines. * * @param file target file * @param encoding the encoding to use while writing the lines * @param lineCount number of lines to create * * @throws IOException If an I/O error occurs */ private List<String> createLinesFile(final File file, final String encoding, final int lineCount) throws IOException { final List<String> lines = createStringLines(lineCount); FileUtils.writeLines(file, encoding, lines); return lines; }
@Test public void testWriteLines_4arg() throws Exception { final Object[] data = new Object[]{ "hello", new StringBuffer("world"), "", "this is", null, "some text"}; final List<Object> list = Arrays.asList(data); final File file = TestUtils.newFile(getTestDirectory(), "lines.txt"); FileUtils.writeLines(file, "US-ASCII", list, "*"); final String expected = "hello*world**this is**some text*"; final String actual = FileUtils.readFileToString(file, "US-ASCII"); assertEquals(expected, actual); }
@Test public void testWriteLines_3argsWithAppendOptionFalse_ShouldDeletePreviousFileLines() throws Exception { final File file = TestUtils.newFile(getTestDirectory(), "lines.txt"); FileUtils.writeStringToFile(file, "This line was there before you..."); final List<String> linesToAppend = Arrays.asList("my first line", "The second Line"); FileUtils.writeLines(file, linesToAppend, false); final String expected = "my first line" + IOUtils.LINE_SEPARATOR + "The second Line" + IOUtils.LINE_SEPARATOR; final String actual = FileUtils.readFileToString(file); assertEquals(expected, actual); }
@Test public void testWriteLinesEncoding_WithAppendOptionFalse_ShouldDeletePreviousFileLines() throws Exception { final File file = TestUtils.newFile(getTestDirectory(), "lines.txt"); FileUtils.writeStringToFile(file, "This line was there before you..."); final List<String> linesToAppend = Arrays.asList("my first line", "The second Line"); FileUtils.writeLines(file, null, linesToAppend, false); final String expected = "my first line" + IOUtils.LINE_SEPARATOR + "The second Line" + IOUtils.LINE_SEPARATOR; final String actual = FileUtils.readFileToString(file); assertEquals(expected, actual); }
@Test public void testWriteLines_5argsWithAppendOptionFalse_ShouldDeletePreviousFileLines() throws Exception { final File file = TestUtils.newFile(getTestDirectory(), "lines.txt"); FileUtils.writeStringToFile(file, "This line was there before you..."); final List<String> linesToAppend = Arrays.asList("my first line", "The second Line"); FileUtils.writeLines(file, null, linesToAppend, null, false); final String expected = "my first line" + IOUtils.LINE_SEPARATOR + "The second Line" + IOUtils.LINE_SEPARATOR; final String actual = FileUtils.readFileToString(file); assertEquals(expected, actual); }
@Test public void testWriteLines_4argsWithAppendOptionFalse_ShouldDeletePreviousFileLines() throws Exception { final File file = TestUtils.newFile(getTestDirectory(), "lines.txt"); FileUtils.writeStringToFile(file, "This line was there before you..."); final List<String> linesToAppend = Arrays.asList("my first line", "The second Line"); FileUtils.writeLines(file, linesToAppend, null, false); final String expected = "my first line" + IOUtils.LINE_SEPARATOR + "The second Line" + IOUtils.LINE_SEPARATOR; final String actual = FileUtils.readFileToString(file); assertEquals(expected, actual); }
@Test public void testWriteLines_4arg_nullSeparator() throws Exception { final Object[] data = new Object[]{ "hello", new StringBuffer("world"), "", "this is", null, "some text"}; final List<Object> list = Arrays.asList(data); final File file = TestUtils.newFile(getTestDirectory(), "lines.txt"); FileUtils.writeLines(file, "US-ASCII", list, null); final String expected = "hello" + IOUtils.LINE_SEPARATOR + "world" + IOUtils.LINE_SEPARATOR + IOUtils.LINE_SEPARATOR + "this is" + IOUtils.LINE_SEPARATOR + IOUtils.LINE_SEPARATOR + "some text" + IOUtils.LINE_SEPARATOR; final String actual = FileUtils.readFileToString(file, "US-ASCII"); assertEquals(expected, actual); }
@Test public void testWriteLines_3argsWithAppendOptionTrue_ShouldNotDeletePreviousFileLines() throws Exception { final File file = TestUtils.newFile(getTestDirectory(), "lines.txt"); FileUtils.writeStringToFile(file, "This line was there before you..."); final List<String> linesToAppend = Arrays.asList("my first line", "The second Line"); FileUtils.writeLines(file, linesToAppend, true); final String expected = "This line was there before you..." + "my first line" + IOUtils.LINE_SEPARATOR + "The second Line" + IOUtils.LINE_SEPARATOR; final String actual = FileUtils.readFileToString(file); assertEquals(expected, actual); }
@Test public void testWriteLines_3arg_nullSeparator() throws Exception { final Object[] data = new Object[]{ "hello", new StringBuffer("world"), "", "this is", null, "some text"}; final List<Object> list = Arrays.asList(data); final File file = TestUtils.newFile(getTestDirectory(), "lines.txt"); FileUtils.writeLines(file, "US-ASCII", list); final String expected = "hello" + IOUtils.LINE_SEPARATOR + "world" + IOUtils.LINE_SEPARATOR + IOUtils.LINE_SEPARATOR + "this is" + IOUtils.LINE_SEPARATOR + IOUtils.LINE_SEPARATOR + "some text" + IOUtils.LINE_SEPARATOR; final String actual = FileUtils.readFileToString(file, "US-ASCII"); assertEquals(expected, actual); }
@Test public void testWriteLinesEncoding_WithAppendOptionTrue_ShouldNotDeletePreviousFileLines() throws Exception { final File file = TestUtils.newFile(getTestDirectory(), "lines.txt"); FileUtils.writeStringToFile(file, "This line was there before you..."); final List<String> linesToAppend = Arrays.asList("my first line", "The second Line"); FileUtils.writeLines(file, null, linesToAppend, true); final String expected = "This line was there before you..." + "my first line" + IOUtils.LINE_SEPARATOR + "The second Line" + IOUtils.LINE_SEPARATOR; final String actual = FileUtils.readFileToString(file); assertEquals(expected, actual); }
@Test public void testWriteLines_5argsWithAppendOptionTrue_ShouldNotDeletePreviousFileLines() throws Exception { final File file = TestUtils.newFile(getTestDirectory(), "lines.txt"); FileUtils.writeStringToFile(file, "This line was there before you..."); final List<String> linesToAppend = Arrays.asList("my first line", "The second Line"); FileUtils.writeLines(file, null, linesToAppend, null, true); final String expected = "This line was there before you..." + "my first line" + IOUtils.LINE_SEPARATOR + "The second Line" + IOUtils.LINE_SEPARATOR; final String actual = FileUtils.readFileToString(file); assertEquals(expected, actual); }
@Test public void testWriteLines_4argsWithAppendOptionTrue_ShouldNotDeletePreviousFileLines() throws Exception { final File file = TestUtils.newFile(getTestDirectory(), "lines.txt"); FileUtils.writeStringToFile(file, "This line was there before you..."); final List<String> linesToAppend = Arrays.asList("my first line", "The second Line"); FileUtils.writeLines(file, linesToAppend, null, true); final String expected = "This line was there before you..." + "my first line" + IOUtils.LINE_SEPARATOR + "The second Line" + IOUtils.LINE_SEPARATOR; final String actual = FileUtils.readFileToString(file); assertEquals(expected, actual); }
@Test public void testWriteLines_4arg_Writer_nullData() throws Exception { final File file = TestUtils.newFile(getTestDirectory(), "lines.txt"); FileUtils.writeLines(file, "US-ASCII", null, "*"); assertEquals("Sizes differ", 0, file.length()); }