Java 如何从行中删除逗号并将其写入输出文件

Java 如何从行中删除逗号并将其写入输出文件,java,Java,这是我的任务-编写一个程序,读取一个文件并删除其中的所有逗号,然后将其写回第二个文件。它应该打印到控制台窗口,最后是删除的逗号数。 该计划需要: 提示用户输入要读取的文件名。 读取文件 将非逗号字符写入output.txt,包括所有空格。 读取完输入文件后,将删除的逗号总数写入控制台窗口 例如,如果输入文件包含3+,2=5m,7%,6=1 hello 那么output.txt文件应该包含: 3+2=5米7%6=1你好 控制台窗口应显示“删除3个逗号” 现在我很难从输入文件中删除逗号,我想我应该在

这是我的任务-编写一个程序,读取一个文件并删除其中的所有逗号,然后将其写回第二个文件。它应该打印到控制台窗口,最后是删除的逗号数。 该计划需要: 提示用户输入要读取的文件名。 读取文件 将非逗号字符写入output.txt,包括所有空格。 读取完输入文件后,将删除的逗号总数写入控制台窗口

例如,如果输入文件包含3+,2=5m,7%,6=1 hello 那么output.txt文件应该包含:

3+2=5米7%6=1你好 控制台窗口应显示“删除3个逗号”

现在我很难从输入文件中删除逗号,我想我应该在最后一个if语句下写这行

试图找出如何从输入文件中删除逗号

 package pkg4.pkg4.assignment;
import java.util.Scanner;
import java.io.*;

/**
 *
 * @author bambo
 */
public class Assignment {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException {
       Scanner keyboard = new Scanner(System.in); 
       System.out.println("What is the name of the inputfile?");
       String inputfile = keyboard.nextLine();
       File f = new File(inputfile);
       Scanner inputFile = new Scanner(f);

         System.out.println("Please enter the output file");
         String outputfile = keyboard.nextLine();


         FileWriter fw = new FileWriter(outputfile);
         PrintWriter pw = new PrintWriter(fw);


       int lineNumber=0;


       while(inputFile.hasNext());
        lineNumber++;
             int commacount = 0;

              String line = inputFile.nextLine();
             if (line.length () != 0)
                 commacount++;
              for(int i=0; i< line.length(); i++)
             {
                 if(line.charAt(i) == ',');
                 {
                     commacount++;
                 }

         pw.println("removed " + commacount + "commas");

    }

}
}
包pkg4.pkg4.assignment;
导入java.util.Scanner;
导入java.io.*;
/**
*
*@作者班博
*/
公共课堂作业{
/**
*@param指定命令行参数
*/
公共静态void main(字符串[]args)引发IOException{
扫描仪键盘=新扫描仪(System.in);
System.out.println(“输入文件的名称是什么?”);
字符串inputfile=keyboard.nextLine();
文件f=新文件(inputfile);
扫描仪输入文件=新扫描仪(f);
System.out.println(“请输入输出文件”);
字符串outputfile=keyboard.nextLine();
FileWriter fw=新的FileWriter(输出文件);
PrintWriter pw=新的PrintWriter(fw);
int lineNumber=0;
while(inputFile.hasNext());
lineNumber++;
int commacount=0;
String line=inputFile.nextLine();
如果(line.length()!=0)
commacount++;
对于(int i=0;i
要在控制台上打印,您应该使用:

System.out.println("removed " + commacount + "commas");
要在输出文件中写入不带逗号的行,请执行以下操作:

pw.println(line.replaceAll(",",""));

根据您对程序的要求,为了简单起见,我建议您使用Java8类

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Scanner;

public class Assignment {

    public static void main(String[] args) throws IOException {
        String content = "";
        Scanner keyboard = new Scanner(System.in);
        System.out.println("What is the name of the input file?");
        String inputfile = keyboard.nextLine();
        content = new String(Files.readAllBytes(Paths.get(inputfile)));
        long total_numbers_of_char = content.chars().filter(num -> num == ',').count();
        System.out.println("Please enter the output file");
        content = content.replaceAll(",", "");
        String outputfile = keyboard.nextLine();
        Files.write(Paths.get(outputfile), content.getBytes());
        System.out.println("removed " + total_numbers_of_char + " commas");
        keyboard.close();
    }

}

将每个字母添加到
StringBuilder
。如果字母是逗号,请增加计数,不要将该字母添加到生成器中。将生成器内容写入文件。也可以使用逗号作为分隔符进行拆分-逗号的数量为数组长度减去1。然后,您可以简单地将拆分的数组内容打印到一个文件中。我创建了一个输入和输出文件,并将该行添加到项目的底部,现在,在输入文件名后,它将继续运行。pw.println(“删除”+逗号数+逗号”);println(line.replaceAll(“,”,“);关闭();可能是这条线吗?如果(行字符(i)=',')?因为我的输出文件仍然为空,我注意到您在println行中使用了常规引号。我试着在那条线上把它们换成普通的,但我出错了