File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java encoding

 
Meet Gaurav
Ranch Hand
Posts: 492
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Meet Gaurav
Ranch Hand
Posts: 492
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 492
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 492
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic