Ich habe die Anforderung, 1 Datei in mehrere Dateien aufzuteilen. Die Datei ist vom Typ: 1 T tabelle1 "a, b, c, d, e, f" 2 W Tabelle1 "a, b, c, d, e, f" 3 D Tabelle1 "a, b , c, d, e, f " Ich möchte diese Datei in 3 Dateien mit Namenskonventionen teilen wie 1_T_table1, 2_W_table1 und 3_D_table1 Ich habe bereits Dateien in 3 Dateien aufgeteilt, aber mit naiven Namen. Ich möchte sie wie oben benennen. Kann mir jemand helfen, diese Dateien mit Namen :) Unten ist der Code:Regex in Java, um die ersten Zeichen der Zeile in der Datei abzuleiten und diese als Dateiname zu verwenden
Hinweis: text.txt ist die Datei
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.Object ;
import java.util.regex.Matcher ;
import java.util.regex.Pattern;
public class Test {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
String inputFile="C:\\path\\test.txt";
BufferedReader br = new BufferedReader(new FileReader(new File(inputFile)));
String line=null;
StringBuilder sb = new StringBuilder();
int count=1;
try {
while((line = br.readLine()) != null){
Matcher match = Pattern.compile("^[0-9]+ [TWD]").matcher(line);
while (match.find())
{
sb.append("split"+"\r\n ");
}
if(sb.length()!=0){
sb.append(line+"\r\n ");
}
}
int c = 0;
Pattern p = Pattern.compile("split");
Matcher m = p.matcher(sb.toString());
while (m.find()) {
c++;
}
//System.out.println(c);
int index = 0;
for(int i=0;i<=c ;i++)
{
if(sb.length() > 0 && sb.toString().contains("split")){
File file = new File("C:\\path\\DOC_ID_"+i+".txt");
PrintWriter writer = new PrintWriter(file, "UTF-8");
index = sb.toString().indexOf("split",2);
//System.out.println(index);
if(index>0)
{
writer.println(sb.toString().substring(7,index));
sb.delete(0, index);
}
else
{
writer.println(sb.toString().substring(7,sb.length()));
sb.delete(0, sb.length());
}
writer.close();
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
finally {
br.close();
}
}
}
dank Piyush zur Bearbeitung. –
ich bekomme dich nicht, aber willst du etwas wie ** [this] (https://regex101.com/r/wG7jA8/1) **? – rock321987
das ist genau das, was ich will .dank –