java.io.InputStreamReader.read()方法的使用及代码示例

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

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

InputStreamReader.read介绍

[英]Reads a single character from this reader and returns it as an integer with the two higher-order bytes set to 0. Returns -1 if the end of the reader has been reached. The byte value is either obtained from converting bytes in this reader's buffer or by first filling the buffer from the source InputStream and then reading from the buffer.
[中]从该读取器读取单个字符,并将其作为两个高阶字节设置为0的整数返回。如果已到达读卡器的末端,则返回-1。字节值可以通过转换读取器缓冲区中的字节来获得,也可以通过先从源InputStream填充缓冲区,然后从缓冲区读取来获得。

代码示例

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

/** Copy the data from an {@link InputStream} to a string using the specified charset.
 * @param estimatedSize Used to allocate the output buffer to possibly avoid an array copy.
 * @param charset May be null to use the platform's default charset. */
public static String copyStreamToString (InputStream input, int estimatedSize, String charset) throws IOException {
  InputStreamReader reader = charset == null ? new InputStreamReader(input) : new InputStreamReader(input, charset);
  StringWriter writer = new StringWriter(Math.max(0, estimatedSize));
  char[] buffer = new char[DEFAULT_BUFFER_SIZE];
  int charsRead;
  while ((charsRead = reader.read(buffer)) != -1) {
    writer.write(buffer, 0, charsRead);
  }
  return writer.toString();
}

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

/** Reads the entire file into a string using the specified charset.
 * @throw RuntimeException if the file handle represents a directory, doesn't exist, or could not be read. */
public String readString (String charset) {
  StringBuilder output = new StringBuilder(512);
  InputStreamReader reader = null;
  try {
    if (charset == null)
      reader = new InputStreamReader(read());
    else
      reader = new InputStreamReader(read(), charset);
    char[] buffer = new char[256];
    while (true) {
      int length = reader.read(buffer);
      if (length == -1) break;
      output.append(buffer, 0, length);
    }
  } catch (IOException ex) {
    throw new RuntimeException("Error reading layout file: " + this, ex);
  } finally {
    try {
      if (reader != null) reader.close();
    } catch (IOException ignored) {
    }
  }
  return output.toString();
}

代码示例来源:origin: jamesagnew/hapi-fhir

private String readSource() throws IOException  {
 StringBuilder s = new StringBuilder();
 InputStreamReader r = new InputStreamReader(source,"UTF-8");
 while (r.ready()) {
  s.append((char) r.read()); 
 }
 r.close();
 return s.toString();
}

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

