@@ -0,0 +1,2808 @@
+package pl.java.scalatech.test.file;%0A%0Aimport static com.google.common.base.Preconditions.checkNotNull;%0Aimport static org.fest.assertions.Assertions.assertThat;%0A%0Aimport java.io.BufferedWriter;%0Aimport java.io.File;%0Aimport java.io.FileWriter;%0Aimport java.io.IOException;%0Aimport java.io.PrintWriter;%0Aimport java.nio.charset.Charset;%0Aimport java.util.List;%0A%0Aimport lombok.extern.slf4j.Slf4j;%0A%0Aimport org.junit.Before;%0Aimport org.junit.Rule;%0Aimport org.junit.Test;%0Aimport org.junit.rules.TemporaryFolder;%0A%0Aimport com.google.common.base.Charsets;%0Aimport com.google.common.collect.Lists;%0Aimport com.google.common.io.Files;%0Aimport com.google.common.io.LineProcessor;%0A%0A@Slf4j%0Apublic class AppendStrToFileTest %7B%0A%0A    @Rule%0A    public TemporaryFolder testFolder = new TemporaryFolder();%0A    private File file;%0A%0A    @Before%0A    public void clean() throws IOException %7B%0A        testFolder.newFolder(%22test_%22);%0A        file = testFolder.newFile(%22t.txt%22);%0A        log.info(%22file :  %7B%7D%22, file.getAbsolutePath());%0A%0A    %7D%0A%0A    @Test%0A    public void shouldAppendText2FileJava7Approach() throws IOException %7B%0A%0A        try (PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(file, true)))) %7B%0A            out.println(%22przodownik%22);%0A        %7D catch (IOException e) %7B%0A            log.error(%22%7B%7D%22, e);%0A        %7D%0A        assertThat(Files.readLines(file, Charsets.UTF_8)).hasSize(1);%0A        assertThat(Files.readLines(file, Charsets.UTF_8)).contains(%22przodownik%22);%0A    %7D%0A%0A    @Test%0A    public void append_to_file_guava() throws IOException %7B%0A        Files.append(%22przodownik%22, file, Charsets.UTF_8);%0A        List%3CString%3E result = Files.readLines(file, Charset.defaultCharset(), new LineProcessor%3CList%3CString%3E%3E() %7B%0A            List%3CString%3E ret = Lists.newArrayList();%0A%0A            @Override%0A            public boolean processLine(String line) throws IOException %7B%0A%0A                return ret.add(line.trim());%0A            %7D%0A%0A            @Override%0A            public List%3CString%3E getResult() %7B%0A%0A                return ret;%0A            %7D%0A        %7D);%0A        assertThat(Files.readLines(file, Charsets.UTF_8)).hasSize(1);%0A        assertThat(Files.readLines(file, Charsets.UTF_8)).contains(%22przodownik%22);%0A    %7D%0A%0A    @Test%0A    public void shouldGuavaWriteTextToFile() throws IOException %7B%0A        writeToFile(file, %22przodownik%22);%0A        assertThat(Files.readLines(file, Charsets.UTF_8)).hasSize(1);%0A        assertThat(Files.readLines(file, Charsets.UTF_8)).contains(%22przodownik%22);%0A    %7D%0A%0A    private void writeToFile(File file, String contents) %7B%0A        checkNotNull(file, %22File must exits%22);%0A        checkNotNull(contents, %22Unable to write null contents.%22);%0A%0A        try %7B%0A            Files.write(contents.getBytes(), file);%0A        %7D catch (IOException fileIoEx) %7B%0A            log.error(%22%7B%7D%22, fileIoEx);%0A%0A        %7D%0A    %7D%0A%7D%0A

write to file test guava