Java Tutorial/File/BufferedReader

Материал из Java эксперт
Перейти к: навигация, поиск

BufferedReader

  1. BufferedReader wraps another Reader and improves performance.
  2. BufferedReader provides a readLine method to read a line of text.

Using BufferedReader:



InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream (textFile), encoding );
BufferedReader bufferedReader = new BufferedReader (inputStreamReader);





Call the static method PressAnykey to keep to DOS window open.

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();
    }
  }
}





Create BufferedReader from FileReader

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();
  }
}





Create BufferedReader from InputStreamReader

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());
  }
}





Create BufferReader from InputStreamReader

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);
    }
  }
}





Create PrintWriter from BufferedWriter

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");
    }
  }
}





Read content of a file

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 "";
    }
  }
}





Reading Text from a File

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();
  }
}





Reads a text file and displays it line by line.

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();
    }

  }
}





Reversing a File

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);
    }
  }
}





Using BufferedReader to read input number from user

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();
  }
}