Tuesday, June 19, 2012

Contoh program Java input/output untuk copy isi text ke text yang lain

import java.io.*;

public class Simple{
public static void main(String [] args){
try {
FileReader fileRead = new FileReader("Test.txt");//sediakan file dengan extensi .txt dan nama file Test
BufferedReader bufferRead = new BufferedReader(fileRead);//

FileWriter fileWrite = new FileWriter("test2.txt");
BufferedWriter bufferWrite = new BufferedWriter(fileWrite);
String line;

//Read 1st line
line = bufferRead.readLine();
while ( line != null ) {
//write the line out to the Output file
bufferWrite.write(line,0,line.length());
bufferWrite.newLine();
//Read next line
line = bufferRead.readLine();
}
bufferRead.close();
bufferWrite.close();
} catch( Exception e){
e.printStackTrace();
}
}
}

---------------------------

Kode diatas akan mencopy text di Test.txt ke test2.txt

Bufferedreader akan membaca file langsung satu line dari text dalam satu waktu.

method close digunakan untuk menghentikan penggunaan bufferedreader itu sendiri.

Salam-

Luthfan

-Usaha dengan Doa

No comments:

Post a Comment

Your Comment is Our Order, Your Majesty