0

Possible Duplicate:
How do I print escape characters in Java

I am writing a program which reads a String from a file and generates tokens from that file.

Eg. if the file is '1\t .\n$$$'

then the java String I want is exactly same. So String str should look like '1\t .\n$$$'

The problem is that as soon as I encounter a '\' (backslash) which can represent a \t or a \n, then the next character (t or n) gets lost.

I am using bufferedreader class and reading the file character by character by using the function read. Here is the piece of code.

    Charset encoding = Charset.defaultCharset();
    InputStream in = new FileInputStream(new File (filepath));
    Reader reader = new InputStreamReader(in, encoding);
    // buffer for efficiency
    buffer = new BufferedReader(reader);
    String temp = "";
    while ((r = buffer.read()) != -1) {
        ch = (char) r;
        if (ch != '\\') {
        temp += ch;
        } else if (ch == '\\'){
         ch = (char) buffer.read();
         if (ch == 'n') {
            temp += "\\n";
         } else {
            temp += "\\t";
         }
        }
    } 

I have also tried using the UTF-8 charset for encoding

 (Charset encoding = Charset.forName("UTF-8");)

but that does not help.

Thanks in advance.

Community
  • 1
  • 1
Deepti Jain
  • 1,901
  • 4
  • 21
  • 29
  • This has been answered before: http://stackoverflow.com/questions/7888004/how-do-i-print-escape-characters-in-java – Werner Kvalem Vesterås Oct 27 '12 at 13:52
  • That link does not help as the case statement is not able to recognize the character as '\n' or '\t'. Whenever a '\n' or a '\t' is encountered is returns the character as '\\' for both the cases and therefore I cannot differentiate between the two escape sequences. – Deepti Jain Oct 27 '12 at 14:03
  • Maybe I misunderstand the question, but I'd just replace("\\", "\\\\"). – ignis Oct 27 '12 at 14:04
  • It is '\\' which is a character. Whereas "\\\\" is a string which will give an error if I compare it with the char ch. – Deepti Jain Oct 27 '12 at 14:07
  • No, I mean, I'd just invoke String.replace on the input, without a manual iteration. I'd do that if what you want is to see slash+char in the output when the string contains a slash+char couple that is treated specially by println etc. – ignis Oct 27 '12 at 14:10
  • The input is a file, not a String. That is the issue. What I am trying to do is to make a String from the file. – Deepti Jain Oct 27 '12 at 14:15
  • 3
    Your string is ambiguous. You say "if the file contains '1\t.\n$$$'"; but that could represent 7 characters (if the \ are escape characters) or 9 characters (if the \ are characters in the file followed by 't' and 'n'. – arcy Oct 27 '12 at 14:19
  • I guess whenever a '\t' or a '\n' is encountered it is treated as a single character. At least that is what happens on my system and this string represents 8 chars (including the space between the . and \, and excluding the outer qoutes) – Deepti Jain Oct 27 '12 at 14:23
  • The question is very vague and Link in the provided by `Werner` should work for you. – Amit Deshpande Oct 27 '12 at 14:24
  • @deepti If you are correct and \t and \n are single characters, then your code is incorrect. If you are correct, there is no \ character in the string above; \t is a way of printing a representation of one character, a tab character, and when you read the \t character you will get a tab, not a \ and a t. Put a trace statement in the code above where you have read the character, and see if \t doesn't come back as a character with an integer value of 9. If that is the case, then you will have to write code that takes that value and prints it out as \t, and likewise for other non-printables. – arcy Oct 27 '12 at 14:43

0 Answers0