[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Reading File into String when file contains null characters

Posted on 2003-10-28
14
Medium Priority
?
5,711 Views
Last Modified: 2010-03-31
Can anyone let me know how to read a file into a string when the file contains null characters inside of it. (The file data is encrypted.)

The piece of code that works is this:

                File file = new File(fileName);
      FileReader fr = new FileReader(file);
      BufferedReader inFile = new BufferedReader(fr);
                String line = infile.readLine();
                System.out.println(line);

Now the output of this is all fine but as soon as the file contains null characters inside of it the String does not read the file properly. Strange things happen, no errors just the println is all wrong.

I was thinking something along the lines of reading a character at a time in a loop and appending each character to the end of a String but have not been able to find out how to do that.

PLEASE HELP!!!!!!!!!


       
0
Comment
Question by:thejackwhite
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 6
14 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 9633281
You'd have to do your own encoding. Read it as a byte array, as you can't use Reader if there is any 'binary' stuff in there.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9633308
...but why would you want to display an encrypted file with these undisplayable 'holes' (null characters) in it? And there may be more undisplayable ones too
0
 

Author Comment

by:thejackwhite
ID: 9633440
Well its more that I need to pass it as a String and i am just printlining it out to test that the correct thing has been put into the string.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Accepted Solution

by:
CEHJ earned 250 total points
ID: 9633462
That's not going to be reliable as String must have a valid content in the chosen encoding in order to be encoded properly. Pass it as a byte array.
0
 

Author Comment

by:thejackwhite
ID: 9633661
Ok I have managed to read it in as ByteArrayOutputStream and that has worked with the nulls, but for the reasons of my program (just trust me on this one, couldn't be bothered explaining it) the ByteArrayOutputStream needs to be serialized.

Can this be done, do you (or anyone else) know?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9633820
Get the byte array from the BAOS (call toByteArray) and serialize that. Streams themselves are not serializable.
0
 

Author Comment

by:thejackwhite
ID: 9633955
Exactly how do I serialize a Byte[]?

I can't find the code to be able to do it and i'm not sure if you can do it?

Can you help?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9634156
objectOutputStream.write(yourByteArray);
0
 

Author Comment

by:thejackwhite
ID: 9634364
I'm sorry i'm not understanding your answers.

When doing the following code.

import java.util.*;

import java.io.*;

class test
{
   public static void main (String[] args) throws IOException
   {
                File file1 = new File("//192.168.0.17/rmit/test2.script");
          byte[] buffer = new byte[(int)file1.length()];
          BufferedInputStream is = new BufferedInputStream(new FileInputStream(file1));
          is.read(buffer, 0, (int)file1.length());
          is.close();
          ByteArrayOutputStream baos = new ByteArrayOutputStream();
          baos.write(buffer, 0, (int)file1.length());
         System.out.println(baos.toString());

      byte[] pass = baos.toByteArray();
      System.out.println (pass.toString());
   }
}

There is nothing wrong with reading in the file or anything but 'baos.toByteValue( )' doesn't seem to write properly.

I have read that 'toByteValue' will not pass the BAOS properly.

So the problem remains. How can I pull in the contents of a file into something like a byte[] and have it serialised ready to be returned. All this when the contents of the file will have null characters inside of it (so this rules out Strings apparently).  
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9634395
I thought you said you were serializing the byte array/ passing it to a function? Why are you calling toString() on it? If you do - you're right back to square one!
0
 

Author Comment

by:thejackwhite
ID: 9634498
Yeah I have put the toString in to check that the pass[] has the correct contents
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9634637
Well - we're going round in circles here. If a String has invalid contents, i.e. it's not encodable (null chars, non-printables,etc.) then you *can't* represent it as a String, unless in another format. A standard way of doing this is to print its hex value.
0
 

Author Comment

by:thejackwhite
ID: 9635348
Ok Sorry mate, its 4:30 am in Australia and i haven't had much sleep. But, i have soughted it out. Thanks for your help. Hope the points were worth the trouble
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9635461
:-)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question