public void append_to_file_guava () throws IOException {
File file = new File(OUTPUT_FILE_NAME);
Files.append("Append text to file w/ guava",
file,
Charsets.UTF_8);
}
public static void main (String args[]) {
https://stackoverflow.com/questions/1625234/how-to-append-text-to-an-existing-file-in-java
File to = new File("C:/test/test.csv");
for (int i = 0; i < 42; i++) {
CharSequence from = "some string" + i + "\n";
Files.append(from, to, Charsets.UTF_8);
}
}
public void writingTextFiles() throws IOException {
File outFile = new File("target/writeme.txt");
Files.write("Nothing to see here. Move along.", outFile, Charsets.UTF_8);
Files.append("\nNo, really.", outFile, Charsets.UTF_8);
}
public class FileAppendTest {
public static void main(String[] args) throws IOException {
File file = new File("C:\\abcd.txt");
Files.append("\nBE THE CODER", file, Charsets.UTF_8);
}
}
public void testAppendString() throws IOException {
File temp = createTempFile();
Files.append(I18N, temp, Charsets.UTF_16LE);
assertEquals(I18N, Files.toString(temp, Charsets.UTF_16LE));
Files.append(I18N, temp, Charsets.UTF_16LE);
assertEquals(I18N + I18N, Files.toString(temp, Charsets.UTF_16LE));
Files.append(I18N, temp, Charsets.UTF_16LE);
assertEquals(I18N + I18N + I18N, Files.toString(temp, Charsets.UTF_16LE));
}
public void testFileChangesDuringRead() throws IOException {
File f1 = new File(tmpDir.getAbsolutePath() + "/file1");
Files.write("file1line1\nfile1line2\n", f1, Charsets.UTF_8);
ReliableSpoolingFileEventReader parser1 = getParser();
List<String> out = Lists.newArrayList();
out.addAll(bodiesAsStrings(parser1.readEvents(2)));
parser1.commit();
assertEquals(2, out.size());
assertTrue(out.contains("file1line1"));
assertTrue(out.contains("file1line2"));
Files.append("file1line3\n", f1, Charsets.UTF_8);
out.add(bodyAsString(parser1.readEvent()));
parser1.commit();
out.add(bodyAsString(parser1.readEvent()));
parser1.commit();
}
private void createInputTestFiles(List<File> spoolDirs, int numFiles, int startNum)
throws IOException {
int numSpoolDirs = spoolDirs.size();
for (int dirNum = 0; dirNum < numSpoolDirs; dirNum++) {
File spoolDir = spoolDirs.get(dirNum);
for (int fileNum = startNum; fileNum < numFiles; fileNum++) {
// Stage the files on what is almost certainly the same FS partition.
File tmp = new File(spoolDir.getParent(), UUID.randomUUID().toString());
Files.append(getTestString(dirNum, fileNum), tmp, Charsets.UTF_8);
File dst = new File(spoolDir, String.format("test-file-%03d", fileNum));
// Ensure we move them into the spool directory atomically, if possible.
assertTrue(String.format("Failed to rename %s to %s", tmp, dst),
tmp.renameTo(dst));
}
}
}
private void saveDataToFile(LoadData data, String dnName) {
if (data.getFileName() == null) {
String dnPath = tempPath + dnName + ".txt";
data.setFileName(dnPath);
}
File dnFile = new File(data.getFileName());
try {
if (!dnFile.exists()) {
Files.createParentDirs(dnFile);
}
Files.append(joinLine(data.getData(), data), dnFile, Charset.forName(loadData.getCharset()));
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
data.setData(null);
}
}
private void addToScenarioList(String testId, Set<String> templates, File scenarioList, ResultsStore resultsStore) {
try {
long estimatedRuntime = getEstimatedRuntime(testId, resultsStore);
List<String> args = Lists.newArrayList();
args.add(testId);
args.add(String.valueOf(estimatedRuntime));
args.addAll(templates);
Files.touch(scenarioList);
Files.append(Joiner.on(';').join(args) + '\n', scenarioList, Charsets.UTF_8);
} catch (IOException e) {
throw new RuntimeException("Could not write to scenario list at " + scenarioList, e);
}
}
public void execute(Tuple tuple) {
File file = getFile();
logger.debug("FILER: Writing tuple to disk: File = {}, tuple={}", file.getAbsolutePath(), tuple);
try {
// Start with just the values; determine later if the fields are needed.
//Files.append(tuple.getFields().toString(), file, Charsets.UTF_8);
Files.append(tuple.getValues().toString() + "\n", file, Charsets.UTF_8);
} catch (IOException e) {
logger.error("FILER: couldn't append to file: {}. Exception: {}. Cause: {}",
file.getAbsolutePath(), e.getMessage(), e.getCause());
}
_collector.ack(tuple);
}