Input dan Output ke Text File part II
- Forums:
Pembahasan kali ini mirip dengan pembahasan sebelumnya. Hanya saja kali ini kita akan menggunakan class berbeda dari sebelumnya, walaupun tujuannya sama, yaitu class FileWriter untuk membuat/membuka file text, class BufferedWriter untuk menulis string ke dalam file text, class FileReader untuk membaca file text, dan BufferedReader untuk membaca string pada file text.
versi satu
versi dua
versi sederhana
versi satu
import java.io.*;
import java.util.Random;
class TulisBacaFile
{
static void tulisKeFile(String note)
{ FileWriter fw;
BufferedWriter bw = null;
try
{ fw = new FileWriter(note);
bw = new BufferedWriter(fw);
Random r = new Random();
int x=0;
String str=null;
for(int i=0;i<5;i++)
{ x = r.nextInt(30)+10;
str = Integer.toString(x);
bw.write(str+"\r\n");
//bw.newLine();
}
str = Integer.toString(x+5);
bw.write(str);
} catch(IOException e)
{ System.err.println("File writing error");
} finally
{ try
{ if(bw != null)
{ bw.flush();
bw.close();
}
} catch(IOException e)
{ System.err.println(e);
}
}
}
static void bacaFile(String note)
{ String str;
int x, jml=0, n=0;
try
{ FileReader fr = new FileReader(note);
BufferedReader br = new BufferedReader(fr);
while((str=br.readLine()) != null)
{ x = Integer.parseInt(str);
System.out.println(x);
jml+=x;
n++;
}
br.close();
} catch(IOException e)
{ System.err.println("File read error");
}
double rata = (double) jml/n;
System.out.println("Jumlah : "+jml+"\nRata-rata : "+rata);
}
public static void main(String[] args)
{ tulisKeFile("coba1.txt");
bacaFile("coba1.txt");
}
}
versi dua
import java.io.*;
import java.util.Random;
class TxtIO
{ public static void main(String[] args)
{ new TxtIO().tulisKeFile("coba1.txt");
new TxtIO().bacaFile("coba1.txt");
}
void tulisKeFile(String note)
{ FileWriter fw;
BufferedWriter bw = null;
try
{ fw = new FileWriter(note);
bw = new BufferedWriter(fw);
Random r = new Random();
int x=0;
String str=null;
for(int i=0;i<5;i++)
{ x = r.nextInt(30)+10;
str = Integer.toString(x);
bw.write(str+"\r\n");
//bw.newLine();
}
str = Integer.toString(x+5);
bw.write(str);
} catch(IOException e)
{ System.err.println("File writing error");
} finally
{ try
{ if(bw != null)
{ bw.flush();
bw.close();
}
} catch(IOException e)
{ System.err.println(e);
}
}
}
void bacaFile(String note)
{ String str;
int x, jml=0, n=0;
try
{ FileReader fr = new FileReader(note);
BufferedReader br = new BufferedReader(fr);
while((str=br.readLine()) != null)
{ x = Integer.parseInt(str);
System.out.println(x);
jml+=x;
n++;
}
br.close();
} catch(IOException e)
{ System.err.println("File read error");
}
double rata = (double) jml/n;
System.out.println("Jumlah : "+jml+"\nRata-rata : "+rata);
}
}
versi sederhana
import java.io.*;
class TulisBacaFile
{
public static void main(String[] args)
{
String str;
FileWriter fw;
BufferedWriter bw;
try
{ fw = new FileWriter("coba2.txt");
bw = new BufferedWriter(fw);
bw.write("baris kesatu"+"\r\n");
bw.write("baris kedua"+"\r\n");
bw.write("baris ketiga\r\n");
bw.write("baris keempat\r\n");
bw.flush();
bw.close();
} catch(IOException e)
{ System.err.println("Kesalahan pada penulisan text ke File");
}
FileReader fr;
BufferedReader br;
try
{
fr = new FileReader("coba2.txt");
br = new BufferedReader(fr);
while((str=br.readLine()) != null)
{
System.out.println(str);
}
br.close();
} catch(IOException e)
{ System.err.println("Kesalahan pada pembacaan file text");
}
}
}
hasil yang tampil di layar: baris kesatu baris kedua baris ketiga baris keempat
Klik di sini untuk lihat PENJELASAN
