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

发布于 2024-11-02 22:01:10 字数 2122 浏览 0 评论 0原文

我已经走到这里了,但似乎缓冲区不会接受数组,因为首先我是这样的,

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

咿呀咿呀哟 2024-11-09 22:01:10

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

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

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

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.

赢得她心 2024-11-09 22:01:10

更简单的是定义一个新的 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));

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));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文