简体   繁体   中英

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();

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM