Java by API/java.io/BufferedReader — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Версия 17:43, 31 мая 2010
Содержание
BufferedReader: readLine()
/*
* Output:
*/
import java.io.BufferedReader;
import java.io.FileReader;
public class MainClass {
public static void main(String args[]) {
try {
FileReader fr = new FileReader(args[0]);
BufferedReader br = new BufferedReader(fr);
String s;
while((s = br.readLine()) != null)
System.out.println(s);
fr.close();
}
catch(Exception e) {
System.out.println("Exception: " + e);
}
}
}
new BufferedReader(FileReader fileReader)
/*
* Output:
*/
import java.io.BufferedReader;
import java.io.FileReader;
public class MainClass {
public static void main(String args[]) {
try {
FileReader fr = new FileReader(args[0]);
BufferedReader br = new BufferedReader(fr);
String s;
while((s = br.readLine()) != null)
System.out.println(s);
fr.close();
}
catch(Exception e) {
System.out.println("Exception: " + e);
}
}
}
new BufferedReader(InputStreamReader input)
/*
* Output:
*/
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class MainClass {
public static void main(String args[]) {
try {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
while (true) {
System.out.print("Radius? ");
String str = br.readLine();
double radius;
try {
radius = Double.valueOf(str).doubleValue();
} catch (NumberFormatException nfe) {
System.out.println("Incorrect format!");
continue;
}
if (radius <= 0) {
System.out.println("Radius must be positive!");
continue;
}
double area = Math.PI * radius * radius;
System.out.println("Area is " + area);
return;
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
new BufferedReader(Reader in)
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
public class Main {
public static void main(String[] args) throws Exception {
URL url = new URL("http://localhost:1776");
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String line;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
in.close();
}
}