Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Sep 14th, 2004, 7:44 PM   #1
qub333
Newbie
 
Join Date: Aug 2004
Posts: 13
Rep Power: 0 qub333 is on a distinguished road
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.
qub333 is offline   Reply With Quote
Old Sep 15th, 2004, 3:14 AM   #2
Berto
Programming Guru
 
Join Date: Aug 2004
Posts: 1,022
Rep Power: 6 Berto is on a distinguished road
Send a message via AIM to Berto Send a message via MSN to Berto
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
Berto is offline   Reply With Quote
Old Oct 1st, 2004, 3:11 PM   #3
mithos177
Newbie
 
Join Date: Oct 2004
Posts: 5
Rep Power: 0 mithos177 is on a distinguished road
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.
mithos177 is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:21 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC