Name: Anonymous 2011-04-03 20:43
Here's my story:
I'm writing a basic program that "encrypts" a .txt file by converting characters to ASCII values, and the program compiles with no problems.
However, the program hangs when printing to a new file. The file is created, but cannot be opened and has no text within.
Can anyone help? I'm not good with I/O operations, and I haven't coded in Java for a long time.
Here's my code:
//Encryption.java
//This program reads a text file and creates an encrypted version of the text file
import java.util.*;
import java.io.*;
public class Encryption {
//declare variables
public static String line;
public static int lineEncrypted;
public static int i;
public static int lineInt;
public static char lineChar;
public static void main(String[] args) throws IOException {
System.out.println("Input file: Secret.txt");
//input file
BufferedReader in = new BufferedReader(new FileReader("Secret.txt"));
line = in.readLine();
//while there is more data to read
while(line != null) {
encrypt();
line = in.readLine();
}
}
public static void encrypt() throws IOException {
//convert each character in the line to its ASCII value
for(int i = 0; i < line.length(); i++) {
char lineChar = line.charAt(i);
int lineInt = (int) lineChar;
print();
}
}
public static void print() throws IOException {
System.out.println("Output file: EncryptedSecret.txt");
//output file
PrintWriter out = new PrintWriter(new FileWriter("EncryptedSecret.txt"));
//while there is more data to write
while(line != null) {
out.println(lineInt + " ");
}
//close file
out.close();
}
}
I'm writing a basic program that "encrypts" a .txt file by converting characters to ASCII values, and the program compiles with no problems.
However, the program hangs when printing to a new file. The file is created, but cannot be opened and has no text within.
Can anyone help? I'm not good with I/O operations, and I haven't coded in Java for a long time.
Here's my code:
//Encryption.java
//This program reads a text file and creates an encrypted version of the text file
import java.util.*;
import java.io.*;
public class Encryption {
//declare variables
public static String line;
public static int lineEncrypted;
public static int i;
public static int lineInt;
public static char lineChar;
public static void main(String[] args) throws IOException {
System.out.println("Input file: Secret.txt");
//input file
BufferedReader in = new BufferedReader(new FileReader("Secret.txt"));
line = in.readLine();
//while there is more data to read
while(line != null) {
encrypt();
line = in.readLine();
}
}
public static void encrypt() throws IOException {
//convert each character in the line to its ASCII value
for(int i = 0; i < line.length(); i++) {
char lineChar = line.charAt(i);
int lineInt = (int) lineChar;
print();
}
}
public static void print() throws IOException {
System.out.println("Output file: EncryptedSecret.txt");
//output file
PrintWriter out = new PrintWriter(new FileWriter("EncryptedSecret.txt"));
//while there is more data to write
while(line != null) {
out.println(lineInt + " ");
}
//close file
out.close();
}
}