下面列出了java.io.InputStreamReader#ready ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static boolean load(String filename) throws IOException {
// Clear the board
Lizzie.board.clear();
File file = new File(filename);
if (!file.exists() || !file.canRead()) {
return false;
}
String encoding = EncodingDetector.detect(filename);
FileInputStream fp = new FileInputStream(file);
InputStreamReader reader = new InputStreamReader(fp, encoding);
StringBuilder builder = new StringBuilder();
while (reader.ready()) {
builder.append((char) reader.read());
}
reader.close();
fp.close();
String value = builder.toString();
if (value.isEmpty()) {
return false;
}
boolean returnValue = parse(value);
return returnValue;
}
@Override public void run() {
String secretName = config.name;
if (secretName == null || !validName(secretName)) {
throw new IllegalArgumentException(format("Invalid name, must match %s", VALID_NAME_PATTERN));
}
byte[] content = {};
if (config.contentProvided) {
content = readSecretContent();
}
partialUpdateSecret(secretName, content, config);
// If it appears that content was piped in but --content was not specified, print a warning
if (!config.contentProvided) {
try {
InputStreamReader reader = new InputStreamReader(stream, "UTF-8");
if (reader.ready()) {
System.out.println("\nWarning: Specify the --content flag to update a secret's content.");
System.out.println("The secret has not been updated with any provided content.");
}
} catch (IOException e) {
logger.warn("Unexpected error trying to create an InputStreamReader for stdin: '{}'", e.getMessage());
}
}
}
static String execute(String action, List<String> command) throws IOException {
final StringBuilder processLog = new StringBuilder();
final Process process = new ProcessBuilder().command(command).redirectErrorStream(true).start();
processLog.append("Command: ");
Joiner.on("\\\n\t").appendTo(processLog, command);
processLog.append("\nOutput:\n");
final InputStreamReader stdout = new InputStreamReader(process.getInputStream(), UTF_8);
while (process.isAlive()) {
processLog.append(CharStreams.toString(stdout));
}
// Make sure the full stdout is read.
while (stdout.ready()) {
processLog.append(CharStreams.toString(stdout));
}
if (process.exitValue() != 0) {
throw new RuntimeException(String.format("Error during %s:", action) + "\n" + processLog);
}
return processLog.toString();
}
@Nonnull
public static String loadText(@Nonnull URL url) throws IOException {
InputStream inputStream = new BufferedInputStream(URLUtil.openStream(url));
InputStreamReader reader = new InputStreamReader(inputStream, ENCODING_UTF_8);
try {
StringBuilder text = new StringBuilder();
char[] buf = new char[5000];
while (reader.ready()) {
final int length = reader.read(buf);
if (length == -1) break;
text.append(buf, 0, length);
}
return text.toString();
}
finally {
reader.close();
}
}
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();
}
public QueryStream(Response response) {
this.response = response;
this.objectMapper = new ObjectMapper();
InputStreamReader isr = new InputStreamReader(
(InputStream) response.getEntity(),
StandardCharsets.UTF_8
);
QueryStream stream = this;
this.responseScanner = new Scanner((buf) -> {
int wait = 1;
// poll the input stream's readiness between interruptable sleeps
// this ensures we cannot block indefinitely on read()
while (true) {
if (closed) {
throw stream.closedIllegalStateException("hasNext()");
}
if (isr.ready()) {
break;
}
synchronized (stream) {
if (closed) {
throw stream.closedIllegalStateException("hasNext()");
}
try {
wait = java.lang.Math.min(wait * 2, 200);
stream.wait(wait);
} catch (InterruptedException e) {
// this is expected
// just check the closed flag
}
}
}
return isr.read(buf);
});
this.bufferedRow = null;
}
/**
* 字节流
* 创建一个文件并读取记录 防止乱码
* @throws IOException
*/
private static void test4() throws IOException {
String path="E:/test/hello.txt";
String path2="E:/test/你好.txt";
String str="你好!";
//从文件读取数据
InputStream input = new FileInputStream(path);
InputStreamReader reader = new InputStreamReader(input, "UTF-8");
StringBuffer sb=new StringBuffer();
while(reader.ready()){
sb.append((char)reader.read());
}
input.close();
reader.close();
//创建一个文件并向文件中写数据
OutputStream output = new FileOutputStream(path2);
OutputStreamWriter writer = new OutputStreamWriter(output, "UTF-8");
writer.write(sb+str);
writer.close();
output.close();
//从文件读取数据
InputStream input2 = new FileInputStream(path2);
InputStreamReader reader2 = new InputStreamReader(input2, "UTF-8");
StringBuffer sb2=new StringBuffer();
while(reader2.ready()){
sb2.append((char)reader2.read());
}
System.out.println("输出:"+sb2);
input2.close();
reader2.close();
}
public static boolean load(String filename) throws IOException {
// Clear the board
Lizzie.board.clear();
File file = new File(filename);
if (!file.exists() || !file.canRead()) {
return false;
}
String encoding = EncodingDetector.detect(filename);
if (encoding == "WINDOWS-1252") encoding = "gb2312";
FileInputStream fp = new FileInputStream(file);
InputStreamReader reader = new InputStreamReader(fp, encoding);
StringBuilder builder = new StringBuilder();
while (reader.ready()) {
builder.append((char) reader.read());
}
reader.close();
fp.close();
String value = builder.toString();
if (value.isEmpty()) {
return false;
}
boolean returnValue = parse(value);
return returnValue;
}
protected String readResourceAsString(String name) throws IOException {
try (InputStream stream = getClass().getResourceAsStream(name)) {
StringBuilder builder = new StringBuilder();
InputStreamReader reader = new InputStreamReader(stream);
char[] buf = new char[4096];
while (reader.ready()) {
int len = reader.read(buf);
builder.append(buf, 0, len);
}
return builder.toString();
}
}
public static void fileStream() {
try {
File f = new File("mkdirs/test/filetest.txt");
FileOutputStream fop = new FileOutputStream(f);
// 构建FileOutputStream对象,文件不存在会自动新建
OutputStreamWriter writer = new OutputStreamWriter(fop, "gbk");
// 构建OutputStreamWriter对象,参数可以指定编码,默认为操作系统默认编码,windows上是gbk
writer.append("中文输入");
// 写入到缓冲区
writer.append("\r\n");
// 换行
writer.append("English");
// 刷新缓存冲,写入到文件,如果下面已经没有写入的内容了,直接close也会写入
writer.close();
// 关闭写入流,同时会把缓冲区内容写入文件,所以上面的注释掉
fop.close();
// 关闭输出流,释放系统资源
FileInputStream fip = new FileInputStream(f);
// 构建FileInputStream对象
InputStreamReader reader = new InputStreamReader(fip, "UTF-8");
// 构建InputStreamReader对象,编码与写入相同
StringBuffer sb = new StringBuffer();
while (reader.ready()) {
sb.append((char) reader.read());
// 转成char加到StringBuffer对象中
}
System.out.println(sb.toString());
reader.close();
// 关闭读取流
fip.close();
// 关闭输入流,释放系统资源
} catch (Exception e) {
}
}