public void whenWriteUsingFiles_thenWritten() throws IOException {
String expectedValue = "Hello world";
File file = new File("test.txt");
Files.write(expectedValue, file, Charsets.UTF_8);
String result = Files.toString(file, Charsets.UTF_8);
assertEquals(expectedValue, result);
}
public void testWriteBytes() throws IOException {
File temp = createTempFile();
byte[] data = newPreFilledByteArray(2000);
Files.write(data, temp);
assertTrue(Arrays.equals(data, Files.toByteArray(temp)));
try {
Files.write(null, temp);
fail("expected exception");
} catch (NullPointerException expected) {
}
}
public void demoFileWrite(final String fileName, final String contents)
{
checkNotNull(fileName, "Provided file name for writing must NOT be null.");
checkNotNull(contents, "Unable to write null contents.");
final File newFile = new File(fileName);
try
{
Files.write(contents.getBytes(), newFile);
}
catch (IOException fileIoEx)
{
err.println( "ERROR trying to write to file '" + fileName + "' - "
+ fileIoEx.toString());
}
}
public class FileWriteTest {
public static void main(String[] args) throws IOException {
File file = new File("C:\\abcd.txt");
//Creates a new file if it doesn't exist
//Overwrites the file if already available
Files.write("BE THE CODER", file, Charsets.UTF_8);
System.out.println("Successfully written the file");
}
}
private static void writeFile() {
String content = "hello";
File file = new File("/Users/dxm/Documents/test.txt");
try {
Files.write(content.getBytes(Charsets.UTF_8),file);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("end");
}
public void should_read_files_in_directory() throws IOException {
// Given a temp directory that contains
File tempDir = Files.createTempDir();
tempDir.deleteOnExit();
// json file
File file1 = File.createTempFile("file1", ".json", tempDir);
String content1 = "content1";
Files.write(content1.getBytes(), file1);
// n1ql file
File file2 = File.createTempFile("file2", ".N1QL", tempDir);
String content2 = "content2";
Files.write(content2.getBytes(), file2);
// txt file
Files.write(getRandomString().getBytes(), File.createTempFile(getRandomString(), ".txt", tempDir));
// When we read files in this directory with extension filter
Map<String, String> result = FileUtils.readFilesInDirectory(tempDir.toPath(), "json", "n1ql");
// Then we should have file content matching this extension
Assert.assertEquals(2, result.size());
Assert.assertEquals(content1, result.get(file1.getName()));
Assert.assertEquals(content2, result.get(file2.getName()));
}
public void mergeHeaders() throws Exception {
File output = new File(".test-files/mergeHeaders/merged-manifest.yml");
File zip = new File(".test-files/mergeHeaders/headers.zip");
zip.getParentFile().mkdirs();
Files.write("xyz", zip, StandardCharsets.UTF_8);
output.delete();
String text = main("merge", "headers",
"com.github.jomof:sqlite:3.16.2-rev48",
zip.toString(),
"include",
output.toString());
assertThat(text).doesNotContain("Usage");
assertThat(text).contains("Merged com.github.jomof:sqlite:3.16.2-rev48 and ");
System.out.printf(text);
System.out.printf(FileUtils.readAllText(output));
}
public void testLifecycle() throws IOException, InterruptedException {
File f1 = new File(tmpDir, "file1");
Files.write("file1line1\nfile1line2\n", f1, Charsets.UTF_8);
Context context = new Context();
context.put(POSITION_FILE, posFilePath);
context.put(FILE_GROUPS, "f1");
context.put(FILE_GROUPS_PREFIX + "f1", tmpDir.getAbsolutePath() + "/file1$");
Configurables.configure(source, context);
for (int i = 0; i < 3; i++) {
source.start();
source.process();
assertTrue("Reached start or error", LifecycleController.waitForOneOf(
source, LifecycleState.START_OR_ERROR));
assertEquals("Server is started", LifecycleState.START,
source.getLifecycleState());
source.stop();
assertTrue("Reached stop or error",
LifecycleController.waitForOneOf(source, LifecycleState.STOP_OR_ERROR));
assertEquals("Server is stopped", LifecycleState.STOP,
source.getLifecycleState());
}
}
public final void saveHistory() {
try {
final StringBuilder builder = new StringBuilder();
for(int i = 0; i < projectsModel.size(); i++) {
builder.append(projectsModel.getElementAt(i) + System.lineSeparator());
}
Files.write(builder.toString(), new File(Utils.getParentFolder(), Constants.FILE_GUI_HISTORY), StandardCharsets.UTF_8);
}
catch(final Exception ex) {
ex.printStackTrace(guiPrintStream);
ex.printStackTrace();
JOptionPane.showMessageDialog(ProjectsFrame.this, String.format(Constants.GUI_DIALOG_ERROR_MESSAGE, ex.getMessage()), ex.getClass().getName(), JOptionPane.ERROR_MESSAGE);
}
}
public void setup() throws Exception {
baseDir = Files.createTempDir();
keyStorePasswordFile = new File(baseDir, "keyStorePasswordFile");
Files.write("keyStorePassword", keyStorePasswordFile, Charsets.UTF_8);
keyAliasPassword = Maps.newHashMap();
keyStoreFile = new File(baseDir, "keyStoreFile");
Assert.assertTrue(keyStoreFile.createNewFile());
}