本文整理了Java中java.io.Reader
类的一些代码示例,展示了Reader
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Reader
类的具体详情如下:
包路径:java.io.Reader
类名称:Reader
[英]The base class for all readers. A reader is a means of reading data from a source in a character-wise manner. Some readers also support marking a position in the input and returning to this position later.
This abstract class does not provide a fully working implementation, so it needs to be subclassed, and at least the #read(char[],int,int) and #close() methods needs to be overridden. Overriding some of the non-abstract methods is also often advised, since it might result in higher efficiency.
Many specialized readers for purposes like reading from a file already exist in this package.
[中]所有读者的基类。读卡器是一种以字符方式从源中读取数据的方法。一些读者还支持在输入中标记一个位置,然后返回到该位置。
这个抽象类没有提供完全工作的实现,因此需要对它进行子类化,至少需要重写#read(char[],int,int)和#close()方法。还经常建议重写一些非抽象方法,因为这样可能会提高效率。
这个包中已经存在许多专门的阅读器,用于读取文件等目的。
代码示例来源:origin: stackoverflow.com
InputStream is = getResources().openRawResource(R.raw.json_file);
Writer writer = new StringWriter();
char[] buffer = new char[1024];
try {
Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
int n;
while ((n = reader.read(buffer)) != -1) {
writer.write(buffer, 0, n);
}
} finally {
is.close();
}
String jsonString = writer.toString();
代码示例来源:origin: stackoverflow.com
Properties properties = new Properties();
InputStream inputStream = new FileInputStream("path/to/file");
try {
Reader reader = new InputStreamReader(inputStream, "UTF-8");
try {
properties.load(reader);
} finally {
reader.close();
}
} finally {
inputStream.close();
}
代码示例来源:origin: jersey/jersey
private static Reader ensureNonEmptyReader(Reader reader) throws XMLStreamException {
try {
Reader mr = reader.markSupported() ? reader : new BufferedReader(reader);
mr.mark(1);
if (mr.read() == -1) {
throw new XMLStreamException("JSON expression can not be empty!");
}
mr.reset();
return mr;
} catch (IOException ex) {
throw new XMLStreamException(ex);
}
}
代码示例来源:origin: json-path/JsonPath
private static String convertReaderToString(Reader reader)
throws IOException {
if (reader != null) {
Writer writer = new StringWriter();
char[] buffer = new char[1024];
try {
int n;
while ((n = reader.read(buffer)) != -1) {
writer.write(buffer, 0, n);
}
} finally {
reader.close();
}
return writer.toString();
} else {
return "";
}
}
代码示例来源:origin: stackoverflow.com
URL url = new URL("http://stackoverflow.com/questions/1381617");
URLConnection con = url.openConnection();
Pattern p = Pattern.compile("text/html;\\s+charset=([^\\s]+)\\s*");
Matcher m = p.matcher(con.getContentType());
/* If Content-Type doesn't match this pre-conception, choose default and
* hope for the best. */
String charset = m.matches() ? m.group(1) : "ISO-8859-1";
Reader r = new InputStreamReader(con.getInputStream(), charset);
StringBuilder buf = new StringBuilder();
while (true) {
int ch = r.read();
if (ch < 0)
break;
buf.append((char) ch);
}
String str = buf.toString();
代码示例来源:origin: stackoverflow.com
InputStream ins = null; // raw byte-stream
Reader r = null; // cooked reader
BufferedReader br = null; // buffered for readLine()
try {
String s;
if (true) {
String data = "#foobar\t1234\n#xyz\t5678\none\ttwo\n";
ins = new ByteArrayInputStream(data.getBytes());
} else {
ins = new FileInputStream("textfile.txt");
}
r = new InputStreamReader(ins, "UTF-8"); // leave charset out for default
br = new BufferedReader(r);
while ((s = br.readLine()) != null) {
System.out.println(s);
}
}
catch (Exception e)
{
System.err.println(e.getMessage()); // handle exception
}
finally {
if (br != null) { try { br.close(); } catch(Throwable t) { /* ensure close happens */ } }
if (r != null) { try { r.close(); } catch(Throwable t) { /* ensure close happens */ } }
if (ins != null) { try { ins.close(); } catch(Throwable t) { /* ensure close happens */ } }
}
代码示例来源:origin: stackoverflow.com
StringBuilder sb = new StringBuilder();
int cp;
while ((cp = rd.read()) != -1) {
sb.append((char) cp);
InputStream is = new URL(url).openStream();
try {
BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
String jsonText = readAll(rd);
JSONObject json = new JSONObject(jsonText);
return json;
} finally {
is.close();
代码示例来源:origin: apache/ignite
/**
* Reads file to string using specified charset.
*
* @param fileName File name.
* @param charset File charset.
* @return File content.
* @throws IOException If error occurred.
*/
public static String readFileToString(String fileName, Charset charset) throws IOException {
Reader input = new InputStreamReader(new FileInputStream(fileName), charset);
StringWriter output = new StringWriter();
char[] buf = new char[4096];
int n;
while ((n = input.read(buf)) != -1)
output.write(buf, 0, n);
return output.toString();
}
}
代码示例来源:origin: stackoverflow.com
private static String readUrl(String urlString) throws Exception {
BufferedReader reader = null;
try {
URL url = new URL(urlString);
reader = new BufferedReader(new InputStreamReader(url.openStream()));
StringBuffer buffer = new StringBuffer();
int read;
char[] chars = new char[1024];
while ((read = reader.read(chars)) != -1)
buffer.append(chars, 0, read);
return buffer.toString();
} finally {
if (reader != null)
reader.close();
}
}
代码示例来源:origin: weibocom/motan
/**
* Parses the JSON input file containing the list of features.
*/
public static List<Feature> parseFeatures(URL file) throws IOException {
InputStream input = file.openStream();
try {
Reader reader = new InputStreamReader(input);
try {
FeatureDatabase.Builder database = FeatureDatabase.newBuilder();
JsonFormat.parser().merge(reader, database);
return database.getFeatureList();
} finally {
reader.close();
}
} finally {
input.close();
}
}
代码示例来源:origin: org.codehaus.plexus/plexus-utils
fileReader = new BufferedReader( new FileReader( from ) );
fileWriter = new FileWriter( to );
FileInputStream instream = new FileInputStream( from );
fileReader = new BufferedReader( new InputStreamReader( instream, encoding ) );
fileWriter.close();
fileWriter = null;
fileReader.close();
fileReader = null;
代码示例来源:origin: stackoverflow.com
public static String readFile(String file, String csName)
throws IOException {
Charset cs = Charset.forName(csName);
return readFile(file, cs);
}
public static String readFile(String file, Charset cs)
throws IOException {
// No real need to close the BufferedReader/InputStreamReader
// as they're only wrapping the stream
FileInputStream stream = new FileInputStream(file);
try {
Reader reader = new BufferedReader(new InputStreamReader(stream, cs));
StringBuilder builder = new StringBuilder();
char[] buffer = new char[8192];
int read;
while ((read = reader.read(buffer, 0, buffer.length)) > 0) {
builder.append(buffer, 0, read);
}
return builder.toString();
} finally {
// Potential issue here: if this throws an IOException,
// it will mask any others. Normally I'd use a utility
// method which would log exceptions and swallow them
stream.close();
}
}
代码示例来源:origin: internetarchive/heritrix3
if (uri.trim().length() == 0) {
File source = getMapPath().getFile();
reader = new FileReader(source);
} else {
URLConnection conn = (new URL(uri)).openConnection();
reader = new InputStreamReader(conn.getInputStream());
reader = new BufferedReader(reader);
Iterator<String> iter =
new RegexLineIterator(
map.put(entry[0],entry[1]);
reader.close();
代码示例来源:origin: org.apache.hadoop/hadoop-common
String extractPassword(String pwFile) {
if (pwFile.isEmpty()) {
// If there is no password file defined, we'll assume that we should do
// an anonymous bind
return "";
}
StringBuilder password = new StringBuilder();
try (Reader reader = new InputStreamReader(
new FileInputStream(pwFile), StandardCharsets.UTF_8)) {
int c = reader.read();
while (c > -1) {
password.append((char)c);
c = reader.read();
}
return password.toString().trim();
} catch (IOException ioe) {
throw new RuntimeException("Could not read password file: " + pwFile, ioe);
}
}
}
代码示例来源:origin: oracle/opengrok
new InputStreamReader(new FileInputStream(
source + filename), StandardCharsets.UTF_8),
null, null, null, filename, tags, nhits > 100,
? new HTMLStripCharFilter(new BufferedReader(new InputStreamReader(new GZIPInputStream(new FileInputStream(
TandemPath.join(data + Prefix.XREF_P + filename, ".gz"))))))
: new HTMLStripCharFilter(new BufferedReader(new FileReader(data + Prefix.XREF_P + filename)))) {
l = r.read(content);
代码示例来源:origin: stanfordnlp/CoreNLP
/**
* Loads treebank data from first argument and prints it.
*
* @param args Array of command-line arguments: specifies a filename
*/
public static void main(String[] args) {
try {
TreeFactory tf = new LabeledScoredTreeFactory();
Reader r = new BufferedReader(new InputStreamReader(new FileInputStream(args[0]), "UTF-8"));
TreeReader tr = new PennTreeReader(r, tf);
Tree t = tr.readTree();
while (t != null) {
System.out.println(t);
System.out.println();
t = tr.readTree();
}
r.close();
} catch (IOException ioe) {
throw new RuntimeIOException(ioe);
}
}
代码示例来源:origin: stackoverflow.com
writer().write(chars);
writer().close();
this.reader = new BufferedReader(new FileReader(filepath));
this.read_ahead = this.reader.readLine();
reader = new BufferedReader(new FileReader(filepath));
reader().close();
代码示例来源:origin: stackoverflow.com
private String readFileAsString(String filePath) throws IOException {
StringBuffer fileData = new StringBuffer();
BufferedReader reader = new BufferedReader(
new FileReader(filePath));
char[] buf = new char[1024];
int numRead=0;
while((numRead=reader.read(buf)) != -1){
String readData = String.valueOf(buf, 0, numRead);
fileData.append(readData);
}
reader.close();
return fileData.toString();
}
代码示例来源:origin: stackoverflow.com
Process nativeApp = Runtime.getRuntime().exec("/data/data/com.yourdomain.yourapp/nativeFolder/application");
BufferedReader reader = new BufferedReader(new InputStreamReader(nativeApp.getInputStream()));
int read;
char[] buffer = new char[4096];
StringBuffer output = new StringBuffer();
while ((read = reader.read(buffer)) > 0) {
output.append(buffer, 0, read);
}
reader.close();
// Waits for the command to finish.
nativeApp.waitFor();
String nativeOutput = output.toString();
代码示例来源:origin: ltsopensource/light-task-scheduler
private static String getStreamAsString(InputStream stream, String charset) throws IOException {
try {
Reader reader = new InputStreamReader(stream, charset);
StringBuilder response = new StringBuilder();
final char[] buff = new char[1024];
int read = 0;
while ((read = reader.read(buff)) > 0) {
response.append(buff, 0, read);
}
return response.toString();
} finally {
if (stream != null) {
stream.close();
}
}
}
内容来源于网络,如有侵权,请联系作者删除!