Hanya Sebagai Pengingat

=================================

Selasa, 21 September 2010

operasi file Java

//ENKRIP DEKRIP
stringRandom sRnd = new stringRandom();

for( int j = 0; j < 3; j++ ) {
String stringRnd = sRnd.randomString(8);
System.out.println(stringRnd);
}

DesEncrypted encrip = new DesEncrypted();
String encripted = encrip.encryp("mugi");
System.out.println("ENKRIP");
System.out.println(encripted);
System.out.println("===ddddddddddddddddddddddddddddddddddddddddddddddd");
String decripted = encrip.decryp(encripted);
System.out.println(decripted);
//return "index";

//MEMBACA FILE PROPERTIES
Properties props = new Properties();
//try retrieve data from file
try {
props.load(new FileInputStream("/home/mugi22/NetBeansProjects/hnfApp/web/WEB-INF/appInit.properties"));
dirHome = props.getProperty("dirHome");
System.out.println("DIRhOME :"+dirHome);
}
//catch exception in case properties file does not exist
catch(IOException e)
{
e.printStackTrace();
}


//BIKIN FILE
File f;
f=new File(dirHome+"myfile.txt");
if(!f.exists()){
try {
f.createNewFile();
} catch (IOException ex) {
Logger.getLogger(indexController.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println("New file "+dirHome+" has been created to the current directory");

} else{
System.out.println("FILE SUDAH ADA COYYYYYYYYYY");
}

//MENULIS KE FILE
try{
// Create file
FileWriter fstream = new FileWriter(dirHome+"myfile.txt");
BufferedWriter out = new BufferedWriter(fstream);
out.write("Hello Java");
out.newLine();
out.write("aaaaaaaaaaaaaaaaaaaaaaa");
out.newLine();
out.write("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
out.newLine();
out.write("cccccccccccccccccccccccccccccccccccc");
//Close the output stream
out.close();
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}


//MEMBACA FILE
try{
// Open the file that is the first
// command line parameter
FileInputStream fstream = new FileInputStream(dirHome+"myfile.txt");
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
//Read File Line By Line
while ((strLine = br.readLine()) != null) {
// Print the content on the console
System.out.println (strLine);
}
//Close the input stream
in.close();
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}



//Append (tambah) ke file
try{
// Create file
FileWriter fstream = new FileWriter(dirHome+"myfile.txt",true);
BufferedWriter out = new BufferedWriter(fstream);
out.newLine();
out.write("Tambahan COyyyyyyyyy.........");
//Close the output stream
out.close();
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}

Tidak ada komentar: