写“\t” '\n'等文件中的字符,例如 string_?
您好,我正在尝试编写一个自动生成代码并写入文件的代码。所以问题是当我尝试编写 '\t' '\n' 等字符时我遇到了问题。
FileOutputStream fos2 = new FileOutputStream("...\\PersonalList.java");
PrintStream pr2 = new PrintStream(fos2);
for (Iterator<String> itr = name.iterator(); itr.hasNext();) {
i++;
s_str = itr.next();
if(i==counter)
pr2.print('"' + s_str.toUpperCase() + '"' + ");\n");
else
pr2.print('"' + s_str.toUpperCase() + '\t' + '"' + '+');
}
我的目标是将代码“pr.println(”var1\t”+“var2\t”)写入另一个文件中,当我编译该文件时,它将创建一个文本文件,所以当我查看我的.txt时文件我应该看到“NAME(这里必须是空格字符)LAST_NAME”,但它写在“\t”字符中。 pr.println("var1" + "var2").我希望我能正确解释我的工作。 :) 如果您能帮助我,我将不胜感激。
hi there i am trying to write a code that generates automatically code and writes in a file.so the problem is when i try to write '\t' '\n' etc. characters i am facing with problems.
FileOutputStream fos2 = new FileOutputStream("...\\PersonalList.java");
PrintStream pr2 = new PrintStream(fos2);
for (Iterator<String> itr = name.iterator(); itr.hasNext();) {
i++;
s_str = itr.next();
if(i==counter)
pr2.print('"' + s_str.toUpperCase() + '"' + ");\n");
else
pr2.print('"' + s_str.toUpperCase() + '\t' + '"' + '+');
}
and my goal is writing a code for example "pr.println("var1\t" + "var2\t") into another file and when i compile that file it would create a text file, so when i look at my .txt file i should see "NAME (here must be white space character) LAST_NAME". but in '\t' character it writes
pr.println("var1" + "var2"). i hope i explain my work correctly. : ) and i appreciated if you could help me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不太清楚你的意思,但我怀疑你只是想逃避反斜杠:(
我冒昧地将最后的所有字符组合成一个字符串,这意味着我们还需要转义双引号,因此是
\"
。)It's not really clear what you mean, but I suspect you're just trying to escape the backslash:
(I've taken the liberty of combining all the characters you've got at the end into a single string, which means we need to escape the double quote as well, hence the
\"
.)反斜杠字符是一个特殊字符,用于转义制表符 (
\t
)、换行符 (\n
) 等。因此,如果您想打印反斜杠,您可以必须输入\\
。在您的特定情况下,您希望打印出
"\\t"
。The backslash character is a special character used to escape things like tabs (
\t
), newlines (\n
), etc. So if you want to print a backslash, you have to type\\
.In your particular case, you want to print out
"\\t"
.