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);
}
}
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);
}
}
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();
}
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());
}
}
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();
}
}
}
Aggregations