2 Replies - 2181 Views - Last Post: 22 January 2011 - 08:23 PM Rate Topic: -----

#1 villalandron  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 153
  • Joined: 24-September 08

File Encryption Filter

Posted 22 January 2011 - 07:48 PM

Good Evening everyone!

I am trying to build a simple encryption program that could read the first file one character at a time, and add 10 to the character code of each character before it is written to the second file. By reading the whole chapter on my java book, there is no mention of encryption anywhere. I am using an example from the book on writing data to binary file but I am stuck! Here's the program and I will explain the issues that I have after the program!

import java.io.*;				// Needed to open and modify binary data


public class FileEncryptionFilter

  {
  public static void main(String [] args)
  								throws IOException
  { 
     // An array to write the file
	  int[] numbers = { 2, 4, 6, 8, 10, 12, 14 };
	  
	  // Create the binary output objects
	  FileOutputStream fstream =
	  				new FileOutputStream( "Numbers.dat");
	  DataOutputStream outputFile =
	  				new DataOutputStream( fstream);
					
	  System.out.println( "Writing the numbers to the file...");
	  
	  // Write the array elements to the file  
	  for (int i = 0; i < numbers.length; i++)
	  		outputFile.writeInt(numbers[i]);
	  
	  System.out.println( "Done.");
	  
	  System.out.println( "Encrypting.....");
	  outputFile.writeInt(numbers);
	  
	  System.out.println( "Done.");
	  
	  // Close the file
	  outputFile.close();
	}
}   



I tried to add outputFile.writeInt(numbers[i + 10]) within the for loop but I got error messages. Any advice will be greatly appreciated!

Is This A Good Question/Topic? 0
  • +

Replies To: File Encryption Filter

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10563
  • View blog
  • Posts: 39,087
  • Joined: 27-December 08

Re: File Encryption Filter

Posted 22 January 2011 - 07:54 PM

In the future, please post your specific error messages. It allows us to better help you debug. :)

However, I have a feeling it is because you write numbers[i+10] (adding 10 to the index), rather than index[i] + 10, adding 10 to the value.
Was This Post Helpful? 0
  • +
  • -

#3 villalandron  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 153
  • Joined: 24-September 08

Re: File Encryption Filter

Posted 22 January 2011 - 08:23 PM

View Postmacosxnerd101, on 22 January 2011 - 07:54 PM, said:

In the future, please post your specific error messages. It allows us to better help you debug. :)

However, I have a feeling it is because you write numbers[i+10] (adding 10 to the index), rather than index[i] + 10, adding 10 to the value.


That was exactly the problem. Thank you for your help!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1