Dr. GÜRAY SONUGÜR

Dr. Öğr. Üyesi Güray SONUGÜR – Öğrenci Bilgi Paylaşım Platformu

Dosyaya yazma (metot içerisinden)

//Dosyaya yazma metodunun “void ” olduğuna  dikkat ediniz!

import java.io.*;

import java.util.Scanner;

public class DosyaIO {

public static void main(String[] args) throws IOException {

Scanner sc = new Scanner(System.in);
System.out.println(“Dosya adını giriniz: “);
String dosyaAdi= sc.next();

System.out.println(“Dosya içeriğini giriniz: “);
String str= sc.next();

DosyaYaz(dosyaAdi, str);
}

private static void DosyaYaz(String dosyaAdi, String icerik) throws IOException
{
FileWriter f = new FileWriter(dosyaAdi);
BufferedWriter out = new BufferedWriter(f);

out.write(icerik);
out.newLine();

out.close();
f.close();

}

}