我做到了这一点,但似乎缓冲区不会采用数组,因为首先我这样做了
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
答案 0 :(得分:4)
如果您只想从字符串中删除逗号,可以使用String.replaceAll(",","");
如果您想用空格替换它们,请使用String.replaceAll(","," ")
:
while ((text = reader.readLine()) != null) {
contents.append(text.replaceAll(","," ");
}
同样在您的代码中,您似乎拆分输入,但不要使用此操作的结果。
答案 1 :(得分:1)
更容易定义一个新的InputStream
,只删除逗号...
class CommaRemovingStream extends InputStream {
private final InputStream underlyingStream;
// Constructor
@Override public int read() throws IOException {
int next;
while (true) {
next = underlyingStream.read();
if (next != ',') {
return next;
}
}
}
}
现在你可以不用逗号读取文件:
InputStream noCommasStream = new CommaRemovingStream(new FileInputStream(file));