输出是
INSERT INTO 'products'('productid', 'color', 'size', 'price') VALUES ('purse8901, red, small handbag, 70.00');
我想添加单引号,以便我可以像这样轻松地将转换后的 sql 文件再次导入数据库:
INSERT INTO 'products'('productid', 'color', 'size', 'price') VALUES
('purse8901', 'red', 'small handbag', '70.00');
问题是我不知道如何拆分 sCurrentLine 字符串并在上面添加 \' 。
package ConvertCSVtoSQL;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Convert {
public static void main(String[] args) throws IOException {
String begin = "INSERT INTO `produts`(`productid`, `color`. `size`,
`price`) VALUES ('";
File fileToSave = new File("C:\\Users\\Blue\\Desktop\\sql\
\products.sql");
if (!fileToSave.exists()) {
fileToSave.createNewFile();
}
FileWriter fw = new FileWriter(fileToSave.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write('\n');
String end = "');";
String sCurrentLine;
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader("C:\\Users\\Blue\\Desktop\
\products.csv"));
} catch (FileNotFoundException ex) {
Logger.getLogger(Convert.class.getName()).log(Level.SEVERE, null,
ex);
}
br.readLine();
for (int count = 1; count <= 236; count++) {
sCurrentLine = br.readLine();
bw.write(begin + sCurrentLine + end);
bw.write('\n');
}
bw.close();
}
}