gpt4 book ai didi

java - 如何将长字符串在 "),"处分割成行?

转载 作者:太空宇宙 更新时间:2023-11-04 10:29:51 25 4
gpt4 key购买 nike

尝试正确解析文本文件有点头疼,它是从 mysql 数据库中提取的,但数据需要进行相当多的更改才能再次插入。

我的程序正在获取一个 .txt 文件并解析它以生成一个 .txt 文件,这很简单。

问题是它没有正确分割文件。该文件如下所示(每个中间字段看起来很奇怪,因为我已将其更改为随机字母以隐藏真实数据):

(92,'xxxname',4013),(93,'sss-xxx',4047),(94,'xxx-sss',3841),(95,'ssss',2593),(96,'ssss-sss',2587),(97,'Bes-sss',2589),

我想分割它,以便它生成一个如下文件:

(92, 'xxxname',4013),

(93, 'sss-xxx', 4047),

(94, 'xxx-sss', 3841),

等等...

当前解析代码如下:

public void parseSQL(File file) throws IOException {
Scanner scanner = new Scanner(file);

while (scanner.hasNext()) {
String line = scanner.next();
String[] lines = line.split(Pattern.quote("),"));

for (String aLine : lines) {
logLine(aLine);
}
}

}

public static void logLine(String message) throws IOException {
PrintWriter out = new PrintWriter(new FileWriter("output.txt", true),
true);
out.println(message);
out.close();
}

目前我得到的输出大致在正轨上,但比应有的分割更多,当然分割方法删除了“)”,这是不必要的。

当前输出示例:

*(1,'Vdddd
Cfffff',1989
(2,'Wdd',3710
(3,'Wfffff
Hffffff
Limited-TLC',3901
(4,'ffffffun88',2714
(5,'ffffff8',1135
(6,'gfgg8*

已经玩了一段时间,并在这里和其他地方做了很多搜索,但没有想法,任何帮助将不胜感激。

最佳答案

您可以使用String.replace 。也无需创建多个 PrintWriter 并每次都关闭流。

public void parseSQL(File file) throws IOException {
Scanner scanner = new Scanner(file);

PrintWriter out = new PrintWriter(new FileWriter("output.txt", true), true);
while (scanner.hasNext()) {
String line = scanner.next();
out.println(line.replace("),", ")," + System.lineSeparator()));
}
out.close();
}

关于java - 如何将长字符串在 "),"处分割成行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50138374/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com