Write to a file using PrintWriter - Java File Path IO

Java examples for File Path IO:PrintWriter

Description

Write to a file using PrintWriter

Demo Code



import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;

public class Ejercicio1 {

  public static void Main(String[] args){
    Scanner sc = new Scanner(System.in);
    String text;//  w  w  w .j ava  2 s.c o  m
    PrintWriter result = null;
    try{
      result = new PrintWriter("a.txt");
      System.out.println("introduce texto:");
      text = sc.nextLine();
      while (text.compareTo("fin") != 0) {
        result.println(text);
        text = sc.nextLine();
      }
      result.flush();
    }catch(FileNotFoundException e){
      System.out.println(e.getMessage());
    }
    result.close();
    sc.close();
  }
}

Related Tutorials