aspose file tools*
The moose likes Beginning Java and the fly likes Java encoding Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Java encoding" Watch "Java encoding" New topic
Author

Java encoding

Meet Gaurav
Ranch Hand

Joined: Oct 08, 2008
Posts: 492
Hi

I have a file(contains special characters Greek) on the server and am reading that file line by line and saving it as a file in my local machine.

I used the below code for reading
in = new BufferedReader(new InputStreamReader(urlContent.openStream(), encodingType));

While writing whether I need to encode it again ? is it needed.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41823
    
  63
It is needed if you want the file to be encoded in a different encoding than the platform default encoding (whatever that may be on your machine).


Ping & DNS - my free Android networking tools app
Meet Gaurav
Ranch Hand

Joined: Oct 08, 2008
Posts: 492
Server is Unix and client in Windows. While reading am using encoding the file and agin if I encode it with same then the file become old type as in the server rite ?.


After storing that file in windows and then sending it to printer as below.

File pdfFile = new File(arg);
is = new BufferedInputStream(new FileInputStream(pdfFile));
DocAttributeSet das = new HashDocAttributeSet();
Doc doc = new SimpleDoc(is, flavor, das);
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41823
    
  63
While reading am using encoding the file and agin if I encode it with same then the file become old type as in the server rite ?.

The encoding will be the same if you use the same encoding, otherwise it will be different (maybe I'm missing what you're trying to say).

is = new BufferedInputStream(new FileInputStream(pdfFile));

Again, this will only work if the file has the platform default encoding. If you're storing the file with any other encoding, then you need to specify that encoding here.
Meet Gaurav
Ranch Hand

Joined: Oct 08, 2008
Posts: 492
Ulf,

Please help me out, How to specify the encoding here. I mean in the below code.

InputStream is = new BufferedInputStream(new FileInputStream(pdfFile));

is it possible to use the encoding string in above line.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41823
    
  63
Actually, I misremembered. For printing, not the platform default encoding is used, but US-ASCII. If the file uses a different encoding then you need to specify the charset as part of the MIME type in the DocFlavor. See its javadocs for more detail: javax.print.DocFlavor.
Meet Gaurav
Ranch Hand

Joined: Oct 08, 2008
Posts: 492
While writing the file I used UTF8 encoding and stored the file.

DocFlavor flavor = DocFlavor.INPUT_STREAM.TEXT_PLAIN_UTF_8;

If I use the above flavor I got

java.lang.IllegalArgumentException: text/plain; charset="utf-8"; class="java.io.InputStream" is an unsupported flavor
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41823
    
  63
The Print API has methods somewhere that allow you to detect/list all supported flavors. You'll have to use one of the supported ones, obviously. Which ones those are will vary from machine to machine, as it depends on the installed printer drivers and printers.
 
 
subject: Java encoding