开发者

Where's definitive reference on string formatting

does anyone know of a good online resource that simply and definitevly explains how to use the string formatter method...?

I need to write a series of "records" into a set ascii text files. I need 开发者_如何学Pythonto "delimit" each "record" with a cr-lf sequence in a windows 2008 server environment. Therefore I'm trying to figure out how to add a \r\n character string at the end of each "record". I tried a "record_string.append(CR) and LF" ; but it didn't work.

Thanks much

Guy


The documentation on the Formatter class appears to be comprehensive.

It has this to say about line separators:

Line Separator

The conversion does not correspond to any argument.

'n' - the platform-specific line separator as returned by System.getProperty("line.separator"). Flags, width, and precision are not applicable. If any are provided an IllegalFormatFlagsException, IllegalFormatWidthException, and IllegalFormatPrecisionException, respectively will be thrown.


If you specifically need to add CR LF to the end of each record (carriage return, linefeed), then you can just use exactly \r\n. The \r translates to a carriage return, and \n to linefeed. For example:

StringBuilder sb = new StringBuilder();
sb.append("some data");
// ...
sb.append("\r\n");  // add CR LF record separator

You can find the exact list of escape sequences that exist in Java in section 3.10.6 of the Java Language Specification.


Just do the: record_string = record_string + "\n"

on widnows \n means CR-LF

Or you can use FileWriter to use writeLine(record);

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