Java PrintWriter write String to file

Description

Java PrintWriter write String to file

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;

public class Main {
  public static void main(String[] args) throws Exception {
    BufferedReader reader = new BufferedReader(new FileReader("Main.java"));
    StringBuilder sb = new StringBuilder();
    String lines = reader.readLine();
    while (lines != null) {
      sb.append(lines.toUpperCase() + "\n");
      lines = reader.readLine();// w  w w .  java 2  s.c om
    }
    reader.close();

    PrintWriter writer = new PrintWriter(new FileWriter(new File("lines.txt")));
    writer.write(sb.toString());
    writer.close();
  }
}



PreviousNext

Related