简体   繁体   中英

In Java, what is the “char” for a tab and how do I pass through a “Charset”?

These are probably really basic questions but I'm stumped. Everywhere I read says "\\t" is the escape sequence for a tab but I need a char. I also tried to create the chartset as "new Charset("UTF-8");" but I get the error "Cannot instantiate the type Charset"

I'm trying to use the following library in my program to output a CSV tab delimited file. The function requires the following parameters.

public CsvWriter(String fileName, char delimiter, Charset charset)

http://www.java2s.com/Code/Java/File-Input-Output/Writingdelimitedtextdatatoafileorastream.htm

Thank you so much!

Have you tried char tab = '\\t'; ?

The tab character is \\t .

The StandardCharsets type provides a static field that holds a reference to a UTF-8 Charset object . You can also use

Charset utf8 = Charset.forName("UTF-8");

You seem to be misunderstanding something.

A Charset is not a character at all. It is a Java object which embodies two things: a character decoding process, and a character encoding process.

The decoding process turns a sequence of byte s into a sequence of char s; the encoding process does the reverse. Unicode calls this a "character encoding". The most commonly used, and by far, is UTF-8 (UTF stands for "Unicode Transformation Format").

It is necessary to specify that to the CSV reader constructor because the contents of the file are, ultimately, bytes; the CSV reader needs to know how it will decode these bytes into char s, therefore it needs to be told which decoding process to use, and in Java, this means passing it a Charset object.

For more information, see CharsetEncoder and CharsetDecoder .

As to what the tab character constant is, you have been told the answer already so this will not be repeated here.

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