![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Aug 2004
Posts: 13
Rep Power: 0
![]() |
Ok, this sounds a bit like a HW question, so my apologies. I've been workin on some Java tuts and the sort, but Im trying to make an 'encryption' program that reads a text file, adds 2 to every charaters ASCII value, then exports it back to the file. My debugging has lead me to get everything working but writing this to a file, but for some reason the file is always blank, Im assuming its something stupid, but another pair of eyes over it (espicaly those more learned then mine) are far more likely to spot my stupid syntax error that is killin me.
import javax.swing.JOptionPane;
import javax.swing.border.*;
import java.io.*;
public class EncryptTest{
public static void main(String args[]) {
String num = JOptionPane.showInputDialog("Enter 1 to encrypt and 2 to decrypt");
int iNum = Integer.parseInt(num);
if( iNum==1){
String fileN = JOptionPane.showInputDialog("What file do you wish to encrypt?");
String fileN2 = JOptionPane.showInputDialog("What should the encrypted file be named?");
try
{
BufferedReader inputStream =
new BufferedReader(new FileReader(fileN));
PrintWriter outputStream=
new PrintWriter(new FileOutputStream(fileN2));
int index= 0, count=0;
String line = inputStream.readLine();
String oline = null;
char l;
while(line != null){
do{
outputStream.print((char)((int)line.charAt(count)+2));
count++;
}while(count<line.length());
line = inputStream.readLine();
count=0;
}
}
catch(FileNotFoundException e)
{
Kevin.outpl("File " + fileN + " is not found");
}
catch(IOException e)
{
Kevin.outpl("Error reading from file " + fileN );
}
}
System.exit(0);
}
}the kevin class import javax.swing.JOptionPane;
import javax.swing.border.*;
public class Kevin{
private String s1;
private String s2;
public static void outpl(String s1){
System.out.println(s1);
}
public static void outp(String s1){
System.out.print(s1);
}
}ive removed the comments so this will fit better, but if you would prefer I return them, no problem. Also, I realize that the class Kevin is quite pointless, but its more an effort of mine to learn this a bit better. |
|
|
|
|
|
#2 |
|
Programming Guru
![]() |
The problem i think is that your nor closing the output buffer or flushing it, so it does not write to the file.
outputStream.close(); or outputStream.flush(); if you flush then it just empties the buffer, if you close it closes the buffer along with emotying it. If the program just ends the output Buffer never empties into the file.
__________________
"Put your hand on a hot stove for a minute, and it seems like an hour. Sit with a pretty girl for an hour, and it seems like a minute. THAT'S relativity." - Albert Einstein |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Oct 2004
Posts: 5
Rep Power: 0
![]() |
I suggest you to use the outputstream.close() in order to avoid nusty bugs dealing with your file handlers especially if you are trying to work this on a windows machine.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|