@@ -106,17 +106,23 @@ protected void writeSingleRecordInternal(Row row) throws WriteRecordException {
106106 if (rowData ==null ){
107107 sb .append (DEFAULT_NULL_DELIM );
108108 }else {
109- String data = String .valueOf (rowData );
110- if (data .contains ("\\ " )){
111- data = data .replaceAll ("\\ \\ " ,"\\ \\ \\ \\ " );
112- }
113- sb .append (data );
109+ sb .append (rowData );
114110 }
115111 if (index != lastIndex ){
116112 sb .append (DEFAULT_FIELD_DELIM );
117113 }
118114 }
119115 String rowVal = sb .toString ();
116+ if (rowVal .contains ("\\ " )){
117+ rowVal = rowVal .replaceAll ("\\ \\ " ,"\\ \\ \\ \\ " );
118+ }
119+ if (rowVal .contains ("\r " )){
120+ rowVal = rowVal .replaceAll ("\r " ,"\\ \\ r" );
121+ }
122+
123+ if (rowVal .contains ("\n " )){
124+ rowVal = rowVal .replaceAll ("\n " ,"\\ \\ n" );
125+ }
120126 ByteArrayInputStream bi = new ByteArrayInputStream (rowVal .getBytes (StandardCharsets .UTF_8 ));
121127 copyManager .copyIn (copySql , bi );
122128 } catch (Exception e ) {
@@ -155,23 +161,31 @@ protected void writeMultipleRecordsInternal() throws Exception {
155161 StringBuilder sb = new StringBuilder (128 );
156162 for (Row row : rows ) {
157163 int lastIndex = row .getArity () - 1 ;
164+ StringBuilder tempBuilder = new StringBuilder (128 );
158165 for (int index =0 ; index < row .getArity (); index ++) {
159166 Object rowData = getField (row , index );
160167 if (rowData ==null ){
161- sb .append (DEFAULT_NULL_DELIM );
168+ tempBuilder .append (DEFAULT_NULL_DELIM );
162169 }else {
163- String data = String .valueOf (rowData );
164- if (data .contains ("\\ " )){
165- data = data .replaceAll ("\\ \\ " ,"\\ \\ \\ \\ " );
166- }
167- sb .append (data );
170+ tempBuilder .append (rowData );
168171 }
169172 if (index != lastIndex ){
170- sb .append (DEFAULT_FIELD_DELIM );
173+ tempBuilder .append (DEFAULT_FIELD_DELIM );
171174 }
172175 }
176+ // \r \n \ 等特殊字符串需要转义
177+ String tempData = tempBuilder .toString ();
178+ if (tempData .contains ("\\ " )){
179+ tempData = tempData .replaceAll ("\\ \\ " ,"\\ \\ \\ \\ " );
180+ }
181+ if (tempData .contains ("\r " )){
182+ tempData = tempData .replaceAll ("\r " ,"\\ \\ r" );
183+ }
173184
174- sb .append (LINE_DELIMITER );
185+ if (tempData .contains ("\n " )){
186+ tempData = tempData .replaceAll ("\n " ,"\\ \\ n" );
187+ }
188+ sb .append (tempData ).append (LINE_DELIMITER );
175189 }
176190
177191 String rowVal = sb .toString ();
0 commit comments