删除.txt文件的每一行中的最后一个字符

时间:2018-02-27 14:52:41

标签: java file csv char

我是Java的初学者。我已经使用循环成功地将用户数据输入导出到.txt文件,但我想删除文件每行的最后一个逗号。我尝试过使用分隔符,但无法使用replaceAll成功删除每一行的最后一个逗号。

我目前导出数据的方法是:

public void Quit()
                {   
                    System.out.println(ProjectList.get(0).ProjectName);
                    File fileObject = new File("results.txt");
                    CorrectInput = true;                
                    ShowMenu = false;                   //if ShowMenu is false, the program's menu will terminate
                    PrintWriter outputStream = null;
                    try
                    {
                        outputStream =
                             new PrintWriter(new FileOutputStream("results.txt"));
                    }
                    catch(FileNotFoundException e)
                    {
                        System.out.println("Error opening the file" +"results.txt");
                        System.exit(0);
                    }
                   for (int i=0; i<ProjectList.size(); i++){
                       outputStream.print(ProjectList.get(i).ProjectName+","+ ProjectList.get(i).NumberOfMember +","); //Project Name and Number of Members exported
                       for (int Membercount = 0; Membercount < ProjectList.get(i).NumberOfMember; Membercount ++) //For as long as the member count is less than the total number of members, the program will ask for the user input
                       {   

                           outputStream.print(ProjectList.get(i).TeamMember[Membercount]);
                           outputStream.print(",");
                           //END OF LIST OF MEMBERS
                       }

                       for (int CountingIndex = 0; CountingIndex < ProjectList.get(i).NumberOfMember; CountingIndex ++) //For as long as the member count is less than the total number of members, the program will ask for the user input
                       {   
                           outputStream.print(ProjectList.get(i).TeamMember[CountingIndex] + ",");
                           for (int CountedIndex = 0; CountedIndex < ProjectList.get(i).NumberOfMember; CountedIndex++) {


                               if(CountingIndex!=CountedIndex) { //new, adding csv format
                               outputStream.print(ProjectList.get(i).TeamMember[CountedIndex] + ",");
                               outputStream.print(ProjectList.get(i).Vote[CountingIndex][CountedIndex] + ",");

                               }


                           }

                       }
                       //for (int CountedIndex = 0; CountedIndex < ProjectList.get(i).NumberOfMember && CountingIndex != CountedIndex; CountedIndex++)


                       outputStream.println();
                   }  
                   outputStream.close();
                // read file data into a String
                // read file data into a String
                   String data = null;
                try {
                    data = new Scanner(new File("results.txt")).useDelimiter("\\Z").next();
                    data = data.replaceAll("(?m)\\,$", " ");
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }


                    System.out.println("\tGoodbye. ");      
                    scan.close();

                }

示例.txt输出如下所示:

PRO1,2,MEM1,MEM2,MEM1,MEM2,100,MEM2,MEM1,100,
PRO2,2,MEM3,MEM4,MEM3,MEM4,100,MEM4,MEM3,100,

但我希望.txt / csv输出看起来像:

PRO1,2,MEM1,MEM2,MEM1,MEM2,100,MEM2,MEM1,100
PRO2,2,MEM3,MEM4,MEM3,MEM4,100,MEM4,MEM3,100

我应该将扫描程序的try / catch语句移动到单独的方法吗?非常感谢你的帮助。

3 个答案:

答案 0 :(得分:1)

你可以读取txt文件,并且,对于每行重写为String,你可以使用String.subString方法取所有String减去最后一个字符:

for ( line ) {
    String newLine = line.subString(0, line.length());
    .
    .
    .
}

希望这对你有所帮助,我可以帮助你更多,但我现在没有时间,抱歉。

答案 1 :(得分:1)

您可以使用substring获取您要查找的字符串,我猜您可以从文本文件中逐行读取:

这是你获得所需字符串的方式:

String s = "PRO1,2,MEM1,MEM2,MEM1,MEM2,100,MEM2,MEM1,100,";

        s = s.substring(0, s.length()-1);

        System.out.println(s);

Output: PRO1,2,MEM1,MEM2,MEM1,MEM2,100,MEM2,MEM1,100

希望这会有所帮助。

答案 2 :(得分:0)

它可以使用commons文件utils。

首先,您可以将文件拆分为行。

List<String> lines = org.apache.commons.io.FileUtils.readLines(new File("file.txt"),"UTF-8");

现在,对于每一行,您可以获取字符串并检查角色出现的最后一个索引。

StringBuilder newFile = new StringBuilder();
    for(String line : lines){
        int endIndex = line.lastIndexOf(".");
        String newString = demo.substring(0, endIndex);
        //newString has the string without the last character.
        newFile.append(newString);
    }

org.apache.commons.io.FileUtils.writeStringToFile(new File("newFile.txt", newFile.toString(), "UTF-8");