Java Tutorial2D Graphics in JavaJava Stack-Walking APIClass - Java ReflectionCommon Java PitfallsDocumenting Java CodeGenerating Java CodeInstalling Java (Standard Edition)Java AgentsJava Alternative CollectionsJava AnnotationsJava Apache Commons LangJava AppDynamics and TIBCO BusinessWorks Instrumentation for Easy IntegrationJava AppletsJava ArraysJava AssertingJava Atomic TypesJava AudioJava AutoboxingJava Basic Control StructuresJava BenchmarksJava BigDecimalJava BigIntegerJava Bit ManipulationJava BufferedWriterJava ByteBufferJava Bytecode ModificationJava C++ ComparisonJava Calendar and its SubclassesJava Character encodingJava Choosing CollectionsJava Classes and ObjectsJava ClassloadersJava Collection Factory MethodsJava CollectionsJava Command line Argument ProcessingJava Comparable and ComparatorJava Compiler - javacJava CompletableFutureJava Concurrent CollectionsJava Concurrent Programming (Threads)Java Console I/OJava ConstructorsJava Converting to and from StringsJava Creating Images ProgrammaticallyJava Currency and MoneyJava Date ClassJava Dates and Time (java.time.*)Java Default MethodsJava deploymentJava Dequeue InterfaceJava Disassembling and DecompilingJava Dynamic Method DispatchJava Editions, Versions, Releases and DistributionsJava EncapsulationJava Enum MapJava Enum starting with numberJava EnumsJava EnumSet classJava Exceptions and exception handlingJava Executor, ExecutorService and Thread poolsJava ExpressionsJava File I/OJava FileUpload to AWSJava Floating Point OperationsJava Fluent InterfaceJava FTP (File Transfer Protocol)Java Functional InterfacesJava GenericsJava Getters and SettersJava HashtableJava HttpURLConnectionJava Immutable ClassJava Immutable ObjectsJava InheritanceJava InputStreams and OutputStreamsJava InterfacesJava Iterator and IterableJava JavaBeanJava JAX-WSJava JAXBJava JMXJava JNDIJava JShellJava Just in Time (JIT) compilerJava JVM FlagsJava JVM Tool InterfaceJava Lambda ExpressionsJava LinkedHashMapJava List vs SET

Java BufferedWriter

From WikiOD

Syntax[edit | edit source]

  • new BufferedWriter(Writer); //The default constructor
  • BufferedWriter.write(int c); //Writes a single character
  • BufferedWriter.write(String str); //Writes a string
  • BufferedWriter.newLine(); //Writes a line separator
  • BufferedWriter.close(); //Closes the BufferedWriter

Remarks[edit | edit source]

  • If you try to write from a BufferedWriter (using BufferedWriter.write()) after closing the BufferedWriter (using BufferedWriter.close()), it will throw an IOException.
  • The BufferedWriter(Writer) constructor does NOT throw an IOException. However, the FileWriter(File) constructor throws a FileNotFoundException, which extends IOException. So catching IOException will also catch FileNotFoundException, there is never a need for a second catch statement unless you plan on doing something different with the FileNotFoundException.

Write a line of text to File[edit | edit source]

This code writes the string to a file. It is important to close the writer, so this is done in a finally block.

  public void writeLineToFile(String str) throws IOException {
    File file = new File("file.txt");
    BufferedWriter bw = null;
    try {
      bw = new BufferedWriter(new FileWriter(file));
    } finally {
      if (bw != null) {

Also note that write(String s) does not place newline character after string has been written. To put it use newLine() method.

Java SE 7

Java 7 adds the java.nio.file package, and try-with-resources:

public void writeLineToFile(String str) throws IOException {
    Path path = Paths.get("file.txt");
    try (BufferedWriter bw = Files.newBufferedWriter(path)) {