Search in sources :

Example 16 with PrintWriter

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

the class WriteFormatAsCsv method write.

@Override
protected void write(String path, ArrayList<IN> tupleList) {
    try (PrintWriter outStream = new PrintWriter(new BufferedWriter(new FileWriter(path, true)))) {
        for (IN tupleToWrite : tupleList) {
            String strTuple = tupleToWrite.toString();
            outStream.println(strTuple.substring(1, strTuple.length() - 1));
        }
    } catch (IOException e) {
        throw new RuntimeException("Exception occured while writing file " + path, e);
    }
}
Also used : FileWriter(java.io.FileWriter) IOException(java.io.IOException) PrintWriter(java.io.PrintWriter) BufferedWriter(java.io.BufferedWriter)

Example 17 with PrintWriter

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

the class WriteSinkFunction method cleanFile.

/**
	 * Creates target file if it does not exist, cleans it if it exists.
	 * 
	 * @param path
	 *            is the path to the location where the tuples are written
	 */
protected void cleanFile(String path) {
    try {
        PrintWriter writer;
        writer = new PrintWriter(path);
        writer.print("");
        writer.close();
    } catch (FileNotFoundException e) {
        throw new RuntimeException("An error occurred while cleaning the file: " + e.getMessage(), e);
    }
}
Also used : FileNotFoundException(java.io.FileNotFoundException) PrintWriter(java.io.PrintWriter)

Example 18 with PrintWriter

use of java.io.PrintWriter in project groovy by apache.

the class ClassCompletionVerifierTest method flattenErrorMessage.

private String flattenErrorMessage() {
    StringWriter stringWriter = new StringWriter();
    PrintWriter writer = new PrintWriter(stringWriter, true);
    for (int i = source.getErrorCollector().getErrorCount() - 1; i >= 0; i--) {
        source.getErrorCollector().getError(i).write(writer);
    }
    writer.close();
    return stringWriter.toString();
}
Also used : StringWriter(java.io.StringWriter) PrintWriter(java.io.PrintWriter)

Example 19 with PrintWriter

use of java.io.PrintWriter in project groovy by apache.

the class SourceParserTest method parse.

protected void parse(String name, Reader reader) {
    SourceBuffer sourceBuffer = new SourceBuffer();
    UnicodeEscapingReader unicodeReader = new UnicodeEscapingReader(reader, sourceBuffer);
    GroovyLexer lexer = new GroovyLexer(unicodeReader);
    unicodeReader.setLexer(lexer);
    GroovyRecognizer parser = GroovyRecognizer.make(lexer);
    parser.setSourceBuffer(sourceBuffer);
    parser.setFilename(name);
    // start parsing at the compilationUnit rule
    try {
        parser.compilationUnit();
    } catch (Exception ex) {
        StringWriter out = new StringWriter();
        out.write(ex.getMessage());
        out.write("\n");
        ex.printStackTrace(new PrintWriter(out));
        fail(out.toString());
    }
}
Also used : StringWriter(java.io.StringWriter) GroovyLexer(org.codehaus.groovy.antlr.parser.GroovyLexer) GroovyRecognizer(org.codehaus.groovy.antlr.parser.GroovyRecognizer) PrintWriter(java.io.PrintWriter)

Example 20 with PrintWriter

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

the class TestConfServlet method verifyGetProperty.

private void verifyGetProperty(Configuration conf, String format, String propertyName) throws Exception {
    StringWriter sw = null;
    PrintWriter pw = null;
    ConfServlet service = null;
    try {
        service = new ConfServlet();
        ServletConfig servletConf = mock(ServletConfig.class);
        ServletContext context = mock(ServletContext.class);
        service.init(servletConf);
        when(context.getAttribute(HttpServer2.CONF_CONTEXT_ATTRIBUTE)).thenReturn(conf);
        when(service.getServletContext()).thenReturn(context);
        HttpServletRequest request = mock(HttpServletRequest.class);
        when(request.getHeader(HttpHeaders.ACCEPT)).thenReturn(TEST_FORMATS.get(format));
        when(request.getParameter("name")).thenReturn(propertyName);
        HttpServletResponse response = mock(HttpServletResponse.class);
        sw = new StringWriter();
        pw = new PrintWriter(sw);
        when(response.getWriter()).thenReturn(pw);
        // response request
        service.doGet(request, response);
        String result = sw.toString().trim();
        // in the response
        if (Strings.isNullOrEmpty(propertyName)) {
            for (String key : TEST_PROPERTIES.keySet()) {
                assertTrue(result.contains(key) && result.contains(TEST_PROPERTIES.get(key)));
            }
        } else {
            if (conf.get(propertyName) != null) {
                // if property name is not empty and property is found
                assertTrue(result.contains(propertyName));
                for (String key : TEST_PROPERTIES.keySet()) {
                    if (!key.equals(propertyName)) {
                        assertFalse(result.contains(key));
                    }
                }
            } else {
                // if property name is not empty, and it's not in configuration
                // expect proper error code and error message is set to the response
                Mockito.verify(response).sendError(Mockito.eq(HttpServletResponse.SC_NOT_FOUND), Mockito.eq("Property " + propertyName + " not found"));
            }
        }
    } finally {
        if (sw != null) {
            sw.close();
        }
        if (pw != null) {
            pw.close();
        }
        if (service != null) {
            service.destroy();
        }
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) StringWriter(java.io.StringWriter) ServletConfig(javax.servlet.ServletConfig) ServletContext(javax.servlet.ServletContext) HttpServletResponse(javax.servlet.http.HttpServletResponse) PrintWriter(java.io.PrintWriter)

Aggregations

PrintWriter (java.io.PrintWriter)3529 StringWriter (java.io.StringWriter)1062 IOException (java.io.IOException)653 File (java.io.File)532 Test (org.junit.Test)432 FileOutputStream (java.io.FileOutputStream)293 FileWriter (java.io.FileWriter)274 OutputStreamWriter (java.io.OutputStreamWriter)255 BufferedReader (java.io.BufferedReader)180 ArrayList (java.util.ArrayList)171 HttpServletResponse (javax.servlet.http.HttpServletResponse)141 ByteArrayOutputStream (java.io.ByteArrayOutputStream)139 FastPrintWriter (com.android.internal.util.FastPrintWriter)124 InputStreamReader (java.io.InputStreamReader)123 HttpServletRequest (javax.servlet.http.HttpServletRequest)121 Date (java.util.Date)120 HashMap (java.util.HashMap)113 Map (java.util.Map)106 BufferedWriter (java.io.BufferedWriter)105 Writer (java.io.Writer)87