Java by API/java.io/BufferedWriter — различия между версиями

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

Текущая версия на 14:32, 31 мая 2010

BufferedWriter: close()

 
/*
 * Output:
 */
import java.io.BufferedWriter;
import java.io.FileWriter;
public class MainClass {
  public static void main(String args[]) {
    try {
      FileWriter fw = new FileWriter(args[0]);
      BufferedWriter bw = new BufferedWriter(fw);
      for(int i = 0; i < 12; i++) {
        bw.write("Line " + i + "\n");
      }
      bw.close();
    }
    catch(Exception e) {
      System.out.println("Exception: " + e);
    }
  }
}





BufferedWriter: flush()

 
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class Main {
  public static void main(String[] args) throws Exception {
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    String strLine = in.readLine();
    BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
    out.write(strLine, 0, strLine.length());
    out.flush();
    in.close();
    out.close();
  }
}





BufferedWriter: write(String s, int off, int len)

 
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class Main {
  public static void main(String[] args) throws Exception {
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    String strLine = in.readLine();
    BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
    out.write(strLine, 0, strLine.length());
    out.flush();
    in.close();
    out.close();
  }
}





BufferedWriter: write(String str)

 
/*
 * Output:
 */
import java.io.BufferedWriter;
import java.io.FileWriter;
public class MainClass {
  public static void main(String args[]) {
    try {
      FileWriter fw = new FileWriter(args[0]);
      BufferedWriter bw = new BufferedWriter(fw);
      for(int i = 0; i < 12; i++) {
        bw.write("Line " + i + "\n");
      }
      bw.close();
    }
    catch(Exception e) {
      System.out.println("Exception: " + e);
    }
  }
}





new BufferedWriter(Writer out)

 
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StringReader;
public class Main {
  public static void main(String[] args) throws Exception {
    BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
    System.out.println(stdin.readLine());
    BufferedReader in = new BufferedReader(new FileReader("Main.java"));
    String s, s2 = new String();
    while ((s = in.readLine()) != null)
      s2 += s + "\n";
    in.close();
    StringReader in1 = new StringReader(s2);
    int c;
    while ((c = in1.read()) != -1)
      System.out.print((char) c);
    BufferedReader in2 = new BufferedReader(new StringReader(s2));
    PrintWriter out1 = new PrintWriter(new BufferedWriter(new FileWriter(
        "IODemo.out")));
    int lineCount = 1;
    while ((s = in2.readLine()) != null)
      out1.println(lineCount++ + ": " + s);
    out1.close();
  }
}