Input dan Output File di Java

File merupakan urutan data yang digunakan untuk melakukan encode informasi digital untuk urusan penyimpanan dan pertukaran data. Di Java, terdapat kelas FileInputStream dan FileOutputStream yang digunakan untuk menangani proses input-output. FileInputStream digunakan untuk membaca data dari file yang merupakan turunan langsung dari class InputStream, sedangkan FileOutputStream untuk menuliskan data ke file merupakan turunan langsung dari class OutputStream.

FileInputStream input = new FileInputStream("path..."); 
FileOutputStream output = new FileOutputStream("path...");

Berikut adalah contoh program sederhana untuk membaca, menulis dan mencopy file.
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;

public class IOFile {

FileOutputStream out = null;
FileInputStream input = null;
int data;
String copy ="";

public void tulisFile(String data){
try {
out = new FileOutputStream("C:/Users/Wim/juno/PraktikumSister/wim_asli.txt");
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
}

//menulis
try {
for (int i = 0; i < data.length(); i++) {
//konversi data
out.write((int)data.charAt(i));
}
} catch (IOException e) {
System.out.println(e.getMessage());
}
//tutup
try {
out.close();
} catch (Exception e) {
}
}

public void bacaFile(String namaFile){
//membuka file
try {
input = new FileInputStream("C:/Users/Wim/juno/PraktikumSister/"+namaFile+".txt");
} catch (FileNotFoundException e) {
System.out.println("File tidak ditemukan !");
}

try {
while ((data = input.read())!= -1) {
System.out.print((char)data);
copy += String.valueOf((char)data);
}
System.out.println("");
} catch (IOException e) {
System.out.println(e.getMessage());
}
//tutup
try {
out.close();
} catch (Exception e) {
}
}

public void copyFile(){
try {
out = new FileOutputStream("C:/Users/Wim/juno/PraktikumSister/wim_tiruan.txt");
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
}

//menulis
try {
for (int i = 0; i < copy.length(); i++) {
//konversi data
out.write((int)copy.charAt(i));
}
} catch (IOException e) {
System.out.println(e.getMessage());
}
//tutup
try {
out.close();
} catch (Exception e) {
}
}

public static void main(String[] args) {
InputStreamReader in = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(in);

IOFile iofile = new IOFile();
String data = "";

System.out.println("--------------------------------");
System.out.println("-----I/O File------");
System.out.println("--------------------------------\n");

try {
System.out.print("Input data : ");
data = br.readLine();
iofile.tulisFile(data);
System.out.print("Isi file asli : ");
iofile.bacaFile("wim_asli");

System.out.print("Copy data? (y/t) : ");
String pilih = br.readLine();

if(pilih.equalsIgnoreCase("y")){
iofile.copyFile();
System.out.print("Isi file tiruan : ");
iofile.bacaFile("wim_tiruan");
}else{
System.exit(0);
}

} catch (Exception e) {
// TODO: handle exception
}
}
}


Output :

Post a Comment

Previous Post Next Post