Problem with Writing text to a File

I have a problem when I want to write text to a file. This isn’t really “normal” text as it has been run through a RSA-crypto but no matter what I do I can’t seem to save the data in the same way that I have it in my Java program so when I read it from the file and run it through the decryption it becomes wierd. And just to make sure, as long as I don’t write and read the data to a file the decryption works perfectly so it’s not that which is a problem.

The lines I encrypt: Ok
we are testing if this works
and I hope the different rows come in

What the encrypted data looks like if you use println on it: 5Ù4ÔU+?Y_+?3+:3O??OÉ?3?O:?UP_Ù:4ÔY?í?Õ??P,+?3?+?íOÉÉ+_+?3?_PU:?âPV+?O?

What it looks like after writing, reading and decryption: Ok
we¼are¼testi¼g¼if¼t¼is¼works
a¼d¼I¼¼ope¼t¼e¼differe¼t¼rows¼come¼i¼

My writing function:
public static void writeFile(String path, String text) throws IOException{
OutputStream out = null;
try{
out = new FileOutputStream(path);
byte[] totalBytes = text.getBytes();
out.write(totalBytes);
}
finally{
if(out !=null)
out.close();
}
}

My reading function:
public static String readFile(String path) throws IOException{
String sContent=null;
byte [] buffer =null;
File a_file = new File(path);
try
{
FileInputStream fis = new FileInputStream(path);
int length = (int)a_file.length();
buffer = new byte [length];
fis.read(buffer);
fis.close();
}
catch(IOException e){e.printStackTrace();}

	 sContent = new String(buffer);  
	 return sContent;  
	 }

Here it is with [ code ] tags:


	public static void writeFile(String path, String text) throws IOException{  
		 OutputStream out = null; 
		 try{  
		     out = new FileOutputStream(path);  
		     byte[] totalBytes = text.getBytes();  
		     out.write(totalBytes);  
		      }  
		    finally{  
		    if(out !=null)  
		        out.close();  
		      }  
		  }


	public static String readFile(String path) throws IOException{  
		 String sContent=null;  
		 byte [] buffer =null;  
		 File a_file = new File(path);  
		 try  
		 {  
		 FileInputStream fis = new FileInputStream(path);  
		 int length = (int)a_file.length();  
		 buffer = new byte [length];  
		 fis.read(buffer);  
		 fis.close();  
		 }  
		 catch(IOException e){e.printStackTrace();} 
		 
		 sContent = new String(buffer);  
		 return sContent;  
		 }

Perhaps try using BufferedWriter and BufferedReader?

To print to text file:

public static void writeText(String[] text) {
	try {
	    PrintWriter out = new PrintWriter((new FileWriter("blahblahblah.txt", false));
	    for(String s : text) { out.println(s); }
	    out.close();
	} catch (IOException e) {
	    e.printStackTrace();
	}
}

There is also a 1 liner to read an entire text file at once into a String array, but I can’t seem to find it atm. Something to do with NIO.

I guess you get the encrypted text as a byte[] via something like this:

public static byte[] encrypt(String text, PublicKey key)

so you can write raw bytes then read them and decrypt via

public static String decrypt(byte[] text, PrivateKey key)

.

How do you make it appear with “code tags”? I really is easier to read that way XD

Well, earlier I worked my encryption on chars but I tried making it so that I worked with bytes from the time I read the data from a file until I wrote it to a new file after having first encrypted and then decrypted it. Well, it still didn’t work but I think the problem is when I write to the file mainly. If I convert a byte into a char then it sometimes makes me lose data. I’m really at a loss here.
If anyone has some completely failsafe code for taking a String, writing it to a file, and then reading it from that file without losing ANY information no matter what chars are in the String (as long as they have a numberical value between 1 and 255 it should always work) then I would really love to get a copy of that code

I’ve meant this.
Have a look also at readers’ comments below.

(Do not include the dots) [.code]Code goes here[/.code]

=>

Code goes here

[code]
// code
[/code]

The problem you are having could be a character encoding issue. Does your encryption function have byte array input and output or String in/out?

Thank you for the help everyone!
I managed to solve the problem by simply reading/writing the encrypted text as pure short values.