Hi all,
I want to read a file from a particular path like
"c:\temp\java\data.txt"
here i am reading the file "data.txt".
and same i want to do with writing a file. please anyone help me on this.
regards,
shreeharsha
Printable View
Hi all,
I want to read a file from a particular path like
"c:\temp\java\data.txt"
here i am reading the file "data.txt".
and same i want to do with writing a file. please anyone help me on this.
regards,
shreeharsha
Please post the code so that we can correct you.
Hi,
below is the code...
i am doing encryption and decryption
import java.io.DataInputStream;
import java.io.File;
import java.io.FileReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.security.InvalidKeyException;
import java.security.Key;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
public class LocalEncrypter {
private static String algorithm = "DESede";
private static Key key = null;
private static Cipher cipher = null;
private static void setUp() throws Exception {
key = KeyGenerator.getInstance(algorithm).generateKey();
cipher = Cipher.getInstance(algorithm);
}
public static void main(String[] args)
throws Exception {
setUp();
if (args.length !=1) {
System.out.println(
"USAGE: java LocalEncrypter " +
"[String]");
System.exit(1);
}
File keyFile= new File("c:\harsha\javatry\data.txt");
DataInputStream in = new DataInputStream(new FileInputStream(keyFile));
byte[] rawkey = new byte[(int) keyFile.length()];
in.readFully(rawkey);
in.close();
DESedeKeySpec keyspec = new DESedeKeySpec(rawkey);
SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("DESede");
SecretKey key = keyfactory.generateSecret(keyspec);
//encryption
byte[] encryptionBytes = null;
encryptionBytes = encrypt(keyFile);
FileOutputStream out= new FileOutputStream("c:\harsha\javatry\Encrypt.txt");
FileOutputStream out1=new FileOutputStream("Key.txt");
PrintStream pr= new PrintStream(out);
pr.println(encryptionBytes);
pr=new PrintStream(out1);
pr.println(key);
pr.close();
//decryption
String decryp=decrypt(encryptionBytes);
FileOutputStream out2= new FileOutputStream("c:\harsha\javatry\Decrypt.csv");
PrintStream pr1=new PrintStream(out2);
pr1.println(decryp);
pr1.close();
}
private static byte[] encrypt(File keyF)
throws InvalidKeyException,
BadPaddingException,
IllegalBlockSizeException,IOException {
DataInputStream in = new DataInputStream(new FileInputStream(keyF));
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] encrData = new byte[(int) keyF.length()];
in.readFully(encrData);
in.close();
return cipher.doFinal(encrData);
}
private static String decrypt(byte[] encrBytes)
throws InvalidKeyException,
BadPaddingException,
IllegalBlockSizeException, IOException{
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] decrBytes = cipher.doFinal(encrBytes);
String recovered =new String(decrBytes);
return recovered;
//return cipher.doFinal(encryptionBytes);
}
}
regards,
harsha
hi,
its working with writing the file but how to read file from that path?
regards,
harsha