Search in sources :

Example 11 with FileWriter

use of java.io.FileWriter in project flink by apache.

the class MassiveStringSorting method generateFileWithStringTuples.

private File generateFileWithStringTuples(int numStrings, String prefix) throws IOException {
    final Random rnd = new Random(SEED);
    final StringBuilder bld = new StringBuilder();
    File f = File.createTempFile("strings", "txt");
    BufferedWriter wrt = null;
    try {
        wrt = new BufferedWriter(new FileWriter(f));
        for (int i = 0; i < numStrings; i++) {
            bld.setLength(0);
            int numComps = rnd.nextInt(5) + 1;
            for (int z = 0; z < numComps; z++) {
                if (z > 0) {
                    bld.append(' ');
                }
                bld.append(prefix);
                int len = rnd.nextInt(20) + 10;
                for (int k = 0; k < len; k++) {
                    char c = (char) (rnd.nextInt(80) + 40);
                    bld.append(c);
                }
            }
            String str = bld.toString();
            wrt.write(str);
            wrt.newLine();
        }
    } finally {
        wrt.close();
    }
    return f;
}
Also used : Random(java.util.Random) FileWriter(java.io.FileWriter) File(java.io.File) BufferedWriter(java.io.BufferedWriter)

Example 12 with FileWriter

use of java.io.FileWriter in project flink by apache.

the class MassiveStringValueSorting method generateFileWithStringTuples.

private File generateFileWithStringTuples(int numStrings, String prefix) throws IOException {
    final Random rnd = new Random(SEED);
    final StringBuilder bld = new StringBuilder();
    File f = File.createTempFile("strings", "txt");
    BufferedWriter wrt = null;
    try {
        wrt = new BufferedWriter(new FileWriter(f));
        for (int i = 0; i < numStrings; i++) {
            bld.setLength(0);
            int numComps = rnd.nextInt(5) + 1;
            for (int z = 0; z < numComps; z++) {
                if (z > 0) {
                    bld.append(' ');
                }
                bld.append(prefix);
                int len = rnd.nextInt(20) + 10;
                for (int k = 0; k < len; k++) {
                    char c = (char) (rnd.nextInt(80) + 40);
                    bld.append(c);
                }
            }
            String str = bld.toString();
            wrt.write(str);
            wrt.newLine();
        }
    } finally {
        if (wrt != null) {
            wrt.close();
        }
    }
    return f;
}
Also used : Random(java.util.Random) FileWriter(java.io.FileWriter) File(java.io.File) BufferedWriter(java.io.BufferedWriter)

Example 13 with FileWriter

use of java.io.FileWriter in project hadoop by apache.

the class TestFileSignerSecretProvider method testGetSecrets.

@Test
public void testGetSecrets() throws Exception {
    File testDir = new File(System.getProperty("test.build.data", "target/test-dir"));
    testDir.mkdirs();
    String secretValue = "hadoop";
    File secretFile = new File(testDir, "http-secret.txt");
    Writer writer = new FileWriter(secretFile);
    writer.write(secretValue);
    writer.close();
    FileSignerSecretProvider secretProvider = new FileSignerSecretProvider();
    Properties secretProviderProps = new Properties();
    secretProviderProps.setProperty(AuthenticationFilter.SIGNATURE_SECRET_FILE, secretFile.getAbsolutePath());
    secretProvider.init(secretProviderProps, null, -1);
    Assert.assertArrayEquals(secretValue.getBytes(), secretProvider.getCurrentSecret());
    byte[][] allSecrets = secretProvider.getAllSecrets();
    Assert.assertEquals(1, allSecrets.length);
    Assert.assertArrayEquals(secretValue.getBytes(), allSecrets[0]);
}
Also used : FileWriter(java.io.FileWriter) Properties(java.util.Properties) File(java.io.File) FileWriter(java.io.FileWriter) Writer(java.io.Writer) Test(org.junit.Test)

Example 14 with FileWriter

use of java.io.FileWriter in project hadoop by apache.

the class TestConfiguration method testReload.

public void testReload() throws IOException {
    out = new BufferedWriter(new FileWriter(CONFIG));
    startConfig();
    appendProperty("test.key1", "final-value1", true);
    appendProperty("test.key2", "value2");
    endConfig();
    Path fileResource = new Path(CONFIG);
    conf.addResource(fileResource);
    out = new BufferedWriter(new FileWriter(CONFIG2));
    startConfig();
    appendProperty("test.key1", "value1");
    appendProperty("test.key3", "value3");
    endConfig();
    Path fileResource1 = new Path(CONFIG2);
    conf.addResource(fileResource1);
    // add a few values via set.
    conf.set("test.key3", "value4");
    conf.set("test.key4", "value5");
    assertEquals("final-value1", conf.get("test.key1"));
    assertEquals("value2", conf.get("test.key2"));
    assertEquals("value4", conf.get("test.key3"));
    assertEquals("value5", conf.get("test.key4"));
    // change values in the test file...
    out = new BufferedWriter(new FileWriter(CONFIG));
    startConfig();
    appendProperty("test.key1", "final-value1");
    appendProperty("test.key3", "final-value3", true);
    endConfig();
    conf.reloadConfiguration();
    assertEquals("value1", conf.get("test.key1"));
    // overlayed property overrides.
    assertEquals("value4", conf.get("test.key3"));
    assertEquals(null, conf.get("test.key2"));
    assertEquals("value5", conf.get("test.key4"));
}
Also used : Path(org.apache.hadoop.fs.Path) FileWriter(java.io.FileWriter) BufferedWriter(java.io.BufferedWriter)

Example 15 with FileWriter

use of java.io.FileWriter in project hadoop by apache.

the class TestConfiguration method testEnumFromXml.

public void testEnumFromXml() throws IOException {
    out = new BufferedWriter(new FileWriter(CONFIG_FOR_ENUM));
    startConfig();
    appendProperty("test.enum", " \t \n   FOO \t \n");
    appendProperty("test.enum2", " \t \n   Yak.FOO \t \n");
    endConfig();
    Configuration conf = new Configuration();
    Path fileResource = new Path(CONFIG_FOR_ENUM);
    conf.addResource(fileResource);
    assertSame(Yak.FOO, conf.getEnum("test.enum", Yak.FOO));
    boolean fail = false;
    try {
        conf.getEnum("test.enum2", Yak.FOO);
    } catch (IllegalArgumentException e) {
        fail = true;
    }
    assertTrue(fail);
}
Also used : Path(org.apache.hadoop.fs.Path) FileWriter(java.io.FileWriter) BufferedWriter(java.io.BufferedWriter)

Aggregations

FileWriter (java.io.FileWriter)1776 File (java.io.File)1056 IOException (java.io.IOException)741 BufferedWriter (java.io.BufferedWriter)705 PrintWriter (java.io.PrintWriter)285 Test (org.junit.Test)222 Writer (java.io.Writer)162 FileReader (java.io.FileReader)121 BufferedReader (java.io.BufferedReader)107 ArrayList (java.util.ArrayList)101 FileNotFoundException (java.io.FileNotFoundException)68 Date (java.util.Date)61 Properties (java.util.Properties)61 FileOutputStream (java.io.FileOutputStream)58 HashMap (java.util.HashMap)53 StringWriter (java.io.StringWriter)50 Path (org.apache.hadoop.fs.Path)49 FileInputStream (java.io.FileInputStream)47 Map (java.util.Map)33 OutputStreamWriter (java.io.OutputStreamWriter)31