package crx.mxtnx.diff.test; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; import java.util.Vector; public class FooFormatter { private static final String TAB = "\t\t\t"; private static final String fileSourceLocation = "C:/.../fichier_source.txt"; private static final String fileDestinationLocation = "C:/.../fichier_destination.txt"; /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub FooFormatter fooFormatter = new FooFormatter(); fooFormatter.formatFromFileToFile(fileSourceLocation, fileDestinationLocation); } public void formatFromFileToFile(String fileSrcLocation, String fileDestlocation) { List<List<String>> lineColumnsContents = formatFileContent(fileSrcLocation); try { BufferedWriter writer = new BufferedWriter(new FileWriter(fileDestlocation)); writeToFileDestination(writer, lineColumnsContents); writer.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void testExtractFile() { } private void writeToFileDestination(BufferedWriter writer, List<List<String>> lineColumnsContents) throws IOException { for (List<String> line : lineColumnsContents) { for (String column : line) writer.write(column + TAB); writer.newLine(); } } public List<List<String>> formatFileContent(String filePath) { // columnDelimiter: storing the index of all content delimiter ( | ) Vector<Integer> columnDelimiter = new Vector<Integer>(); // contentMatrix List<List<String>> contentMatrix = new ArrayList<List<String>>(); // final formated content List<List<String>> lineContents = new ArrayList<List<String>>(); BufferedReader reader = null; try { // read the source file reader = new BufferedReader(new InputStreamReader(new FileInputStream(fileSourceLocation))); String line = null; boolean endOfRowReached = false; int columnCount = 0; // for each line in source file while ((line = reader.readLine()) != null) { // check if the end of each row has been reached if (line.contains("---")) { endOfRowReached = true; } if (!endOfRowReached) { // while the end of row has not yet been reached int index = line.indexOf('|'); while (index >= 0) { // harvest all delimiter position in their order columnDelimiter.add(index); index = line.indexOf('|', index + 1); } if (columnDelimiter.size() > 0) { // harvest column content in each column limited by delimiter columnCount = columnDelimiter.size() - 1; List<String> rowContent = new ArrayList<String>(columnDelimiter.size() - 1); for (int i = 0; i < columnDelimiter.size() - 1; i++) { int j = i + 1; String columnContent = line.substring(columnDelimiter.get(i) + 1, columnDelimiter.get(j)).trim(); rowContent.add(columnContent); } if (rowContent.size() > 0) { contentMatrix.add(rowContent); } } // prepare for next delimiter context columnDelimiter.clear(); } else { // if end of Row has been reached List<String> tempListContent = new ArrayList<String>(); lineContents.add(tempListContent); for (int i = 0; i < columnCount; i++) { tempListContent.add(""); } for (List<String> tempRow : contentMatrix) { for (int i = 0; i < tempRow.size(); i++) { // transform/concatenate columns into line String tempColumn = tempListContent.get(i); tempColumn += tempRow.get(i); tempListContent.set(i, tempColumn); } } // empty matrix table contentMatrix.clear(); // reset toggle; endOfRowReached = false; } } } catch (FileNotFoundException e) { e.printStackTrace(); // please handle exception well } catch (IOException e) { e.printStackTrace(); } finally { if (reader != null) try { reader.close(); } catch (IOException e) { e.printStackTrace(); // please handle exception well } } return lineContents; } }
date de saisie intitulé complet du compte
16/10/2011 Capital social
16/10/2011 reserves facultatives et extraordinaires
16/10/2011 Autres emprunt a long terme aupres d'autres preteurs
611300 en EUR
16/10/2011 Capital social
16/10/2011 reserves facultatives et extraordinaires
16/10/2011 Autres emprunt a long terme aupres d'autres preteurs
public List<List<String>> formatFileContent_2(String filePath) { // columnDelimiter: storing the index of all content delimiter ( | ) Vector<Integer> columnDelimiter = new Vector<Integer>(); // contentMatrix List<List<String>> contentMatrix = new ArrayList<List<String>>(); // final formated content List<List<String>> lineContents = new ArrayList<List<String>>(); // header lines count int headerLineCount = 0; BufferedReader reader = null; try { // read the source file reader = new BufferedReader(new InputStreamReader(new FileInputStream(fileSourceLocation))); String line = null; boolean endOfRowReached = false; int columnCount = 0; // cursor header Start boolean headerStartReached = false; boolean headerEndReached = false; // for each line in source file // A- work on header while ((line = reader.readLine()) != null) { if (isHeaderDelimiterLine(line)) { // check on the header delimiters if (!headerStartReached) { headerStartReached = true; continue; } else { headerEndReached = true; headerStartReached = false; } } if (headerStartReached) { // while the end of row has not yet been reached int index = line.indexOf('|'); while (index >= 0) { // harvest all delimiter position in their order columnDelimiter.add(index); index = line.indexOf('|', index + 1); } if (columnDelimiter.size() > 0) { // harvest column content in each column limited by delimiter columnCount = columnDelimiter.size() - 1; List<String> rowContent = new ArrayList<String>(columnDelimiter.size() - 1); for (int i = 0; i < columnDelimiter.size() - 1; i++) { int j = i + 1; String columnContent = line.substring(columnDelimiter.get(i) + 1, columnDelimiter.get(j)).trim(); rowContent.add(columnContent); } if (rowContent.size() > 0) { contentMatrix.add(rowContent); headerLineCount++; } } // prepare for next delimiter context columnDelimiter.clear(); } if (headerEndReached) { // if end of header has been reached List<String> tempListContent = new ArrayList<String>(); lineContents.add(tempListContent); for (int i = 0; i < columnCount; i++) { tempListContent.add(""); } for (List<String> tempRow : contentMatrix) { for (int i = 0; i < tempRow.size(); i++) { // transform/concatenate columns into line String tempColumn = tempListContent.get(i); tempColumn += tempRow.get(i); tempListContent.set(i, tempColumn); } } // exit loop break; } } // B- work on row content while ((line = reader.readLine()) != null) { if (isRowDelimiterLine(line)) { // check if the end of each row has been reached endOfRowReached = true; } if (!endOfRowReached) { // while the end of row has not yet been reached int index = line.indexOf('|'); while (index >= 0) { // harvest all delimiter position in their order columnDelimiter.add(index); index = line.indexOf('|', index + 1); } if (columnDelimiter.size() > 0) { // harvest column content in each column limited by delimiter columnCount = columnDelimiter.size() - 1; List<String> rowContent = new ArrayList<String>(columnDelimiter.size() - 1); for (int i = 0; i < columnDelimiter.size() - 1; i++) { int j = i + 1; String columnContent = line.substring(columnDelimiter.get(i) + 1, columnDelimiter.get(j)).trim(); rowContent.add(columnContent); } if (rowContent.size() > 0) { contentMatrix.add(rowContent); } } // prepare for next delimiter context columnDelimiter.clear(); } else { // if end of Row has been reached List<String> newLine = null; // start transformation of rows after the all headers for (int j = headerLineCount; j < contentMatrix.size(); j++) { // add a new line and initialize it with blank value List<String> tempRow = contentMatrix.get(j); // if pivot line is NOT empty, create new line if (!isLineBlank(tempRow.get(PIVOT_LINE_INDEX))) { newLine = new ArrayList<String>(); lineContents.add(newLine); for (int i = 0; i < tempRow.size(); i++) { newLine.add(""); } } for (int i = 0; i < tempRow.size(); i++) { // transform/concatenate columns into line String tempColumn = null; if (isColumnContentToConcatenate(i)) { tempColumn = newLine.get(i); tempColumn += tempRow.get(i); } else { // only add non empty column values if (!isLineBlank(tempRow.get(PIVOT_LINE_INDEX))) tempColumn = tempRow.get(i); else continue; } newLine.set(i, tempColumn); } } if (headerLineCount != 0) headerLineCount = 0; // empty matrix table contentMatrix.clear(); // reset toggle; endOfRowReached = false; } } } catch (FileNotFoundException e) { e.printStackTrace(); // please handle exception well } catch (IOException e) { e.printStackTrace(); } finally { if (reader != null) try { reader.close(); } catch (IOException e) { e.printStackTrace(); // please handle exception well } } return lineContents; }
main(String[] args)
List<List<String>> lineColumnsContents = formatFileContent(fileSrcLocation);
List<List<String>> lineColumnsContents = formatFileContent_2(fileSrcLocation);
public static void main(String[] args) { String test="|101000 |Capital social | "; Matcher matcher = Pattern.compile("\\p{Space}*\\|\\p{Space}*([0-9]+)\\p{Space}*\\|\\p{Space}*([a-zA-Z ]+).*").matcher(test); while (matcher.find()) { System.out.println(matcher.group(1)+"\t"+matcher.group(2)); } }
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionlabandus
labandus
|++++++++++++|++++++++++++++| |date de |Intitulé compl| |saisie |et du compte | |++++++++++++|++++++++++++++| |16/10/2011 |Capital social| |16/10/2011 |Réserves facul| | |tatives et ext| | | raordinaires | |16/10/2011 |Autres emprunt| | |s a long terme| | |aupres d'autre| | |preteurs | |------------|--------------| |611300 en EUR | |------------|------------- | |16/10/2011 |Capital social| |16/10/2011 |Réserves facul| | |tatives et ext| | | raordinaires | |16/10/2011 |Autres emprunt| | |s a long terme| | |aupres d'autre| | |preteurs | |------------|--------------|
date de saisie intitulé complet du compte 16/10/2011 Capital social 16/10/2011 reserves facultatives et extraordinaires 16/10/2011 Autres emprunt a long terme aupres d'autres preteurs
|16/10/2011 |Capital social| |16/10/2011 |Réserves facul| | |tatives et ext| | | raordinaires | |16/10/2011 |Autres emprunt| | |s a long terme| | |aupres d'autre| | |preteurs |
|------------|--------------| |611300 en EUR | |------------|------------- |
j'aimerai insérer une condition dans le code qui concatène
tant que la ligne suivante commence par"| |"
"| |", non ?
date de saisie intitulé complet du compte 16/10/2011 Capital social 16/10/2011 reserves facultatives et extraordinaires 16/10/2011 Autres emprunt a long terme aupres d'autres preteurs 611300 en EUR 16/10/2011 Capital social 16/10/2011 reserves facultatives et extraordinaires 16/10/2011 Autres emprunt a long terme aupres d'autres preteurs
AVANT LE FORMATAGE
|+++++++|++++++++++++++|++++|++++++++++++++++++|++++++++++++++++++|++++++++++++++++++| |Num du |Intitulé compl|Dev | Sld Init Déb | Sld Init Créd | Mvts Débit | |compte | et du compte | | | | | |+++++++|++++++++++++++|++++|++++++++++++++++++|++++++++++++++++++|++++++++++++++++++| |101000 |Capital social|XAF | | 10 000 000 000| 0 | |-------|--------------|----|------------------|------------------|------------------| |121000 |Réserve légale|XAF | | 900 000 000| 0 | |-------|--------------|----|------------------|------------------|------------------| |123000 |Réserves facul|XAF | | 3 392 715 994| 0 | | |tatives et ext| | | | | | | raordinaires | | | | | |-------|--------------|----|------------------|------------------|------------------|
APRES LE FORMATAGE
Numducompte Intitulécompletducompte Dev SldInitDéb SldInitCréd MvtsDébit 101000 Capital social XAF 10000000000 0 0 121000 Réservelégale XAF 900000000 0 0 123000 Réservesfacul... XAF 3 392 715 994 0 0 0 0 0 3392715994 0 0
9 déc. 2017 à 03:38