﻿<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
    <CodeSnippet Format="1.0.0">
        <Header>
            <Title> Search Files for an Expression </Title>
            <Description> Expansion snippet to search file for an expression. </Description>
            <Author>Microsoft Corporation</Author>
            <SnippetTypes>          
                <SnippetType> Expansion </SnippetType>
            </SnippetTypes>
        </Header>
        <Snippet>
            <Declarations>
                <Literal>
                    <ID> FileName </ID>
                    <Default> strFileName </Default>
                    <ToolTip> Variable to hold the filename in which the expression has to be searched. </ToolTip>
                </Literal>
        <Literal>
                    <ID> Expression </ID>
                    <Default> strExpr </Default>
                    <ToolTip> Variable to hold the expression to be searched for in the file. </ToolTip>
                </Literal>
            </Declarations>
            <Code Language="vjsharp" Format="CData"><![CDATA[
boolean flFound = false;  //variable is set to true if search string is found in the file else false

try
{
    //check if the file name and search string are not null and not blank
    if (($FileName$ != null && $FileName$.length() > 0) &&
        ($Expression$ != null && $Expression$.length() > 0))
    {
        java.io.File objFile = new java.io.File($FileName$);
        if (objFile.exists())
        {
            java.io.BufferedReader objBuffStream = new java.io.BufferedReader(new java.io.FileReader($FileName$));
            String strLine;
            
            //read each line from the file
            while ((strLine = objBuffStream.readLine()) != null)
            {
                //Compare with the search string
                if (strLine.Contains($Expression$))
                {
                    System.out.println("The line : " + strLine);
                    flFound =  true;
                }
            }
            objBuffStream.close();
        }
    }
}
catch (java.io.IOException e)
{
    System.out.println("Error: Invalid IO operation");
}
$selected$ $end$
]]>
</Code>
        </Snippet>
    </CodeSnippet>
</CodeSnippets>
