开发者

writing '\t' '\n' etc. characters in a file like a string_?

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开发者_Go百科();

        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.


It's not really clear what you mean, but I suspect you're just trying to escape the backslash:

 pr2.print('"' + s_str.toUpperCase() + "\\t\"+"); 

(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 \".)


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".

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