Results 1 to 5 of 5
  1. #1
    Join Date
    Apr 2010
    Posts
    19
    Rep Power
    0

    Default Reading and writing the file in a specified path

    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

  2. #2
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    841
    Rep Power
    6

    Default

    Please post the code so that we can correct you.
    Ramya:cool:

  3. #3
    Join Date
    Apr 2010
    Posts
    19
    Rep Power
    0

    Default

    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

  4. #4
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by harsha.udupa2008 View Post
    FileOutputStream out= new FileOutputStream("c:\harsha\javatry\Encrypt.txt");

    if your os is windows when you use backslashes as path separator the use always \\ so that the second backslash is ignored by the jvm. so for your path use

    Quote Originally Posted by harsha.udupa2008 View Post
    FileOutputStream out= new FileOutputStream("c:\\harsha\\javatry\\Encrypt.txt ");

    no comment for the code lines.

  5. #5
    Join Date
    Apr 2010
    Posts
    19
    Rep Power
    0

    Default reading file

    hi,

    its working with writing the file but how to read file from that path?

    regards,
    harsha

Similar Threads

  1. File reading / writing
    By MattBSibley in forum New To Java
    Replies: 5
    Last Post: 04-19-2010, 05:20 AM
  2. reading file from a path
    By new_coder in forum New To Java
    Replies: 5
    Last Post: 08-17-2009, 04:52 AM
  3. Reading and writing to a file
    By jigglywiggly in forum New To Java
    Replies: 13
    Last Post: 03-09-2009, 10:44 AM
  4. Reading/Writing to file
    By Doctor Cactus in forum New To Java
    Replies: 2
    Last Post: 10-28-2008, 02:05 PM
  5. Help with File reading and writing
    By baltimore in forum New To Java
    Replies: 1
    Last Post: 07-31-2007, 06:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •