Java by API/java.io/BufferedWriter

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

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