0

I am using a function to open a first file and to copy the content to another one with some changes. First I look for a symbole that I want to do some changes before it, my problem is that the function is looking for all the same symbole and make changes before all of them. I want from the function to only look for the first occurance of the symbole and to make changes. here is the code:

public static void Trouver(String theInputFileName, String theOutputFileName, String theSymbol, String theExpression ){

    int m_IndexDebut = -1;
    String m_TmpStr  = null;
    String m_OutputString   = "";
    boolean m_found = false;

    RandomAccessFile m_InputFile = null;
    RandomAccessFile m_OutputFile = null;
    String m_Ligne = null;
    String m_xref = "xref";
    try{
        // Ouverture du fichier
        m_InputFile = new RandomAccessFile(theInputFileName, "r");
        m_OutputFile = new RandomAccessFile(theOutputFileName, "rw");

        // Lecture Ligne par Ligne
        while (  (m_Ligne = m_InputFile.readLine() ) != m_xref ){

            // Initialisation de l'index pour une nouvelle ligne
            m_IndexDebut = -1;
            m_found = false;
            m_OutputString = "";
            m_TmpStr = m_Ligne;

            // On Cherche le "symbole" et on ajoute ce qu'on à ajouter
            while ( m_TmpStr != theSymbol && (m_IndexDebut = m_TmpStr.indexOf(theSymbol)) != -1 ){

                m_found = true;
                m_OutputString = m_TmpStr.substring(0, m_IndexDebut).concat(theExpression).concat("\n").concat( theSymbol );
                m_TmpStr = m_TmpStr.substring(m_IndexDebut + theSymbol.length() );

            }

            // Ajout de la fin de la ligne


            if ( m_found )
                m_OutputString = m_OutputString.concat("\n");
            else
                // Si aucun symbole trouvé ,Réecriture de la ligne
                m_OutputString = m_Ligne.concat("\n");

            // Ajout de cette ligne au fichier sortant
            m_OutputFile.write( m_OutputString.getBytes());
        }

    }catch (IOException e){
        System.err.println("IOException : "+e.getMessage());
    }finally {
        // Fermeture des fichiers
        try{
            m_InputFile.close();
            m_OutputFile.close();
        }catch ( Exception e ){

        }
    }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
  • 2
    `(m_Ligne = m_InputFile.readLine() ) != m_xref` --> Don't compare strings with `==` or `!=`: https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java – assylias Oct 12 '17 at 10:04
  • i had to change while `(m_TmpStr != theSymbol)` to `equals(theSymbol)`. Thank you for your answer ! – Qossai Labyad Oct 12 '17 at 12:46

0 Answers0