5

In C#, if you want to read a string without having to escape the characters, you can use an at-quote

String file = @"C:\filename.txt"

which is equivalent to

String file = "C:\\filename.txt"

Is there a simple way to escape an entire string in Java?

amccormack
  • 13,207
  • 10
  • 38
  • 61
  • possible duplicate http://stackoverflow.com/questions/2673855/java-equivalent-of-cs-verbatim-strings-with and http://stackoverflow.com/questions/2018556/does-java-have-the-character-to-escape-string-quotes/2018583 – nan Dec 11 '10 at 18:19

3 Answers3

5

No, unfortunately not.

As a side note: If you don't want to support Windows 9x and ME anymore, you can use "/" as folder separator. It works on all operating systems, including Microsoft Windows since 2000.

Hendrik Brummermann
  • 8,242
  • 3
  • 31
  • 55
  • Thanks. The filename needing to be escaped was the first example I thought of, but in practice is not what I was trying to fix. – amccormack Dec 11 '10 at 18:29
2

No. And don't hardwire file paths.

What you can do is replace one character for another.

String file = reverseSlashes("C:/filename.txt");
Tom Hawtin - tackline
  • 145,806
  • 30
  • 211
  • 305
-1

Well in Java you would use the follow. Java like C and C++ tend to arrange things so you don't need to use \

String file = "C:/filename.txt"

EDIT: If you want to write your text without escapes first, you can cut the text as is and paste into your string and your IDE should place \, \t, and \n as required.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130