开发者

How to remove commas from a text file?

I got this far, but it seems that buffer won't take arrays, because first I had it this way

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]);
  }

now I leave at this, because it's reading but not taking out commas.

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());
    }
}

of how it should look like

input

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

output

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


If you only want to remove commas from a String, you can use String.replaceAll(",",""); If you want to replace them by spaces, use String.replaceAll(","," "):

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

Also in your code you seem to split the input, but don't use the result of this operation.


Easier is to define a new InputStream that just removes the commas...

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;
      }
    }
  }
}

Now you can read the file without commas:

InputStream noCommasStream = new CommaRemovingStream(new FileInputStream(file));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