Search in sources :

Example 36 with FileWriter

use of java.io.FileWriter in project platform_frameworks_base by android.

the class DropBoxTest method testAddEntriesInTheFuture.

public void testAddEntriesInTheFuture() throws Exception {
    File dir = getEmptyDir("testAddEntriesInTheFuture");
    long before = System.currentTimeMillis();
    // Near future: should be allowed to persist
    FileWriter w0 = new FileWriter(new File(dir, "DropBoxTest@" + (before + 5000) + ".txt"));
    w0.write("FUTURE0");
    w0.close();
    // Far future: should be collapsed
    FileWriter w1 = new FileWriter(new File(dir, "DropBoxTest@" + (before + 100000) + ".txt"));
    w1.write("FUTURE1");
    w1.close();
    // Another far future item, this one gzipped
    File f2 = new File(dir, "DropBoxTest@" + (before + 100001) + ".txt.gz");
    GZIPOutputStream gz2 = new GZIPOutputStream(new FileOutputStream(f2));
    gz2.write("FUTURE2".getBytes());
    gz2.close();
    // Tombstone in the far future
    new FileOutputStream(new File(dir, "DropBoxTest@" + (before + 100002) + ".lost")).close();
    DropBoxManagerService service = new DropBoxManagerService(getContext(), dir);
    DropBoxManager dropbox = new DropBoxManager(getContext(), service.getServiceStub());
    // Until a write, the timestamps are taken at face value
    DropBoxManager.Entry e0 = dropbox.getNextEntry(null, before);
    DropBoxManager.Entry e1 = dropbox.getNextEntry(null, e0.getTimeMillis());
    DropBoxManager.Entry e2 = dropbox.getNextEntry(null, e1.getTimeMillis());
    DropBoxManager.Entry e3 = dropbox.getNextEntry(null, e2.getTimeMillis());
    assertTrue(null == dropbox.getNextEntry(null, e3.getTimeMillis()));
    assertEquals("FUTURE0", e0.getText(80));
    assertEquals("FUTURE1", e1.getText(80));
    assertEquals("FUTURE2", e2.getText(80));
    assertEquals(null, e3.getText(80));
    assertEquals(before + 5000, e0.getTimeMillis());
    assertEquals(before + 100000, e1.getTimeMillis());
    assertEquals(before + 100001, e2.getTimeMillis());
    assertEquals(before + 100002, e3.getTimeMillis());
    e0.close();
    e1.close();
    e2.close();
    e3.close();
    // Write something to force a collapse
    dropbox.addText("NotDropBoxTest", "FUTURE");
    e0 = dropbox.getNextEntry(null, before);
    e1 = dropbox.getNextEntry(null, e0.getTimeMillis());
    e2 = dropbox.getNextEntry(null, e1.getTimeMillis());
    e3 = dropbox.getNextEntry(null, e2.getTimeMillis());
    assertTrue(null == dropbox.getNextEntry("DropBoxTest", e3.getTimeMillis()));
    assertEquals("FUTURE0", e0.getText(80));
    assertEquals("FUTURE1", e1.getText(80));
    assertEquals("FUTURE2", e2.getText(80));
    assertEquals(null, e3.getText(80));
    assertEquals(before + 5000, e0.getTimeMillis());
    assertEquals(before + 5001, e1.getTimeMillis());
    assertEquals(before + 5002, e2.getTimeMillis());
    assertEquals(before + 5003, e3.getTimeMillis());
    e0.close();
    e1.close();
    e2.close();
    e3.close();
}
Also used : DropBoxManager(android.os.DropBoxManager) DropBoxManagerService(com.android.server.DropBoxManagerService) GZIPOutputStream(java.util.zip.GZIPOutputStream) FileWriter(java.io.FileWriter) FileOutputStream(java.io.FileOutputStream) File(java.io.File)

Example 37 with FileWriter

use of java.io.FileWriter in project frodo by android10.

the class DataPresenter method executeDiskIOTaskOnUiThread.

public void executeDiskIOTaskOnUiThread() {
    try {
        File file = File.createTempFile("test", ".txt");
        FileWriter writer = new FileWriter(file);
        writer.write("This is Fernando Cejas testing something");
        writer.close();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
    }
}
Also used : FileWriter(java.io.FileWriter) IOException(java.io.IOException) File(java.io.File)

