private void put ( PrintStream p , String s ) { if ( s == null ) { // nl(); put ( p , " " ) ; return ; } if ( wasPreviousField ) { p . print ( separator ) ; } if ( trim ) { s = s . trim ( ) ; } if ( s . indexOf ( quote ) >= 0 ) { /* worst case, needs surrounding quotes and internal quotes doubled */ p . print ( quote ) ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { char c = s . charAt ( i ) ; if ( c == quote ) { p . print ( quote ) ; p . print ( quote ) ; } else { p . print ( c ) ; } } p . print ( quote ) ; } else if ( quoteLevel == 2 || quoteLevel == 1 && s . indexOf ( ' ' ) >= 0 || s . indexOf ( separator ) >= 0 ) { /* need surrounding quotes */ p . print ( quote ) ; p . print ( s ) ; p . print ( quote ) ; } else { /* ordinary case, no surrounding quotes needed */ p . print ( s ) ; } /* make a note to print trailing comma later */ wasPreviousField = true ; }
Write one csv field to the file followed by a separator unless it is the last field on the line . Lead and trailing blanks will be removed .