我正在编写代码以在 java 中拆分行,但它无法正常工作。
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.lang.*;
public class mainprogram {
public static void main(String [] args ) throws FileNotFoundException, IOException
{
//creating object for purpose of writing into file
writFile ObjectwritFile = new writFile();
//creating object for purpose of reading from file
loadFile ObjectloadFile = new loadFile();
//this will be used for storing text into from file
String text="";
//text = ObjectloadFile.loadFile("C://names.txt");
System.out.println(text);
ObjectwritFile.writeFile("C://testfile.txt",text);
//these regexp didn't work
//String regexp = "[\\r\\n]+";
//String regexp = "[\\s,;\\n\\t]+";
String regexp = "[\\n]+";
//this one didn't work
//String[] lines = text.split(regexp);
String[] lines = text.split(System.getProperty("line.separator"));
for(int i=0; i<lines.length; i++){
System.out.println("Line "+i+": "+lines[i]);
ObjectwritFile.writeFile("C://testfilelines.txt","Line "+i+": "+lines[i]);
}
}
}
文本采用这种格式。
John, Smith, 4 ,5 ,6
Adams, Olsen, 7,8, 3
Steve, Tomphson , 4,5 9
Jake, Oliver, 9,8,9
一旦我按行分隔文本,我必须按字母和数字将其排序到具有相同文本格式的文件中。