Java Tutorial/File/BufferedReader
Содержание
- 1 BufferedReader
- 2 Call the static method PressAnykey to keep to DOS window open.
- 3 Create BufferedReader from FileReader
- 4 Create BufferedReader from InputStreamReader
- 5 Create BufferReader from InputStreamReader
- 6 Create PrintWriter from BufferedWriter
- 7 Read content of a file
- 8 Reading Text from a File
- 9 Reads a text file and displays it line by line.
- 10 Reversing a File
- 11 Using BufferedReader to read input number from user
BufferedReader
- BufferedReader wraps another Reader and improves performance.
- BufferedReader provides a readLine method to read a line of text.
Using BufferedReader:
<source lang="java">
InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream (textFile), encoding ); BufferedReader bufferedReader = new BufferedReader (inputStreamReader);</source>
Call the static method PressAnykey to keep to DOS window open.
<source lang="java">
import java.io.BufferedReader; import java.io.InputStreamReader; public class Main{
public static void main(String[] args) { PressAnyKey(); } public static void PressAnyKey() { BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Press any key..."); try { input.readLine(); } catch (Exception e) { e.printStackTrace(); } }
}</source>
Create BufferedReader from FileReader
<source lang="java">
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class MainClass {
public static void main(String[] args) throws IOException { // 1. Reading input by lines: BufferedReader in = new BufferedReader(new FileReader("IOStreamDemo.java")); String s, s2 = new String(); while ((s = in.readLine()) != null) s2 += s + "\n"; in.close(); }
}</source>
Create BufferedReader from InputStreamReader
<source lang="java">
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class MainClass {
public static void main(String[] args) throws IOException { BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter a line:"); System.out.println(stdin.readLine()); }
}</source>
Create BufferReader from InputStreamReader
<source lang="java">
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.ObjectOutputStream; import java.net.Socket; import java.util.Locale; public class DateClient {
final static int PORT = 5000; public static void main(String[] args) { try { Socket s = new Socket("localhost", PORT); ObjectOutputStream oos; oos = new ObjectOutputStream(s.getOutputStream()); oos.writeObject(Locale.getDefault()); InputStreamReader isr; isr = new InputStreamReader(s.getInputStream()); BufferedReader br = new BufferedReader(isr); System.out.println(br.readLine()); } catch (Exception e) { System.err.println(e); } }
}</source>
Create PrintWriter from BufferedWriter
<source lang="java">
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.EOFException; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.io.StringReader; public class MainClass {
public static void main(String[] args) throws IOException { try { BufferedReader in4 = new BufferedReader(new StringReader("asdf")); PrintWriter out1 = new PrintWriter(new BufferedWriter(new FileWriter("IODemo.out"))); int lineCount = 1; String s = null; while((s = in4.readLine()) != null ) out1.println(lineCount++ + ": " + s); out1.close(); } catch(EOFException e) { System.err.println("End of stream"); } }
}</source>
Read content of a file
<source lang="java">
import java.io.BufferedReader; import java.io.File; import java.io.FileReader; public class Main {
public static void main(String[] argv) { readTextFile(new File("c:\\a.txt")); } public static String readTextFile(File filename) { try { BufferedReader br = new BufferedReader(new FileReader(filename)); StringBuilder sb = new StringBuilder(); String line = br.readLine(); while (line != null) { sb.append(line + "\n"); line = br.readLine(); } br.close(); return sb.toString(); } catch (Exception e) { e.printStackTrace(); System.err.println(filename); System.err.println(e); return ""; } }
}</source>
Reading Text from a File
<source lang="java">
import java.io.BufferedReader; import java.io.FileReader; public class Main {
public static void main(String[] argv) throws Exception { BufferedReader in = new BufferedReader(new FileReader("infilename")); String str; while ((str = in.readLine()) != null) { System.out.println(str); } in.close(); }
}</source>
Reads a text file and displays it line by line.
<source lang="java">
import java.io.BufferedReader; import java.io.File; import java.io.FileReader; public class MainClass {
public static void main(String[] a) { FileReader fr; try { fr = new FileReader (new File("yourFile.txt")); BufferedReader br = new BufferedReader (fr); String line = br.readLine(); while (line != null) { System.out.println(line); line = br.readLine(); } br.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }
}
}</source>
Reversing a File
<source lang="java">
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; public class ReverseLines {
public static void main(String[] args) { try { BufferedReader input = new BufferedReader(new FileReader(args[0])); ArrayList list = new ArrayList(); String line; while ((line = input.readLine()) != null) { list.add(line); } input.close(); Collections.reverse(list); PrintWriter output = new PrintWriter(new BufferedWriter(new FileWriter(args[1]))); for (Iterator i = list.iterator(); i.hasNext();) { output.println((String) i.next()); } output.close(); } catch (IOException e) { System.err.println(e); } }
}</source>
Using BufferedReader to read input number from user
<source lang="java">
import java.io.BufferedReader; import java.io.InputStreamReader; public class Main {
public static void main(String[] args) throws Exception { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); String input = reader.readLine(); double number = Double.parseDouble(input); System.out.println(input + ":" + Math.sqrt(number)); reader.close(); }
}</source>