Sekilas tentang IO, pentingnya IO dalam suatu pemrogramman namun mereka yang gila dengan desain tak sedikitpun memperhatikan tentang IO dalam java, padahal tanpa IO sebuah Aplikasi tidak akan ada gunanya, paling hanya beberapa saja yang tidak menggunakan IO.
nah...disini saya akan coba berbagi manfaat import dari java IO.
Membuat sebuah file Documents
package BuffererWriter;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
/**
*
* @author pc05
*/
public class Writer {
public static void main(String[]args){
String str = "/home/pc05/man.txt";
String trs = "/home/pc05/calep.php";
try {
FileWriter write = new FileWriter(str);
FileWriter write1 = new FileWriter(trs);
BufferedWriter bufer2 = new BufferedWriter(write1);
BufferedWriter bufer = new BufferedWriter(write);
bufer.write("test");
bufer.newLine();
bufer.write("Testing");
bufer.close();
} catch (IOException ex) {
System.out.println("ops sory"+ex.getMessage());
}
}
}
Membaca File Documents
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author pc05
*/
public class Reader {
public static void main(String[]args){
String name = "/home/pc05/man.txt";
try {
FileInputStream input = new FileInputStream(name);
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
try {
String hasil = reader.readLine();
System.out.println(hasil);
} catch (IOException ex) {
Logger.getLogger(Reader.class.getName()).log(Level.SEVERE, null, ex);
}
} catch (FileNotFoundException ex) {
System.err.println("error"+ex.getMessage());
}
}
}
Membaca dan Membuat File Documents
package ReaderWriter;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
public class readFile {
public readFile(){
String name = "/home/pc05/cute.txt";
FileWriter write;
try {
write = new FileWriter(name);
BufferedWriter bufer = new BufferedWriter(write);
bufer.write("jangan ditiru");
bufer.close();
} catch (IOException ex) {
Logger.getLogger(readFile.class.getName()).log(Level.SEVERE, null, ex);
}
readText(name);
}
void readText(String nama){
try {
FileInputStream strem = new FileInputStream(nama);
BufferedReader bufer = new BufferedReader(new InputStreamReader(strem));
String out = bufer.readLine();
System.out.println(out);
}catch(IOException e){
}
}
public static void main(String[]args){
new readFile();
}
}
Membuat File Bentuk PDF
package FilePdf;
import com.itextpdf.text.pdf.*;
import com.itextpdf.text.*;
import java.io.*;
import java.awt.Color;
import java.util.logging.Level;
import java.util.logging.Logger;
public class MembuatPdf {
public static void main(String[]args) throws FileNotFoundException{
float leading = (float) 100.400;
Document dok = new Document();
Paragraph p = new Paragraph(leading,"Test", FontFactory.getFont(FontFactory.HELVETICA,58,Font.BOLD,
new CMYKColor(0,0,0,255)));
p.setAlignment(1);
try {
PdfWriter.getInstance(dok, new FileOutputStream("Filepdf.pdf"));
dok.open();
dok.add(p);
dok.close();
System.out.println("pdf sudah dibuat");
} catch (DocumentException ex) {
Logger.getLogger(MembuatPdf.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
nah....sekarang dah tau kan betapa pentingnya IO dalam pemrogramman.., padahal fungsi di atas hanya sebagian ja lho, masih bayak lagi penggunaanya dalam pembuatan aplikasi laennya...jadi jangan sepelekan IO dalam pemrogramman java ocre.... :-)