的帮助你,我希望:
public static void readSplitWrite(String pathToFile){
//To recognize how are your sentences : between 1 and 25 digit, then some spaces, and then what you want
Pattern patt = Pattern.compile("([0-9]{1,25})\\s*(.*)");
try {
//Read over all the lines of your file
for(String line : Files.readAllLines(Paths.get(pathToFile),Charset.forName("UTF-8"))){
//Catch the different parts of the string (group(0) is the hole sentence, group(1) is inside first(), group(2) is inside second()
Matcher m = patt.matcher(line);
if(m.find()){
//Open a file, in UTF-8 encoding (to be able to read Arabic character, as name the ID
Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(m.group(1)+".txt"), "UTF-8"));
try {
//write the content into
out.write(m.group(2));
}catch (Exception e) {
e.printStackTrace();
} finally {
out.close();
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
//Just the name if the file is in the folder, if not write absolute path
String pathToFile = "Text.txt";
readSplitWrite(pathToFile);
}
此代码将阅读您的文件,然后对每行创建一个new File
,与姓名的数量,作为内容的句子 如果有任何问题,告诉我在评论
编辑:我评论更好的代码,以帮助您
参见[读,写和创建文件](https://docs.oracle.com/javase/tutorial/essential/ io/file.html)在Oracle的Java教程中。 – Jesper