Google Answers Logo
View Question
 
Q: ByteArray to String in J2ME (Java Micro Edition) ( No Answer,   1 Comment )
Question  
Subject: ByteArray to String in J2ME (Java Micro Edition)
Category: Computers > Programming
Asked by: spinal-ga
List Price: $2.00
Posted: 25 May 2006 16:08 PDT
Expires: 24 Jun 2006 16:08 PDT
Question ID: 732424
How do I read a String contained in a file stored as a ByteArray[] in J2ME?
(e.g.
some file, read and transferred over wifi; received as a byteArray[]
fileName = sampleFile.txt
file Contents:
hello world/n
eof
------------------------------------------------------
String fileToString (ByteArray[] ba, String fileName){
String s = null;
// how to do?
// s = "hello world" from ba
return s;

Thanks in advance!

Clarification of Question by spinal-ga on 25 May 2006 16:13 PDT
clarification:

// s = "hello world/n" from ba

i.e. with or without the /n, there will always be only one line of text anyways...
Answer  
There is no answer at this time.

Comments  
Subject: Re: ByteArray to String in J2ME (Java Micro Edition)
From: zoiber-ga on 09 Jun 2006 08:41 PDT
 
You could try something like...

	public byte[] readUnicodeFile(String filename) throws Exception {
	
	     StringBuffer buffer = null;
	     InputStream is = null;
	     InputStreamReader isr = null;
	     try {
	       Class c = this.getClass();
	       is = c.getResourceAsStream("/"+filename);
	       if (is == null)
	         throw new Exception("File Does Not Exist");
	       
	       isr = new InputStreamReader(is,"UTF8");     

	       buffer = new StringBuffer();
	       int ch;
	       while ((ch = isr.read()) > -1) {
        	 	buffer.append((char)ch);
	       }      
	       if (isr != null) 
	         isr.close();              
	     } catch (Exception ex) {
	       System.out.println("Exception="+ex);
	     }
	     return buffer.toString().getBytes();      
	   }

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy