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?
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.
No. And don't hardwire file paths.
What you can do is replace one character for another.
String file = reverseSlashes("C:/filename.txt");
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.
精彩评论