Java 删除.txt文件每行中的最后一个字符
我是Java的初学者。我已经使用循环成功地将用户数据输入导出到.txt文件中,但我想删除文件每行的最后一个逗号。我尝试使用分隔符,但无法让replaceAll成功删除每行的最后一个逗号 我当前导出数据的方法是:Java 删除.txt文件每行中的最后一个字符,java,file,csv,char,Java,File,Csv,Char,我是Java的初学者。我已经使用循环成功地将用户数据输入导出到.txt文件中,但我想删除文件每行的最后一个逗号。我尝试使用分隔符,但无法让replaceAll成功删除每行的最后一个逗号 我当前导出数据的方法是: public void Quit() { System.out.println(ProjectList.get(0).ProjectName); File fileObj
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/csv输出如下所示:
PRO1,2,MEM1,MEM2,MEM1,MEM2,100,MEM2,MEM1,100
PRO2,2,MEM3,MEM4,MEM3,MEM4,100,MEM4,MEM3,100
我是否应该将扫描仪的try/catch语句移动到单独的方法?非常感谢您的帮助。您可以读取txt文件,对于读取为字符串的每一行,您可以使用String.subString方法获取所有字符串减去最后一个字符:
for ( line ) {
String newLine = line.subString(0, line.length());
.
.
.
}
希望这对您有所帮助,我可以帮助您更多,但我现在没有时间,对不起。您可以使用
子字符串来获取您要查找的字符串,我猜您可以从文本文件中逐行读取:
这是获取所需字符串的方法:
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
希望这会有所帮助。一种方法是使用commons文件utils
首先,您可以将文件拆分为行
List<String> lines = org.apache.commons.io.FileUtils.readLines(new File("file.txt"),"UTF-8");
如果在第一个循环中使用if语句,在最后一列中不加逗号,如何?您好,我确实尝试过,但遇到了一个问题,因为它是数组列表中的二维数组,所以如果我没有弄错的话,逗号将被删除两次。我只希望在每行末尾删除一次逗号更重要的是,在替换逗号后,您对数据字符串什么都不做,好的,所以我应该肯定地重新打印数据字符串您可以用逗号加入集合。。。若将实际长度放入“endIndex”值中,那个么它不会对字符串进行任何更改。它将保持不变。这将为此任务生成一个额外的循环,并且我们可以从上一个索引中删除“,”,在同一个循环中,我们迭代字符串中的所有文本行。好的,谢谢您的帮助!我是否需要在单独的方法中使用PrintWriter类来导出新字符串,而不是println?这取决于您,如果读取文件的方法和逻辑不是太复杂,那么您可以在那里自己编写导出逻辑,但是的,最好的方法是将每件事情分开,所以特定的方法会很好。
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");