1. KONSEP DASAR STREAM IO
Seperti yang telah disebutkan sebelumnya, secara umum ada dua tipe dari stream, yaitu stream karakter dan byte. Kita hanya mengulang perbedaan mendasar antara keduanya. Stream byte adalah abstraksi file atau alat untuk data biner sedangkan stream karakter adalah untuk karakter Unicode.
Stream juga dikategorikan berdasarkan apakah mereka digunakan untuk membaca atau menulis stream.Walaupun ini sudah cukup nyata, Anda diperbolehkan untuk membaca dari input stream tapi tidak menulisnya. Di lain pihak, Anda diperbolehkan untuk menulis output streams tapi tidak membacanya.
Class InputStream dan class Reader adalah superclass-superclass dari semua input stream. Class OutputStream dan class Writer adalah class-class root dari semua output stream. Input stream juga dikenal sebagai stream sumber (source stream) sejak kita memperoleh informasi dari stream ini. sementara itu output stream disebut juga stream hasil(sink stream).
2. CONTOH SINTAX
coding enkripsi :
---------------------------------------
awal coding pada text plaintext :
---------------------------------------
package enkripsi;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import java.io.*;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class UserInterface extends javax.swing.JFrame {
JFileChooser fc;
File file;
boolean Nfile;
final String[] EXT = { ".csv" };
final String[] EXT2 = { ".txt", ".java" };
public UserInterface() {
initComponents();
editor.setVisible(false);
editor2.setFocusable(false);
editor2.setEditable(false);
this.setTitle("Untitled - NOTES");
fc = new JFileChooser();
fc.addChoosableFileFilter(new jenisFile(EXT));
fc.addChoosableFileFilter(new jenisFile(EXT2));
fc.setAcceptAllFileFilterUsed(false);
Nfile = true;
}
@SuppressWarnings("unchecked")
-----------------------------------------------
coding selanjutnya pada button new :
-----------------------------------------------
private void btnNewActionPerformed(java.awt.event.ActionEvent evt) {
editor.setText("");
btnSave.setEnabled(true);
btnSAs.setEnabled(true);
editor.requestFocus();
this.setTitle("Untitled - NOTES");
Nfile = true;
editor.setEnabled(true);
editor.setVisible(true);
spnGeser.setEnabled(true);
editor2.setEnabled(true);
txtCPass.setEnabled(true);
btnDes.setEnabled(true);
btnCrypt.setEnabled(true);
btnRC.setEnabled(true);
btnCC.setEnabled(true);
btnEE.setEnabled(true);
btnSave2.setEnabled(true);
btnMD5.setEnabled(true);
}
--------------------------------
coding pada button open :
--------------------------------
private void btnOpenActionPerformed(java.awt.event.ActionEvent evt) {
spnGeser.setEnabled(true);
editor2.setEnabled(true);
txtCPass.setEnabled(true);
btnDes.setEnabled(true);
btnCrypt.setEnabled(true);
btnRC.setEnabled(true);
btnCC.setEnabled(true);
btnEE.setEnabled(true);
btnSave2.setEnabled(true);
btnMD5.setEnabled(true);
int hasil = fc.showOpenDialog(this);
editor.setEnabled(true);
editor.setVisible(true);
btnSave.setEnabled(true);
btnSAs.setEnabled(true);
if (hasil == JFileChooser.APPROVE_OPTION) {
file = fc.getSelectedFile();
try {
editor.read(new FileInputStream(file), null);
} catch (IOException e) {
msg("Gagal Buka File : "+file.getName() );
return ;
}
}
this.setTitle(file.getName()+" - NOTES" );
Nfile = false;
}
--------------------------------
coding pada button save :
--------------------------------
private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {
if (Nfile)
btnSAs.doClick();
else
SimpanFile();
}
--------------------------------
coding pada button save As:
--------------------------------
private void btnSAsActionPerformed(java.awt.event.ActionEvent evt) {
if (fc.showSaveDialog(this) == JFileChooser.APPROVE_OPTION){
file = fc.getSelectedFile();
if (!file.exists() || okToReplace())
SimpanFile();
}
}
--------------------------------
coding clear text :
--------------------------------
private void btnClearActionPerformed(java.awt.event.ActionEvent evt) {
editor.setText("");
}
----------------------------------------------------
hasil dari desaig Plain text coding diatas :
----------------------------------------------------
--------------------------------
coding set pass pada enkripsi :
--------------------------------
private void txtCPassActionPerformed(java.awt.event.ActionEvent evt) {
}
-------------------------------- }
coding button enkripsi :
--------------------------------
private void btnDesActionPerformed(java.awt.event.ActionEvent evt) {
if (txtCPass.getText().equals(txtPass.getText())){
JOptionPane.showMessageDialog(null, "Descryption Success");
editor2.setText(editor.getText());
}
else
JOptionPane.showMessageDialog(null, "What Are You Doing");
}
-------------------------------- if (txtCPass.getText().equals(txtPass.getText())){
JOptionPane.showMessageDialog(null, "Descryption Success");
editor2.setText(editor.getText());
}
else
JOptionPane.showMessageDialog(null, "What Are You Doing");
}
coding edit text enkripsi :
--------------------------------
private void btnEEActionPerformed(java.awt.event.ActionEvent evt) {
editor2.setFocusable(true);
editor2.setEditable(true);
}
--------------------------------
coding button save pada enkripsi :
--------------------------------
private void btnSave2ActionPerformed(java.awt.event.ActionEvent evt) {
if (fc.showSaveDialog(this) == JFileChooser.APPROVE_OPTION){
file = fc.getSelectedFile();
if (!file.exists() || okToReplace())
SimpanFile2();
}
}
--------------------------------
tambahkan coding berikut:
--------------------------------
private void btnCCActionPerformed(java.awt.event.ActionEvent evt) {
getData=editor.getText();
len=getData.length();
con=getData.toCharArray();
getData="";
j=len+1;
try {
for( i1=0;i1<j;i1++){
a=con[i1];
k=(int)a;
k+=1;
a=(char)k;
getData=getData+(a.toString());
} }
catch(Exception e){
}
editor2.setText(getData);
}
private void cobaActionPerformed(java.awt.event.ActionEvent evt) {
getData=editor.getText();
getData=getData.trim();
len=getData.length();
con=getData.toCharArray();
j=len-3;
getData="";
try {
for( i1=j;i1>=0;i1--){
a=con[i1];
k=(int)a;
k+=(int)spnGeser.getValue();
a=(char)k;
getData=getData+(a.toString());
} } catch(Exception e){
}
System.out.println(getData);
System.out.println(len);
editor2.setText(getData);
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
// TODO add your handling code here:
double randomNumber;
//char saya ganti double soalnya bisa ubah lagi dibawah
String randomkey="";
for (int i = 0; i < editor.getText().length(); i++) {
randomNumber = Math.random()*100;
//100 bloeh diubah berapa aja rekomendasi jgn <40 coba sendiri
randomkey = randomkey + (char)randomNumber;
//iniperlu untuk mengubah String randomkey="" ketingkat berikutnya
}
editor2.setText(randomkey);
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void txtPassActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void txtCPassActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void msg(String pesan) {
JOptionPane.showMessageDialog(this,pesan,"Informasi",
JOptionPane.INFORMATION_MESSAGE);
}
private boolean okToReplace() {
return (JOptionPane.showConfirmDialog(this,
"File "+file.getName()+" Sudah Ada\n"+
"Akan Ditimpa ?",
"Peringatan",JOptionPane.YES_NO_OPTION)==0) ;
}
private void SimpanFile2() {
PrintWriter pw = null;
try {
pw = new PrintWriter(new BufferedWriter(new FileWriter(file)));
} catch (IOException e) {
msg("Gagal Simpan File : '" + file.getName());
return;
}
pw.print(editor2.getText());
pw.close();
this.setTitle(file.getName()+" - NOTES" );
Nfile = false;
}
private void SimpanFile() {
PrintWriter pw = null;
try {
pw = new PrintWriter(new BufferedWriter(new FileWriter(file)));
} catch (IOException e) {
msg("Gagal Simpan File : '" + file.getName());
return;
}
pw.print(editor.getText());
pw.close();
this.setTitle(file.getName()+" - NOTES" );
Nfile = false;
}
String getData,cs,main="";
char con[];
int len,hc,j=0;
int i1=0;
char bb[];
int k=0;
Character a;
public void encrypt() {
getData=editor.getText();
getData=getData.trim();
len=getData.length();
con=getData.toCharArray();
j=len-1;
getData="";
try {
for( i1=j;i1>=0;i1--){
a=con[i1];
k=(int)a;
k+=(int)spnGeser.getValue();
a=(char)k;
getData=getData+(a.toString());
} }
catch(Exception e){
}
System.out.println(getData);
System.out.println(len);
editor2.setText(getData);
}
getData=editor.getText();
len=getData.length();
con=getData.toCharArray();
getData="";
j=len+1;
try {
for( i1=0;i1<j;i1++){
a=con[i1];
k=(int)a;
k+=1;
a=(char)k;
getData=getData+(a.toString());
} }
catch(Exception e){
}
editor2.setText(getData);
}
private void cobaActionPerformed(java.awt.event.ActionEvent evt) {
getData=editor.getText();
getData=getData.trim();
len=getData.length();
con=getData.toCharArray();
j=len-3;
getData="";
try {
for( i1=j;i1>=0;i1--){
a=con[i1];
k=(int)a;
k+=(int)spnGeser.getValue();
a=(char)k;
getData=getData+(a.toString());
} } catch(Exception e){
}
System.out.println(getData);
System.out.println(len);
editor2.setText(getData);
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
// TODO add your handling code here:
double randomNumber;
//char saya ganti double soalnya bisa ubah lagi dibawah
String randomkey="";
for (int i = 0; i < editor.getText().length(); i++) {
randomNumber = Math.random()*100;
//100 bloeh diubah berapa aja rekomendasi jgn <40 coba sendiri
randomkey = randomkey + (char)randomNumber;
//iniperlu untuk mengubah String randomkey="" ketingkat berikutnya
}
editor2.setText(randomkey);
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void txtPassActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void txtCPassActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void msg(String pesan) {
JOptionPane.showMessageDialog(this,pesan,"Informasi",
JOptionPane.INFORMATION_MESSAGE);
}
private boolean okToReplace() {
return (JOptionPane.showConfirmDialog(this,
"File "+file.getName()+" Sudah Ada\n"+
"Akan Ditimpa ?",
"Peringatan",JOptionPane.YES_NO_OPTION)==0) ;
}
private void SimpanFile2() {
PrintWriter pw = null;
try {
pw = new PrintWriter(new BufferedWriter(new FileWriter(file)));
} catch (IOException e) {
msg("Gagal Simpan File : '" + file.getName());
return;
}
pw.print(editor2.getText());
pw.close();
this.setTitle(file.getName()+" - NOTES" );
Nfile = false;
}
private void SimpanFile() {
PrintWriter pw = null;
try {
pw = new PrintWriter(new BufferedWriter(new FileWriter(file)));
} catch (IOException e) {
msg("Gagal Simpan File : '" + file.getName());
return;
}
pw.print(editor.getText());
pw.close();
this.setTitle(file.getName()+" - NOTES" );
Nfile = false;
}
String getData,cs,main="";
char con[];
int len,hc,j=0;
int i1=0;
char bb[];
int k=0;
Character a;
public void encrypt() {
getData=editor.getText();
getData=getData.trim();
len=getData.length();
con=getData.toCharArray();
j=len-1;
getData="";
try {
for( i1=j;i1>=0;i1--){
a=con[i1];
k=(int)a;
k+=(int)spnGeser.getValue();
a=(char)k;
getData=getData+(a.toString());
} }
catch(Exception e){
}
System.out.println(getData);
System.out.println(len);
editor2.setText(getData);
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new UserInterface().setVisible(true);
}
});
}
class EncryptMD5 {
private String convertToHex(byte[] data) {
StringBuilder buf = new StringBuilder();
for (int i = 0; i < data.length; i++) {
int halfbyte = (data[i] >>> 4) & 0x0F;
int two_halfs = 0;
do {
if ((0 <= halfbyte) && (halfbyte <= 9))
buf.append((char) ('0' + halfbyte));
else
buf.append((char) ('a' + (halfbyte - (int)spnGeser.getValue())));
halfbyte = data[i] & 0x0F;
} while(two_halfs++ < 1);
}
return buf.toString();
}
public String MD5(String text)
throws NoSuchAlgorithmException, UnsupportedEncodingException {
MessageDigest md;
md = MessageDigest.getInstance("MD5");
byte[] md5hash = new byte[32];
md.update(text.getBytes("iso-8859-1"), 0, text.length());
md5hash = md.digest();
return convertToHex(md5hash);
}
}
----------------------------------------------------java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new UserInterface().setVisible(true);
}
});
}
class EncryptMD5 {
private String convertToHex(byte[] data) {
StringBuilder buf = new StringBuilder();
for (int i = 0; i < data.length; i++) {
int halfbyte = (data[i] >>> 4) & 0x0F;
int two_halfs = 0;
do {
if ((0 <= halfbyte) && (halfbyte <= 9))
buf.append((char) ('0' + halfbyte));
else
buf.append((char) ('a' + (halfbyte - (int)spnGeser.getValue())));
halfbyte = data[i] & 0x0F;
} while(two_halfs++ < 1);
}
return buf.toString();
}
public String MD5(String text)
throws NoSuchAlgorithmException, UnsupportedEncodingException {
MessageDigest md;
md = MessageDigest.getInstance("MD5");
byte[] md5hash = new byte[32];
md.update(text.getBytes("iso-8859-1"), 0, text.length());
md5hash = md.digest();
return convertToHex(md5hash);
}
}
hasil dari desaig Plain text coding diatas :
----------------------------------------------------
0 komentar:
Posting Komentar