Java Tutorial/File/InputStreamReader
Содержание
Deal with Keyboard Input with BufferedReader
<source lang="java">
import java.io.BufferedReader; import java.io.InputStreamReader; public class Main {
public static void main(String[] argv) throws Exception { System.out.print("Enter your name: "); InputStreamReader reader = new InputStreamReader(System.in); BufferedReader in = new BufferedReader(reader); String name = in.readLine(); System.out.println("Hello, " + name + ". Enter three ints..."); int[] values = new int[3]; double sum = 0.0; for (int i = 0; i < values.length; i++) { System.out.print("Number " + (i + 1) + ": "); String temp = in.readLine(); values[i] = Integer.parseInt(temp); sum += values[i]; } System.out.println("The average equals " + sum / values.length); }
}</source>
Get a class of an object
<source lang="java">
import java.util.ArrayList; import java.util.List; public class Main {
public static void main(String[] args) { Person p = new Person("A"); Animal a = new Animal("B"); Thing t = new Thing("C"); String text = "hello"; Integer number = 1000; List<Object> list = new ArrayList<Object>(); list.add(p); list.add(a); list.add(t); list.add(text); list.add(number); for (int i = 0; i < list.size(); i++) { Object o = list.get(i); if (o instanceof Person) { System.out.println("My name is " + ((Person) o).getName()); } else if (o instanceof Animal) { System.out.println("I live in " + ((Animal) o).getHabitat()); } else if (o instanceof Thing) { System.out.println("My color is " + ((Thing) o).getColor()); } else if (o instanceof String) { System.out.println("My text is " + o.toString()); } else if (o instanceof Integer) { System.out.println("My value is " + ((Integer) o)); } } }
} class Person {
private String name; public Person(String name) { this.name = name; } public String getName() { return name; }
} class Animal {
private String habitat; public Animal(String habitat) { this.habitat = habitat; } public String getHabitat() { return habitat; }
} class Thing {
private String color; public Thing(String color) { this.color = color; } public String getColor() { return color; }
}</source>
InputStreamReader
- An InputStreamReader reads bytes and translates them into characters using the specified character set.
- InputStreamReader is ideal for reading from the output of an OutputStreamWriter or a PrintWriter.
The InputStreamReader class has four constructors:
<source lang="java">
public InputStreamReader (InputStream in) public InputStreamReader (InputStream in, java.nio.charset.Charset cs) public InputStreamReader (InputStream in, java.nio.charset.CharsetDecoder, dec) public InputStreamReader (InputStream in, String charsetName)</source>
Reading ISO Latin-1 Encoded Data
<source lang="java">
import java.io.BufferedReader; import java.io.FileInputStream; import java.io.InputStreamReader; public class Main {
public static void main(String[] argv) throws Exception { BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream("infilename"), "8859_1")); String str = in.readLine(); System.out.println(str); }
}</source>
Uses a PrintWriter to write two Chinese characters and read them back.
<source lang="java">
import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; public class MainClass {
public static void main(String[] args) { try { char[] chars = new char[2]; chars[0] = "\u4F60"; chars[1] = "\u597D"; String encoding = "GB18030"; File textFile = new File("C:\\temp\\myFile.txt"); PrintWriter writer = new PrintWriter(textFile, encoding); writer.write(chars); writer.close(); // read back InputStreamReader reader = new InputStreamReader(new FileInputStream(textFile), encoding); char[] chars2 = new char[2]; reader.read(chars2); System.out.print(chars2[0]); System.out.print(chars2[1]); reader.close(); } catch (IOException e) { System.out.println(e.toString()); } }
}</source>