java.io.PrintWriter.print()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(8.9k)|赞(0)|评价(0)|浏览(221)

本文整理了Java中java.io.PrintWriter.print()方法的一些代码示例,展示了PrintWriter.print()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。PrintWriter.print()方法的具体详情如下:
包路径:java.io.PrintWriter
类名称:PrintWriter
方法名:print

PrintWriter.print介绍

[英]Prints the string representation of the specified character to the target.
[中]将指定字符的字符串表示形式打印到目标。

代码示例

canonical example by Tabnine

public void writeToFile(File dest, String content, boolean append) throws IOException {
  // append - true for writing to the end of the file rather to the beginning
  try (PrintWriter writer = new PrintWriter(new FileWriter(dest, append))) {
    writer.print(content);
  }
}

代码示例来源:origin: redisson/redisson

public void print(PrintWriter out) {
    out.print("UTF8 \"");
    out.print(string);
    out.println("\"");
  }
}

代码示例来源:origin: apache/incubator-dubbo

public static String toString(Throwable e) {
  StringWriter w = new StringWriter();
  PrintWriter p = new PrintWriter(w);
  p.print(e.getClass().getName() + ": ");
  if (e.getMessage() != null) {
    p.print(e.getMessage() + "\n");
  }
  p.println();
  try {
    e.printStackTrace(p);
    return w.toString();
  } finally {
    p.close();
  }
}

代码示例来源:origin: alibaba/Sentinel

/**
 * Write `400 Bad Request` HTTP response status line and message body, then flush.
 */
private void badRequest(/*@NonNull*/ final PrintWriter out, String message) {
  out.print("HTTP/1.1 400 Bad Request\r\n"
    + "Connection: close\r\n\r\n");
  out.print(message);
  out.flush();
  writtenHead = true;
}

代码示例来源:origin: stackoverflow.com

public void write(String txt, OutputStream out) {
  PrintWriter printer = new PrintWriter(out);
  printer.print(txt);
  printer.flush();
  //it is very unpolite to close someone else's streams!
  //printer.close();
}

代码示例来源:origin: stackoverflow.com

PrintWriter writer = new PrintWriter(file);
writer.print("");
writer.close();

代码示例来源:origin: jenkinsci/jenkins

rsp.setContentType("text/html;charset=UTF-8");
  out = new PrintWriter(new OutputStreamWriter(rsp.getOutputStream()));
} catch (IllegalStateException e) {
  out = rsp.getWriter();
  out.print("                              ");
out.close();

代码示例来源:origin: stanfordnlp/CoreNLP

/**
 * {@inheritDoc}
 */
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 logger.info("GET SPIED query from " + request.getRemoteAddr());
 if (request.getCharacterEncoding() == null) {
  request.setCharacterEncoding("utf-8");
 }
 response.setContentType("text/json; charset=UTF-8");
 PrintWriter out = response.getWriter();
 try {
  String raw = request.getParameter("q");
  String seedwords = request.getParameter("seedwords");
  String model = request.getParameter("model");
  if (raw == null || "".equals(raw)) {
   out.print("{\"okay\":false,\"reason\":\"No data provided\"}");
  } else {
   run(out, raw, seedwords, model);
  }
 } catch (Exception t) {
  writeError(t, out, request.toString());
 }
 out.close();
}

代码示例来源:origin: apache/nifi

@Override
  public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    baseRequest.setHandled(true);
    if ("Fetch".equalsIgnoreCase(request.getMethod())) {
      final int status = Integer.valueOf(target.substring("/status".length() + 1));
      response.setStatus(status);
      response.setContentType("text/plain");
      response.setContentLength(target.length());
      try (PrintWriter writer = response.getWriter()) {
        writer.print(target);
        writer.flush();
      }
    } else {
      response.setStatus(404);
      response.setContentType("text/plain");
      response.setContentLength(target.length());
    }
  }
}

代码示例来源:origin: gocd/gocd

private void handleInvalidResponse(HttpResponse response, String url) throws IOException {
    StringWriter sw = new StringWriter();
    try (PrintWriter out = new PrintWriter(sw)) {
      out.print("Problem accessing GoCD Server at ");
      out.println(url);
      if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
        LOG.info("Response code: {}", response.getStatusLine().getStatusCode());
        out.println("Possible causes:");
        out.println("1. Your GoCD Server is down, not accessible or starting up.");
        out.println("2. This agent might be incompatible with your GoCD Server. Please fix the version mismatch between GoCD Server and GoCD Agent.");

        throw new ClientProtocolException(sw.toString());
      } else if (response.getFirstHeader(MD5_HEADER) == null || response.getFirstHeader(SSL_PORT_HEADER) == null) {
        out.print("Missing required headers '");
        out.print(MD5_HEADER);
        out.print("' and '");
        out.print(SSL_PORT_HEADER);
        out.println("' in response.");
        throw new ClientProtocolException(sw.toString());
      }
    }
  }
}

