gpt4 book ai didi

java - 如何从文本文件中删除逗号?

转载 作者:行者123 更新时间:2023-12-01 17:36:29 25 4
gpt4 key购买 nike

我到目前为止,但似乎缓冲区不会接受数组,因为首先我是这样的

while ((strLine = br.readLine()) != null)   
{
// Print the content on the console
// System.out.println (strLine);
String Record = strLine;
String delims = "[,]";
String[] LineItem = Record.split(delims);

//for (int i = 0; i < LineItem.length; i++)
for (int i = 0; i == 7; i++)
{
System.out.print(LineItem[i]);
}

现在我离开这里,因为它正在阅读但没有去掉逗号。

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.io.IOException;

public class mainPro1test {
public static void main(String[] args) {
File file = new File("test.txt");
StringBuffer contents = new StringBuffer();
BufferedReader reader = null;

try {
reader = new BufferedReader(new FileReader("C:\\2010_Transactions.txt"));
String text = null;

// repeat until all lines is read
while ((text = reader.readLine()) != null) {

String Record = text;
String delims = "[,]";
String[] LineItem = Record.split(delims);

contents.append(text)
.append(System.getProperty(
"line.separator"));


}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}

// show file contents here
System.out.println(contents.toString());
}
}

它应该是什么样子

输入

Sell,400,IWM   ,7/6/2010,62.481125,24988.02,4.43
Sell,400,IWM ,7/6/2010,62.51,24999.57,4.43

输出

Sell    400    IWM    7/6/2010    62.481125    24988.02    4.43
Sell 400 IWM 7/6/2010 62.51 24999.57 4.43

最佳答案

如果只想从字符串中删除逗号,可以使用String.replaceAll(",","");如果您想用空格替换它们,请使用 String.replaceAll(",",""):

while ((text = reader.readLine()) != null) {
contents.append(text.replaceAll(","," ");
}

此外,在您的代码中,您似乎分割了输入,但不使用此操作的结果。

关于java - 如何从文本文件中删除逗号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5766680/

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