Example 38 with FileWriter

use of java.io.FileWriter in project jstorm by alibaba.

the class SandBoxMaker method generatePolicyFile.

public String generatePolicyFile(Map<String, String> replaceMap) throws IOException {
    // dynamic generate policy file, no static file
    String tmpPolicy = StormConfig.supervisorTmpDir(conf) + File.separator + UUID.randomUUID().toString();
    InputStream inputStream = SandBoxMaker.class.getClassLoader().getResourceAsStream(SANBOX_TEMPLATE_NAME);
    PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(tmpPolicy)));
    BufferedReader reader = null;
    InputStreamReader inputReader = null;
    try {
        inputReader = new InputStreamReader(inputStream);
        reader = new BufferedReader(new LineNumberReader(inputReader));
        String line = null;
        while ((line = reader.readLine()) != null) {
            String replaced = replaceLine(line, replaceMap);
            writer.println(replaced);
        }
        return tmpPolicy;
    } catch (Exception e) {
        LOG.error("Failed to generate policy file\n", e);
        throw new IOException(e);
    } finally {
        if (inputStream != null) {
            inputStream.close();
        }
        if (writer != null) {
            writer.close();
        }
        if (reader != null) {
            reader.close();
        }
        if (inputReader != null) {
            inputReader.close();
        }
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) FileWriter(java.io.FileWriter) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) IOException(java.io.IOException) PrintWriter(java.io.PrintWriter) BufferedWriter(java.io.BufferedWriter) LineNumberReader(java.io.LineNumberReader)

Example 39 with FileWriter

use of java.io.FileWriter in project gradle by gradle.

the class BrowserEvaluate method doEvaluate.

@TaskAction
void doEvaluate() {
    HttpFileServer fileServer = new SimpleHttpFileServerFactory().start(getContent(), 0);
    try {
        Writer resultWriter = new FileWriter(getResult());
        getEvaluator().evaluate(fileServer.getResourceUrl(getResource()), resultWriter);
        resultWriter.close();
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    } finally {
        fileServer.stop();
    }
    setDidWork(true);
}
Also used : SimpleHttpFileServerFactory(org.gradle.plugins.javascript.envjs.http.simple.SimpleHttpFileServerFactory) FileWriter(java.io.FileWriter) HttpFileServer(org.gradle.plugins.javascript.envjs.http.HttpFileServer) UncheckedIOException(org.gradle.api.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(org.gradle.api.UncheckedIOException) FileWriter(java.io.FileWriter) Writer(java.io.Writer)

Example 40 with FileWriter

use of java.io.FileWriter in project groovy-core by groovy.

the class SourceUnit method saveAsXML.

private void saveAsXML(String name, ModuleNode ast) {
    XStream xstream = new XStream();
    try {
        xstream.toXML(ast, new FileWriter(name + ".xml"));
        System.out.println("Written AST to " + name + ".xml");
    } catch (Exception e) {
        System.out.println("Couldn't write to " + name + ".xml");
        e.printStackTrace();
    }
}
Also used : XStream(com.thoughtworks.xstream.XStream) FileWriter(java.io.FileWriter) NoViableAltForCharException(antlr.NoViableAltForCharException) MismatchedCharException(antlr.MismatchedCharException) MismatchedTokenException(antlr.MismatchedTokenException) IOException(java.io.IOException) NoViableAltException(antlr.NoViableAltException)

Aggregations

FileWriter (java.io.FileWriter)1994 File (java.io.File)1195 IOException (java.io.IOException)866 BufferedWriter (java.io.BufferedWriter)798 PrintWriter (java.io.PrintWriter)329 Test (org.junit.Test)243 Writer (java.io.Writer)181 FileReader (java.io.FileReader)148 BufferedReader (java.io.BufferedReader)128 ArrayList (java.util.ArrayList)121 FileNotFoundException (java.io.FileNotFoundException)78 Date (java.util.Date)68 FileOutputStream (java.io.FileOutputStream)65 Properties (java.util.Properties)65 HashMap (java.util.HashMap)61 FileInputStream (java.io.FileInputStream)54 StringWriter (java.io.StringWriter)51 Path (org.apache.hadoop.fs.Path)50 Map (java.util.Map)42 InputStreamReader (java.io.InputStreamReader)34