Java/File Input Output/Buffered Reader Writer

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

Creates a tiny text editor with BufferedReader

 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
class TinyEdit {
  public static void main(String args[]) throws IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String str[] = new String[100];
    System.out.println("Enter lines of text.");
    System.out.println("Enter "stop" to quit.");
    for (int i = 0; i < 100; i++) {
      str[i] = br.readLine();
      if (str[i].equals("stop"))
        break;
    }
    System.out.println("\nHere is your file:");
    for (int i = 0; i < 100; i++) {
      if (str[i].equals("stop"))
        break;
      System.out.println(str[i]);
    }
  }
}





how to read lines from a file and print these to the standard output stream

 
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
class FileReaderDemo {
  public static void main(String args[]) throws IOException {
    FileReader fr = new FileReader("FileReaderDemo.java");
    BufferedReader br = new BufferedReader(fr);
    String s;
    while ((s = br.readLine()) != null) {
      System.out.println(s);
    }
    fr.close();
  }
}





Read a string from console using a BufferedReader.

 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
class BRReadLines {
  public static void main(String args[]) throws IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String str;
    System.out.println("Enter lines of text.");
    System.out.println("Enter "stop" to quit.");
    do {
      str = br.readLine();
      System.out.println(str);
    } while (!str.equals("stop"));
  }
}





Use a BufferedReader to read characters from the console.

 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
class BRRead {
  public static void main(String args[]) throws IOException {
    char c;
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Enter characters, "q" to quit.");
    do {
      c = (char) br.read();
      System.out.println(c);
    } while (c != "q");
  }
}





Writing to a Text File

 

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
public class WriteFile {
  public static void main(String[] args) {
    Product[] movies = new Product[10];
    movies[0] = new Product("L", 1946, 14.95);
    movies[1] = new Product("T", 1965, 12.95);
    movies[2] = new Product("Y", 1974, 16.95);
    movies[3] = new Product("T", 1975, 11.95);
    movies[4] = new Product("S", 1977, 17.95);
    movies[5] = new Product("B", 1987, 16.95);
    movies[6] = new Product("G", 1989, 14.95);
    movies[7] = new Product("A", 1995, 19.95);
    movies[8] = new Product("G", 1997, 14.95);
    movies[9] = new Product("R", 2001, 19.95);
    PrintWriter out = openWriter("movies.txt");
    for (Product m : movies)
      writeMovie(m, out);
    out.close();
  }
  private static PrintWriter openWriter(String name) {
    try {
      File file = new File(name);
      PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(file)), true);
      return out;
    } catch (IOException e) {
      System.out.println("I/O Error");
      System.exit(0);
    }
    return null;
  }
  private static void writeMovie(Product m, PrintWriter out) {
    String line = m.title;
    line += "\t" + Integer.toString(m.year);
    line += "\t" + Double.toString(m.price);
    out.println(line);
  }

}
class Product {
  public String title;
  public int year;
  public double price;
  public Product(String title, int year, double price) {
    this.title = title;
    this.year = year;
    this.price = price;
  }
}