c = reader.read();
if (c != -1) {
 response.getWriter().write(c);
reader.close();

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

/** Copy the data from an {@link InputStream} to a string using the specified charset.
 * @param estimatedSize Used to allocate the output buffer to possibly avoid an array copy.
 * @param charset May be null to use the platform's default charset. */
public static String copyStreamToString (InputStream input, int estimatedSize, String charset) throws IOException {
  InputStreamReader reader = charset == null ? new InputStreamReader(input) : new InputStreamReader(input, charset);
  StringWriter writer = new StringWriter(Math.max(0, estimatedSize));
  char[] buffer = new char[DEFAULT_BUFFER_SIZE];
  int charsRead;
  while ((charsRead = reader.read(buffer)) != -1) {
    writer.write(buffer, 0, charsRead);
  }
  return writer.toString();
}

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

/** Reads the entire file into a string using the specified charset.
 * @throw RuntimeException if the file handle represents a directory, doesn't exist, or could not be read. */
public String readString (String charset) {
  StringBuilder output = new StringBuilder(512);
  InputStreamReader reader = null;
  try {
    if (charset == null)
      reader = new InputStreamReader(read());
    else
      reader = new InputStreamReader(read(), charset);
    char[] buffer = new char[256];
    while (true) {
      int length = reader.read(buffer);
      if (length == -1) break;
      output.append(buffer, 0, length);
    }
  } catch (IOException ex) {
    throw new RuntimeException("Error reading layout file: " + this, ex);
  } finally {
    try {
      if (reader != null) reader.close();
    } catch (IOException ignored) {
    }
  }
  return output.toString();
}

代码示例来源:origin: ca.uhn.hapi.fhir/hapi-fhir-utilities

private String readSource() throws IOException  {
 StringBuilder s = new StringBuilder();
 InputStreamReader r = new InputStreamReader(source,"UTF-8");
 while (r.ready()) {
  s.append((char) r.read()); 
 }
 r.close();
 return s.toString();
}

代码示例来源:origin: org.fudaa.soft.fudaa-refonde/refonde-server

public void run() {
 int ch;
 while (!stopReading_) {
  try { while ((ch=is_.read())!=-1) {
   sb_.append((char)ch);
  } }
  catch (final IOException _exc) { stopReading_=true; }
  try { Thread.sleep(100); } catch (final InterruptedException _exc) {}
 }
 try { is_.close(); } catch (final IOException _exc) {}
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Copy the contents of the given InputStream into a String.
 * Leaves the stream open when done.
 * @param in the InputStream to copy from (may be {@code null} or empty)
 * @return the String that has been copied to (possibly empty)
 * @throws IOException in case of I/O errors
 */
public static String copyToString(@Nullable InputStream in, Charset charset) throws IOException {
  if (in == null) {
    return "";
  }
  StringBuilder out = new StringBuilder();
  InputStreamReader reader = new InputStreamReader(in, charset);
  char[] buffer = new char[BUFFER_SIZE];
  int bytesRead = -1;
  while ((bytesRead = reader.read(buffer)) != -1) {
    out.append(buffer, 0, bytesRead);
  }
  return out.toString();
}

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

public String serialize(InputStream in) {
  InputStreamReader reader = null;
  int temp;
  StringBuilder item = new StringBuilder();
  boolean eof = false;
  try {
    reader = new InputStreamReader(in);
    while (!eof) {
      temp = reader.read();
      if (temp == -1) {
        eof = true;
      } else {
        item.append((char) temp);
      }
    }
  } catch (IOException e) {
    LOG.error("Unable to merge source and patch locations", e);
  } finally {
    if (reader != null) {
      try{ reader.close(); } catch (Throwable e) {
        LOG.error("Unable to merge source and patch locations", e);
      }
    }
  }
  return item.toString();
}

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

InputStreamReader reader = new InputStreamReader(
    yourInputStream, "windows-1252"); 
    // or what ever seems to be the correct encoding!

StringBuilder builder = new StringBuilder();

while (reader.ready())
{
  builder.append(reader.read());
}
reader.close();

String string = builder.toString();

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

FileReader fin = null
try {
  fin = new FileReader("numbers.txt");
  int in = -1;
  while((in = fin.read()) > -1 ){
    System.out.print((char)in);
  }
} finally {
  try {
    fin.close(); 
  } catch (Exception exp) {}
}

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

public static String toString(InputStream input) throws IOException {
  if (input == null) {
    return null;
  }
  final InputStreamReader inputStreamReader = new InputStreamReader(input);
  final StringBuilder stringBuilder = new StringBuilder();
  final char[] buffer = new char[BUFFER_SIZE];
  int n = 0;
  while (EOF != (n = inputStreamReader.read(buffer))) {
    stringBuilder.append(buffer, 0, n);
  }
  return stringBuilder.toString();
}

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

public String serialize(InputStream in) {
  InputStreamReader reader = null;
  int temp;
  StringBuilder item = new StringBuilder();
  boolean eof = false;
  try {
    reader = new InputStreamReader(in);
    while (!eof) {
      temp = reader.read();
      if (temp == -1) {
        eof = true;
      } else {
        item.append((char) temp);
      }
    }
  } catch (IOException e) {
    LOG.error("Unable to merge source and patch locations", e);
  } finally {
    if (reader != null) {
      try{ reader.close(); } catch (Throwable e) {
        LOG.error("Unable to merge source and patch locations", e);
      }
    }
  }
  return item.toString();
}

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

File inputFile = new File("input.txt");
File outputFile = new File("output.txt");

FileReader in = new FileReader(inputFile);
FileWriter out = new FileWriter(outputFile);
int c;

while ((c = in.read()) != -1)
 out.write(c);

in.close();
out.close();

代码示例来源:origin: org.springframework/spring-core

/**
 * Copy the contents of the given InputStream into a String.
 * Leaves the stream open when done.
 * @param in the InputStream to copy from (may be {@code null} or empty)
 * @return the String that has been copied to (possibly empty)
 * @throws IOException in case of I/O errors
 */
public static String copyToString(@Nullable InputStream in, Charset charset) throws IOException {
  if (in == null) {
    return "";
  }
  StringBuilder out = new StringBuilder();
  InputStreamReader reader = new InputStreamReader(in, charset);
  char[] buffer = new char[BUFFER_SIZE];
  int bytesRead = -1;
  while ((bytesRead = reader.read(buffer)) != -1) {
    out.append(buffer, 0, bytesRead);
  }
  return out.toString();
}

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

private String readInputStream(InputStream is, int size, int messageNumber) throws IOException {
  InputStreamReader reader = new InputStreamReader(is);
  try {
    char[] buffer;
    if (size > 0) {
      buffer = new char[size];
    } else {
      buffer = new char[1024];
    }
    int count;
    StringBuilder builder = new StringBuilder();
    while ((count = reader.read(buffer)) != -1) {
      builder.append(buffer, 0, count);
      if (size > 0) break;
    }
    return builder.toString();
  } catch (IOException ioe) {
    return createDefaultMessage(messageNumber);
  } finally {
    reader.close();
  }
}

代码示例来源:origin: Alluxio/alluxio

/**
 * Checks whether an Alluxio service is running.
 *
 * @param className class name of the Alluxio service
 * @return whether the Alluxio service is running
 */
public static boolean isAlluxioRunning(String className) {
 String[] command = {"bash", "-c",
           "ps -Aww -o command | grep -i \"[j]ava\" | grep " + className};
 try {
  Process p = Runtime.getRuntime().exec(command);
  try (InputStreamReader input = new InputStreamReader(p.getInputStream())) {
   if (input.read() >= 0) {
    return true;
   }
  }
  return false;
 } catch (IOException e) {
  System.err.format("Unable to check Alluxio status: %s.%n", e.getMessage());
  return false;
 }
}

代码示例来源:origin: jiangqqlmj/FastDev4Android

public static String file2String(File file, String encoding) {
  InputStreamReader reader = null;
  StringWriter writer = new StringWriter();
  try {
    if (encoding == null || "".equals(encoding.trim())) {
      reader = new InputStreamReader(new FileInputStream(file));
    } else {
      reader = new InputStreamReader(new FileInputStream(file),
          encoding);
    }
    // 将输入流写入输出流
    char[] buffer = new char[BUFF_SIZE];
    int n = 0;
    while (-1 != (n = reader.read(buffer))) {
      writer.write(buffer, 0, n);
    }
  } catch (Exception e) {
    e.printStackTrace();
    return null;
  } finally {
    if (reader != null)
      try {
        reader.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
  }
  return writer.toString();
}

代码示例来源:origin: liyiorg/weixin-popular

/**
 * Copy the contents of the given InputStream into a String.
 * Leaves the stream open when done.
 * @param in the InputStream to copy from
 * @param charset charset
 * @return the String that has been copied to
 * @throws IOException in case of I/O errors
 */
public static String copyToString(InputStream in, Charset charset) throws IOException {
  StringBuilder out = new StringBuilder();
  InputStreamReader reader = new InputStreamReader(in, charset);
  char[] buffer = new char[BUFFER_SIZE];
  int bytesRead = -1;
  while ((bytesRead = reader.read(buffer)) != -1) {
    out.append(buffer, 0, bytesRead);
  }
  return out.toString();
}

相关文章