本文整理匯總了Java中java.io.InputStreamReader類的典型用法代碼示例。如果您正苦於以下問題:Java InputStreamReader類的具體用法?Java InputStreamReader怎麽用?Java InputStreamReader使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
InputStreamReader類屬於java.io包,在下文中一共展示了InputStreamReader類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: getReader
import java.io.InputStreamReader; //導入依賴的package包/類
/**
* This method checks if the given file is a Zip file containing one entry (in case of file
* extension .zip). If this is the case, a reader based on a ZipInputStream for this entry is
* returned. Otherwise, this method checks if the file has the extension .gz. If this applies, a
* gzipped stream reader is returned. Otherwise, this method just returns a BufferedReader for
* the given file (file was not zipped at all).
*/
public static BufferedReader getReader(File file, Charset encoding) throws IOException {
// handle zip files if necessary
if (file.getAbsolutePath().endsWith(".zip")) {
try (ZipFile zipFile = new ZipFile(file)) {
if (zipFile.size() == 0) {
throw new IOException("Input of Zip file failed: the file archive does not contain any entries.");
}
if (zipFile.size() > 1) {
throw new IOException("Input of Zip file failed: the file archive contains more than one entry.");
}
Enumeration<? extends ZipEntry> entries = zipFile.entries();
InputStream zipIn = zipFile.getInputStream(entries.nextElement());
return new BufferedReader(new InputStreamReader(zipIn, encoding));
}
} else if (file.getAbsolutePath().endsWith(".gz")) {
return new BufferedReader(new InputStreamReader(new GZIPInputStream(new FileInputStream(file)), encoding));
} else {
return new BufferedReader(new InputStreamReader(new FileInputStream(file), encoding));
}
}
示例2: buildModSource
import java.io.InputStreamReader; //導入依賴的package包/類
public String buildModSource() {
String template;
try (BufferedReader buffer = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("/cslmusicmod/stationeditor/Mod.cs")))) {
template = buffer.lines().collect(Collectors.joining("\n"));
} catch (IOException e) {
throw new IllegalStateException();
}
String namespace = "MusicPack_" + getName().trim().replaceAll("[^a-zA-Z0-9]" , "_");
template = template.replace("__NAME__", getName())
.replace("__DESCRIPTION__", getDescription())
.replace("__NAMESPACE__", namespace);
return template;
}
示例3: read
import java.io.InputStreamReader; //導入依賴的package包/類
public static JSONObject read(final String url) throws Exception {
Exception exception = null;
for (int i = 0; i < 3; i++) {
Expectant.delay((i + 1) * API_DELAY_MS);
try (InputStream is = new URL(url).openStream()) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
String jsonText = readWhole(reader);
JSONObject json = new JSONObject(jsonText);
return json;
} catch (Exception ex) {
System.err.println("Failed read url: " + url
+ " try #" + (i + 1) + "\n" + ex.getMessage());
exception = ex;
}
}
throw exception;
}
示例4: findPatternsInLogs
import java.io.InputStreamReader; //導入依賴的package包/類
public static boolean[] findPatternsInLogs(DockerContainer dockerContainer, Pattern... patterns) throws IOException {
boolean found[] = new boolean[patterns.length];
dockerContainer.dockerLogs(istream -> {
try (BufferedReader br = new BufferedReader(new InputStreamReader(istream, "UTF-8"))) {
br.lines().forEach(line -> {
for (int i = 0; i < patterns.length; ++i) {
Pattern pattern = patterns[i];
if (pattern.matcher(line).find()) {
LOGGER.info("Found pattern {} on line '{}'", pattern, LogCleaner.cleanLine(line));
found[i] = true;
}
}
});
}
});
return found;
}
示例5: readStream
import java.io.InputStreamReader; //導入依賴的package包/類
/**
*
*
* @param coordstream
* InputStream
* @param bufferSize
* int
* @return result String
* @throws IOException
*/
public static String readStream(InputStream stream, int bufferSize, String encoding) throws IOException {
StringWriter sw = new StringWriter();
int bytesRead;
if (!Helper.isEmpty(encoding)) {
BufferedReader br = new BufferedReader(new InputStreamReader(stream, encoding), bufferSize);
String str;
while ((str = br.readLine()) != null) {
sw.write(str);
}
} else {
byte[] buffer = new byte[bufferSize];
while ((bytesRead = stream.read(buffer)) != -1) {
sw.write(new String(buffer, 0, bytesRead));
}
}
return sw.toString();
}
示例6: loadContent
import java.io.InputStreamReader; //導入依賴的package包/類
@Override
public final boolean loadContent(final File file) {
try {
FileInputStream fis = new FileInputStream(file);
DataInputStream dis = new DataInputStream(fis);
BufferedReader br = new BufferedReader(new InputStreamReader(dis));
StringBuilder sb = new StringBuilder();
String strLine;
while ((strLine = br.readLine()) != null) {
sb.append(strLine);
sb.append("\n");
}
if (sb.length() != 0) {
sb.deleteCharAt(sb.length() - 1);
}
setText(sb.toString());
dis.close();
return true;
} catch (IOException e) {
LOG.warning("KH: could not load specified file: " + file.getPath());
e.printStackTrace();
}
return false;
}
示例7: loadJS
import java.io.InputStreamReader; //導入依賴的package包/類
private String loadJS() {
StringBuilder buf = new StringBuilder();
try {
InputStream is = getResources().getAssets().open("youtube_inject.js");
BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
String str;
while ((str = br.readLine()) != null) {
buf.append(str);
}
br.close();
} catch (Throwable throwable) {
throwable.printStackTrace();
}
return buf.toString();
}
示例8: onCreate
import java.io.InputStreamReader; //導入依賴的package包/類
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_aboutus);
Intent incoming = getIntent();
Bundle extras = incoming.getExtras();
String uri = "html/about.html";
if(extras != null){
uri = extras.getString(EXTRA_HTML_URI);
}
mWebView = (WebView) findViewById(R.id.webView);
mCloseBtn = (ImageButton) findViewById(R.id.closeButton);
mCloseBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onBackPressed();
}
});
InputStream is;
String htmlData = "";
try {
is = this.getAssets().open(uri);
BufferedReader r = new BufferedReader(new InputStreamReader(is));
StringBuilder stringBuilder = new StringBuilder();
String line;
while( (line=r.readLine()) != null ) {
stringBuilder.append(line);
}
htmlData = stringBuilder.toString();
} catch( IOException error ) {}
mWebView.loadDataWithBaseURL("file:///android_asset/", htmlData, "text/html", "utf-8", "about:blank");
}
示例9: main
import java.io.InputStreamReader; //導入依賴的package包/類
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s;
String[] parts;
int testsNum = Integer.parseInt(br.readLine());
for (int t = 0; t < testsNum; t++) {
arrLength = Integer.parseInt(br.readLine());
s = br.readLine();
parts = s.split(" ", -1);
for (int i = 0; i < arrLength; i++) {
arr[i] = Integer.parseInt(parts[i]);
}
int result = solveProblem();
System.out.println(result);
}
}
示例10: main
import java.io.InputStreamReader; //導入依賴的package包/類
public static void main(String[] args) throws Exception {
// Считать строки с консоли и объявить ArrayList list тут
ArrayList <String> list = new ArrayList<>();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
for (int i=0; i<10; i++) {
list.add(br.readLine());
}
ArrayList<String> result = doubleValues(list);
// Вывести на экран result
for (String s: result) {
System.out.println(s);
}
}
示例11: commandLoop
import java.io.InputStreamReader; //導入依賴的package包/類
private void commandLoop() throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
while (true) {
TerminalCommand tc = Terminal.create(in.readLine());
if (tc instanceof TerminalCommand.Guest) {
TerminalCommand.Guest tcg = (TerminalCommand.Guest) tc;
createGuest(tcg.count, tcg.coffee, tcg.maxCoffeeCount);
} else if (tc == TerminalCommand.Status.Instance) {
getStatus();
} else if (tc == TerminalCommand.Quit.Instance) {
system.terminate();
break;
} else {
TerminalCommand.Unknown u = (TerminalCommand.Unknown) tc;
log.warning("Unknown terminal command {}!", u.command);
}
}
}
示例12: readFieldLayout
import java.io.InputStreamReader; //導入依賴的package包/類
static Map<String, Object> readFieldLayout(int level) {
try {
String assetPath = "tables/table" + level + ".json";
InputStream fin = _context.getAssets().open(assetPath);
BufferedReader br = new BufferedReader(new InputStreamReader(fin));
StringBuilder buffer = new StringBuilder();
String line;
while ((line=br.readLine())!=null) {
buffer.append(line);
}
fin.close();
Map<String, Object> layoutMap = JSONUtils.mapFromJSONString(buffer.toString());
return layoutMap;
}
catch(Exception ex) {
throw new RuntimeException(ex);
}
}
示例13: getNextPreKeyId
import java.io.InputStreamReader; //導入依賴的package包/類
private static synchronized int getNextPreKeyId(Context context) {
try {
File nextFile = new File(getPreKeysDirectory(context), PreKeyIndex.FILE_NAME);
if (!nextFile.exists()) {
return Util.getSecureRandom().nextInt(Medium.MAX_VALUE);
} else {
InputStreamReader reader = new InputStreamReader(new FileInputStream(nextFile));
PreKeyIndex index = JsonUtils.fromJson(reader, PreKeyIndex.class);
reader.close();
return index.nextPreKeyId;
}
} catch (IOException e) {
Log.w("PreKeyUtil", e);
return Util.getSecureRandom().nextInt(Medium.MAX_VALUE);
}
}
示例14: main
import java.io.InputStreamReader; //導入依賴的package包/類
public static void main(String ... ags)throws IOException
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String str = in.readLine();
long n = Long.parseLong(in.readLine());
long a =0;
for(int i=0;i<str.length();i++)
{
if(str.charAt(i)=='a')
a++;
}
long c =n/(long)str.length();
a *= c;
c = n%(long)str.length();
for(int i=0;i<(int)c;i++)
{
if(str.charAt(i)=='a')
a++;
}
System.out.println(a);
}
示例15: getMode
import java.io.InputStreamReader; //導入依賴的package包/類
private static String getMode(String hostPort) throws NumberFormatException, UnknownHostException, IOException {
String parts[] = hostPort.split(":");
Socket s = new Socket(parts[0], Integer.parseInt(parts[1]));
s.getOutputStream().write("stat".getBytes());
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
String line;
try {
while((line = br.readLine()) != null) {
if (line.startsWith("Mode: ")) {
return line.substring(6);
}
}
return "unknown";
} finally {
s.close();
}
}