Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2006

Tip: Looking for answers? Try searching our database.

read file.txt in unicode

Thread view: 
Chameleon - 01 Jan 2006 14:29 GMT
I want a small piece of code (for mobile) which reads entile file
"file.txt" from JAR and store it to a string.
"file.txt" has unicode format (2 bytes for every char)

The following code does not work:
---------------------------------
String s = null;
try {
    DataInputStream dis = new
DataInputStream(LanguageLoader.class.getResourceAsStream("file.txt"));
    s = dis.DataInputStream.readUTF();
} catch(Exception e) {
}
return s;
---------------------------------
IchBin - 01 Jan 2006 17:56 GMT
> I want a small piece of code (for mobile) which reads entile file
> "file.txt" from JAR and store it to a string.
[quoted text clipped - 11 lines]
> return s;
> ---------------------------------

Not sure if this helps but there are a few examples in the The Java
Developers Almanac 1.4

To find which file entry in zip file...
*e454. Listing the Contents of a ZIP File*
http://javaalmanac.com/egs/java.util.zip/ListZip.html?l=rel

*e455. Retrieving a Compressed File from a ZIP File*
http://javaalmanac.com/egs/java.util.zip/GetZip.html?l=rel

Thanks in Advance...
IchBin, Pocono Lake, Pa, USA
http://weconsultants.servebeer.com/JHackerAppManager
__________________________________________________________________________

'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor,  Regular Guy (1952-)
Roedy Green - 02 Jan 2006 14:56 GMT
>To find which file entry in zip file...
>*e454. Listing the Contents of a ZIP File*
>http://javaalmanac.com/egs/java.util.zip/ListZip.html?l=rel

you don't need the zip stuff to get an a resource.  getResource and
getResourceAsStream do that for you.

getResource gives you an URL. getResourceAsStream gets you an
InputStream.

Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Svante Frey - 02 Jan 2006 13:27 GMT
Unicode can be encoded in several different ways. "two bytes per char"
sounds like UTF-16 or UCS-2. The readUTF() function reads UTF-8
encoding (which uses 1-4 bytes for every Unicode character), so it
can't be used to read an UTF-16 encoded file. See
http://en.wikipedia.org/wiki/Unicode for more info.
Roedy Green - 02 Jan 2006 14:55 GMT
On Sun, 01 Jan 2006 16:29:26 +0200, Chameleon
<cham_gss@hotmail.NOSPAM.com> wrote, quoted or indirectly quoted
someone who said :

>    DataInputStream dis = new
>DataInputStream(LanguageLoader.class.getResourceAsStream("file.txt"));
>    s = dis.DataInputStream.readUTF();

DataInputStream is for binary files where strings have preceding
length counts.  You want a Reader with explicit encoding.

See http://mindprod.com/applets/fileio.html

Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.