代码示例来源:origin: gocd/gocd

private void generateHTMLResponse() {
  response.setContentType("text/html");
  response.setCharacterEncoding("utf-8");
  try {
    response.getWriter().print(htmlResponse);
  } catch (IOException e) {
    LOGGER.error("General IOException: {}", e.getMessage());
  }
}

代码示例来源:origin: stanfordnlp/CoreNLP

String sep = "\t";
for (int i = 0; i < featureIndex.size(); i++) {
 pw.print(sep + featureIndex.get(i));
pw.println();
for (int i = 0; i < size; i++) { // changed labels.length to size
 pw.print(labelIndex.get(labels[i])); // changed i to labels[i]
 Map<Integer, Double> feats = Generics.newHashMap();
 for (int j = 0; j < data[i].length; j++) {
   pw.print(sep + feats.get(Integer.valueOf(j)));
  } else {
   pw.print(sep);
   pw.print(' ');
 pw.println();
pw.flush();

代码示例来源:origin: macrozheng/mall

@Override
  public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
    response.setHeader("Content-Type", "application/json;charset=utf-8");
    response.getWriter().print("{\"code\":200,\"message\":\"登录成功\"}");
    response.getWriter().flush();
  }
}

代码示例来源:origin: alibaba/Sentinel

private static void writeDefaultBlockedPage(HttpServletResponse response) throws IOException {
  PrintWriter out = response.getWriter();
  out.print(DEFAULT_BLOCK_MSG);
  out.flush();
  out.close();
}

代码示例来源:origin: javaee-samples/javaee7-samples

@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  request.setAttribute("jaccTest", "true");
  try {
    HttpServletRequest requestFromPolicy = (HttpServletRequest) PolicyContext.getContext("javax.servlet.http.HttpServletRequest");
    if (requestFromPolicy != null) {
      response.getWriter().print("Obtained request from context.");
      if ("true".equals(requestFromPolicy.getAttribute("jaccTest"))) {
        response.getWriter().print("Attribute present in request from context.");
      }
      if ("true".equals(requestFromPolicy.getParameter("jacc_test"))) {
        response.getWriter().print("Request parameter present in request from context.");
      }
    }
  } catch (PolicyContextException e) {
    e.printStackTrace(response.getWriter());
  }
}

代码示例来源:origin: stanfordnlp/CoreNLP

void writeError(Throwable t, PrintWriter out, String input){
 StringWriter sw = new StringWriter();
 PrintWriter pw = new PrintWriter(sw);
 t.printStackTrace(pw);
 logger.info("input is " + input);
 logger.info(sw.toString());
 out.print("{\"okay\":false, \"reason\":\"Something bad happened. Contact the author.\"}");
}

代码示例来源:origin: stanfordnlp/CoreNLP

public void printSentences(Iterable<List<? extends HasWord>> sentences, String filename) {
 try {
  PrintWriter pw = IOUtils.getPrintWriter(filename);
  for (List<? extends HasWord> sentence:sentences) {
   pw.print("<s> ");   // Note: Use <s sentence-id > to identify sentences
   String sentString = SentenceUtils.listToString(sentence);
   if (sentence.size() > maxSentenceLength) {
    logger.warning("Sentence length=" + sentence.size() +
        " is longer than maximum set length " + maxSentenceLength);
    logger.warning("Long Sentence: " + sentString);
   }
   pw.print(sentString);
   pw.println(" </s>");
  }
  pw.close();
 } catch (IOException ex) {
  throw new RuntimeException(ex);
 }
}

代码示例来源:origin: apache/hbase

@Override
 public void doGet(HttpServletRequest request,
          HttpServletResponse response
          ) throws ServletException, IOException {
  PrintWriter out = response.getWriter();
  SortedSet<String> sortedKeys = new TreeSet<>();
  Enumeration<String> keys = request.getParameterNames();
  while(keys.hasMoreElements()) {
   sortedKeys.add(keys.nextElement());
  }
  for(String key: sortedKeys) {
   out.print(key);
   out.print(':');
   out.print(request.getParameter(key));
   out.print('\n');
  }
  out.close();
 }
}

代码示例来源:origin: loklak/loklak_server

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  Query post = RemoteAccess.evaluate(request);
  String freifunkDataQuery = "http://www.freifunk-karte.de/data.php";
  // manage DoS
  if (post.isDoS_blackout()) {response.sendError(503, "your request frequency is too high"); return;}
  JSONObject json = readJsonFromUrl(freifunkDataQuery);
  PrintWriter sos = response.getWriter();
  sos.print(json.toString(2));
  sos.println();
}

代码示例来源:origin: Netflix/concurrency-limits

protected void outputThrottleError(HttpServletResponse response) {
  try {
    response.setStatus(STATUS_TOO_MANY_REQUESTS);
    response.getWriter().print("Concurrency limit exceeded");
  } catch (IOException e) {
  }
}

相关文章