Java/File Input Output/BufferedWriter

Материал из Java эксперт
Версия от 18:01, 31 мая 2010; (обсуждение)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Appending to a File

 
import java.io.BufferedWriter;
import java.io.FileWriter;
public class Main {
  public static void main(String[] argv) throws Exception {
    BufferedWriter out = new BufferedWriter(new FileWriter("filename", true));
    out.write("aString");
    out.close();
  }
}





Create BufferedWriter from FileWriter and write strings to the file

 
import java.io.BufferedWriter;
import java.io.FileWriter;
class BufferedWriterDemo {
  public static void main(String args[]) throws Exception {
    FileWriter fw = new FileWriter(args[0]);
    BufferedWriter bw = new BufferedWriter(fw);
    // Write strings to the file
    for (int i = 0; i < 12; i++) {
      bw.write("Line " + i + "\n");
    }
    // Close buffered writer
    bw.close();
  }
}





Nested creation of FileWriter, BufferedWriter and PrintWriter

 
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.PrintWriter;
public class Main {
  public static void main(String[] argv) throws Exception {
    FileWriter file = new FileWriter("a.html");
    BufferedWriter buffer = new BufferedWriter(file);
    PrintWriter out = new PrintWriter(buffer);
    out.println("<html>\n\t<body>");
    out.println("\t</body>\n</html>");
    out.close();
    buffer.close();
    file.close();
  }
}





new BufferedWriter(new OutputStreamWriter(System.out))

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





Read Input From User and Write to File

 
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.InputStream;
public class Main {
  public static void main(String[] args) throws Exception {
    InputStream in = System.in;
    BufferedWriter bw = new BufferedWriter(new FileWriter("output.txt"));
    int letter;
    while ((letter = in.read()) != -1) {
      bw.write((char) letter);
      bw.flush();
    }
  }
}





Use a BufferedReader and a BufferedWriter to copy a text file, inverting the case of letters in the process

 
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
public class Main {
  public static void main(String[] argv) throws Exception {
    BufferedReader br = new BufferedReader(new FileReader("in.txt"));
    BufferedWriter bw = new BufferedWriter(new FileWriter("out.txt"));
    int i;
    do {
      i = br.read();
      if (i != -1) {
        if (Character.isLowerCase((char) i))
          bw.write(Character.toUpperCase((char) i));
        else if (Character.isUpperCase((char) i))
          bw.write(Character.toLowerCase((char) i));
        else
          bw.write((char) i);
      }
    } while (i != -1);
    br.close();
    bw.close();
  }
}





Write text file

 
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.Writer;
public class Main {
  public static void main(String[] args) throws Exception {
    Writer writer = null;
    String text = "This is a text file";
    File file = new File("write.txt");
    writer = new BufferedWriter(new FileWriter(file));
    writer.write(text);
    writer.close();
  }
}





Write to file using a BufferedWriter

 
import java.io.BufferedWriter;
import java.io.FileWriter;
public class Main {
  public static void main(String[] args) throws Exception {
    BufferedWriter bufferedWriter = null;
    bufferedWriter = new BufferedWriter(new FileWriter("yourFile.txt"));
    bufferedWriter.write("string");
    bufferedWriter.newLine();
    bufferedWriter.write("string");
  }
}





Writing to a File: If the file does not exist, it is automatically created.

 
import java.io.BufferedWriter;
import java.io.FileWriter;
public class Main {
  public static void main(String[] argv) throws Exception {
    BufferedWriter out = new BufferedWriter(new FileWriter("outfilename"));
    out.write("asdf");
    out.close();
  }
}