Q:

How do you encrypt data using a key file in java?

I'm trying to make a program which uses RSA encryption to encrypt and decrypt data sent to and from multiple computers. I have a folder which stores the public keys of other computers, on my computer. I can't however, seem to load this key file and create a publicKey object which points to it. My current attempt is shown below but I was wondering what methods other people use to do this?
      //Returns the key of a friend (given as input)

  public PublicKey getFriendsKey(String friend){
    try{ObjectInputStream friendsKeyInput = new ObjectInputStream(new fileInputStream(programLocation + "/Text Files/encryptionKeys/" + friend + "PublicKey.key"));
    PublicKey friendsKey = friendsKeyInput.readObject();
    }
    catch(Exception e){e.printStackTrace();}
    return friendsKey;
  }

A:

Why are you using ObjectInputStream? Most probably your key is not a serializable java object. 
See this answer.
Load RSA public key from file

