0

i have to write a little parsing application, which is rewriting a postscriptfile with some additional infos.

To reach that goal, i am just using a BufferedReader with a FileReader and a FileWriter. But i am reaching into a problem with an escape character (the % sign).

Java is ignoring this character, the line "%!PS-Adobe-3.0" from the input file will be "!PS-Adobe-3.0" in the String and further the same in the output file.

Do you fellaz have any ideas, how i can do that ... except from reading bytewise :D

Best regards

edit: Here is the code fragment ... the base file is in encoded in UTF8 :

BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(args[0]), "UTF-8")); String line = br.readLine();

Before i testet it with the following: BufferedReader br = new BufferedReader(new FileReader(args[0]));

It fails at the same problem -> "%!PS-Adobe-3.0" from the input file became "!PS-Adobe-3.0" in the variable "line"

5
  • Show the code where you're trying to read that. % is not special to any of the classes you've listed. Commented May 26, 2014 at 11:49
  • 1
    There's nothing about your now-quoted code that would prevent your reading the % correctly. Either the character isn't there and you just think it is (perhaps whatever you're using to look at it uses % to indicate something?), or you're doing something else, elsewhere, that strips it out or treats it specially. Commented May 26, 2014 at 12:00
  • Here are the first lines of the .ps File i try to read into java: %!PS-Adobe-3.0 %%Creator: CompuSet Version 9.4.0 %%CreationDate: 5/15/2014 11:11:16 %%BoundingBox: 0 0 595 842 %%Pages: (atend) %%DocumentNeededResources: (atend) %%DocumentSuppliedResources: (atend) The first % sign at the lines which are begining with "%%" will also be ignored. Commented May 26, 2014 at 12:01
  • And no ... i am creating the BufferedReader and a the next line i am using br.readLine() ... how should i "manipulate" it otherwise. Thanks for your great answer Commented May 26, 2014 at 12:08
  • See above: If those characters are really there, they will be read by the above. What makes you think they aren't being? Commented May 26, 2014 at 12:29

1 Answer 1

1

The % will be read from the file using the code you've quoted. As I've said in comments on the question, if the characters are really there (and I expect they are), the issue is not in that code, but in the code later.

From this comment:

%!PS-Adobe-3.0 %%Creator: CompuSet Version 9.4.0 %%CreationDate: 5/15/2014 11:11:16 %%BoundingBox: 0 0 595 842 %%Pages: (atend) %%DocumentNeededResources: (atend) %%DocumentSuppliedResources: (atend) The first % sign at the lines which are begining with "%%" will also be ignored.

...I'm guessing you're outputting what you're reading using System.out.printf, but incorrectly like this:

System.out.printf(line); // <=== Wrong

The first string you pass into printf is a formatting string, and it can placeholders that start with %. The %% is a special "placeholder" that outputs %.

So you're not really seeing the contents of line. Try using print or println instead:

System.out.println(line);

...or use printf correctly:

System.out.printf("%s\n", line);

%s outputs a string unchanged.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.