How do i search for a string in a txt file using java??

How do i search for a string in a txt file using java??
could you please help thanks
J

Regular expressinos work just fine, especially when
searching for patterns. But they seem to be impying
it's a specific group of characters they're looking
for, and indexOf() is much faster than a regex.If he's reading from a file, the I/O time will likely swamp any performance hit that regex introduces. I think contains() (or indexOf() if he's not on 5.0 yet) is preferable to regex just because it's simpler. (And in the case of contains(), the name makes for a very clear, direct mapping between your intent and the code that realizes it.)

Similar Messages

  • Searching for a string in an excel file and return true if found.

    How can I search for a string in an excel file and return true if found, its location and also a value.

    The problem with searching an excel file is that it contains a header. It would be easiest if you just read the file into a LabVIEW array and search the array. This way you can also get the index and the value of the location.

  • How to read and write a string into a txt.file

    Hi, I am now using BEA Workshop for Weblogic Platform version10. I am using J2EE is my programming language. The problem I encounter is as the above title; how to read and write a string into a txt.file with a specific root directory? Do you have any sample codes to reference?
    I hope someone can answer my question as soon as possible
    Thank you very much.

    Accessing the file system directly from a web app is a bad idea for several reasons. See http://weblogs.java.net/blog/simongbrown/archive/2003/10/file_access_in.html for a great discussion of the topic.
    On Weblogic there seems to be two ways to access files. First, use a File T3 connector from the console. Second, use java.net.URL with the file: protocol. The T3File object has been deprecated and suggests:
    Deprecated in WebLogic Server 6.1. Use java.net.URL.openConnection() instead.
    Edited by: m0smith on Mar 12, 2008 5:18 PM

  • How can I search for a string like a partial IP address in the global search

    When searching for a string like "10.255." the global search finds messages containing "255" or "10" and both.
    This happens no matter whether quotation is used or not.
    When quotation is used it probably will match on the given order as well, but beeing in the timezone '+0100' the string "10" is always found first (in the email headers...).
    Is there a way to tell the global search not to use '.' or any other character as whitespace?

    When searching for a string like "10.255." the global search finds messages containing "255" or "10" and both.
    This happens no matter whether quotation is used or not.
    When quotation is used it probably will match on the given order as well, but beeing in the timezone '+0100' the string "10" is always found first (in the email headers...).
    Is there a way to tell the global search not to use '.' or any other character as whitespace?

  • Replace the text numbers string in a txt file using C++.. Help Me..

    Read a Document and replace the text numbers in a txt file using c++..
    For ex: 
    Before Document: 
    hai hello my daily salary is two thousand and five and your salary is five billion. my age is 
    twenty-five. 
    After Document: 
    hai hello my daily salary is # and your salary is #. my age is #. 
    All the text numbers and i put the # symbol.. 
    I am trying this code: 
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    ifstream myfile_in ("input.txt");
    ofstream myfile_out ("output.txt");
    string line;
    void find_and_replace( string &source, string find, string replace ) {
    size_t j;
    for ( ; (j = source.find( find )) != string::npos ; ) {
    source.replace( j, find.length(), replace );
    myfile_out << source <<endl;
    cout << source << endl;
    int main () {
    if (myfile_in.is_open())
    int i = 0,j;
    //string strcomma ;
    // string strspace ;
    while (! myfile_in.eof() )
    getline (myfile_in,line);
    string strcomma= "two";
    string strspace = "#";
    find_and_replace( line , strcomma , strspace );
    i++;
    myfile_in.close();
    else cout << "Unable to open file(s) ";
    system("PAUSE");
    return 0;
    Please help me.. Give me the correct code..

    Open the file as a RandomAccessFile. Check its length. Declare a byte array as big as its length and do a single read to get the file into RAM.
    Is this a simple text file (bytes)? No problem. If it's really 16-bit chars, use java.nio to first wrap the byte array as a ByteBuffer and then view the ByteBuffer as a CharBuffer.
    Then you're ready for search/replace. Do it as you would in any other language. Be sure to use System.arraycopy() to shove your bytes right (replace bigger than search) or left (replace smaller than search).
    When done, a single write() to the RandomAccessFile will put it all back. As you search/replace, keep track of size. If the final file is smaller than the original, use a setLength() to the new size to avoid extraneous data at the end.

  • Code for reading the and placing the file using java webservice

    Hi All,
    Can anybody can guide me on how to read a file and place the file using java.
    Let  me know if any code is available which has been completed using java.
    Regards,
    Rahul

    Hi,
    The "square" symbol that you are referring to is probably a CRLF (Carriage Return - Life Feed) Control Character.
    This is more commonly known as a "Enter" at the end of a line / sentence.
    To clean this character from strings in Java, please use:
    String patternStr = "(?m)$^|[\\r\\n]+\\z";
    String replaceStr = " ";
    Pattern pattern = Pattern.compile(patternStr);
    Matcher matcher = pattern.matcher(inputStr);
    return matcher.replaceAll(replaceStr);
    Hope that helps.
    Thanks.
    p256960.

  • How do I search for a string in the Text tab of the Layers panel

    I have a world map with a text layer for country names.  I hide the country names as needed by clicking on the 'eye' icon for Toggle Visibility in the Layers panel.  I've inadvertently hidden one name and I need to know how to find it in the Text panel so I can make it visible again.  How do I seatrch for a name in the Text panel?

    Just in case there is an unlocked and visible object close to the hidden one in the stacking order, you may select that object right on the artboard and then go to the Layers palette menu and take the Locate Object command.
    Of course, this would be just one possible workaround to locate the hidden objects a bit easier.

  • Searching for a string in given text file

    Hi,
    I have two strings and i need to search the entire file for the strings and highlight the lines between the strings
    say, for example,
    hi this is xyz
    i am a begineer in java
    can i have some help in this issue please
    i gave 'hi' and 'can' i need the three lines to be highlighted
    can you give me an idea about this.
    how to proceed
    thanks all in advance
    Edited by: anub on Feb 6, 2009 3:24 AM

    Here's an idea. Find a link to the java API. Create a utility class with a method to read a file and a method to write a file. Look in java.io for this. That should keep you busy for a while and give you something specific to ask questions about. Next, create a GUI class and put a button on it. Figure out how to make a button click open a "FileChooser" or "JFileChooser" (Look in the api for that) Imagine that these need to talk to each other so figure out what parameters are needed. After you've done that then figure out how the user is going to enter the two search criteria (something that acts like a text field). Choose a GUI class that supports text highlighting. Display all the information that the user would like to know e.g. The name of the file selected to parse, the name of the file to write, and whether the strings were found successfully.
    kr
    walker

  • How do I search for a specific word in the code using Dreamweaver?

    I need to search for a specific word in the code for all of my website pages. How can I do that using Dreamweaver?

    To search local files of a Defined Site in DW...
    Open the Find & Replace tool with Ctrl + F
    Change the Find In dropdown to Entire Current Local Site
    Change the Search dropdown to Text or Source code
    Add the text to find in the Find field
    Add the text to replace in the Replace field (or nothing if you just want to delete the text)
    Hit Replace All

  • How can I search for a string, as an input parameter to a procedure?

    Hi all,
    I need to solve one scenario. In this the search string and search against columns, both are passed as input parameters to a procedure. till now i didn't face this situation.. can you please help me in this by giving some sample code...
    Thanks in advance.
    Regards,
    Ramu.

    Maybe something like this then?
    SQL> CREATE OR REPLACE PROCEDURE EMP_SEARCH
      2  (
      3          pColumnName     IN VARCHAR2
      4  ,       pSearchParam    IN VARCHAR2
      5  ,       pResult         OUT SYS_REFCURSOR
      6  )
      7  AS
      8  BEGIN
      9          IF UPPER(pColumnName) = 'ENAME' THEN
    10                  OPEN pResult FOR
    11                          SELECT  ENAME
    12                          FROM    SCOTT.EMP
    13                          WHERE   ENAME LIKE '%' || pSearchParam || '%';
    14          ELSIF UPPER(pColumnName) = 'JOB' THEN
    15                  OPEN pResult FOR
    16                          SELECT  ENAME
    17                          FROM    SCOTT.EMP
    18                          WHERE   JOB LIKE '%' || pSearchParam || '%';
    19          ELSIF UPPER(pColumnName) = 'DEPTNO' THEN
    20                  OPEN pResult FOR
    21                          SELECT  ENAME
    22                          FROM    SCOTT.EMP
    23                          WHERE   DEPTNO LIKE '%' || pSearchParam || '%'
    24          END IF;
    25  END;
    26  /
    SP2-0804: Procedure created with compilation warnings
    SQL> VAR C REFCURSOR
    SQL> EXEC EMP_SEARCH('ENAME','SMITH',:c);
    PL/SQL procedure successfully completed.
    SQL> PRINT c;
    ENAME
    SMITH
    SQL> EXEC EMP_SEARCH('JOB','MANAGER',:c);
    PL/SQL procedure successfully completed.
    SQL> print c;
    ENAME
    JONES
    BLAKE
    CLARK
    SQL> EXEC EMP_SEARCH('DEPTNO','30',:c);
    PL/SQL procedure successfully completed.
    SQL> print c
    ENAME
    ALLEN
    WARD
    MARTIN
    BLAKE
    TURNER
    JAMES
    6 rows selected.Be careful though. If you have a large amount of data this search could take a long time because Oracle will not choose an indexed path. If that's an issue you should investigate using Oracle Text.
    HTH!
    Edited by: Centinul on Apr 26, 2010 7:36 AM

  • How do I search for a value in an Excel sheet using ActixeX

    I am already accessing my excel sheets however I need to be able to search the sheet for a certain value and return the row/cell location. Is there an activeX control that can do this?

    You will need a reference to the range to search. This can be the entire sheet if you wish. Just wire that to the method node Find. Here you can specify all of the typical search options. Wire your string or number into the What input. For the what to LOOKAT value, use -4163 for Value and -4123 for formulas.
    The reference returned is the range of the found value. You can wire it into the property node Row and Column to get the location.
    Michael Munroe
    www.abcdef.biz
    Michael Munroe, ABCDEF
    Certified LabVIEW Developer, MCP
    Find and fix bad VI Properties with Property Inspector

  • How do I search for a list of all AVI files on my Mac?

    I need to find all of the AVI files on my MacPro's 4 hard drives.
    How do I generate a list of all of the AVI files on these drives, their locations/size etc.??

    Hi Hassiman,
    I'd get EasyFind...
    http://www.versiontracker.com/dyn/moreinfo/macosx/8707
    It's free, just type in .avi, search all volumes & wait.

  • How to Write in MS Word's plate to a file using java I/O classes?

    hi,
    are there any ways that can write text files in the form of certain pattern plate of MS Word?

    hi,
    are there any ways that can write text files in the
    form of certain pattern plate of MS Word?What about your duke dollars?

  • Search in txt file using PL/SQL

    Is there any way to search for a string in a text file using a PL/SQL block??
    Thnks in advance.
    Ashish

    Richard:
    It would depend on the nature of the text file, but it could be done for most plain text files. If there is some consistent delimiter, that is, the text file is fielded in some way, you could define an external table with the appropriate structure then use normal SQL queries against the particular fields from the file.
    If the file is just plain text with no fields, you could create an external table something like:
    CREATE TABLE myexternal (
       line VARCHAR2(4000),
    ORGANIZATION EXTERNAL
    (TYPE oracle_loader
    DEFAULT DIRECTORY mydirectory
    ACCESS PARAMETERS
    (RECORDS DELIMITED BY newline
      BADFILE 'textfile.bad'
      DISCARDFILE 'textfile.dis'
      LOGFILE 'textfile.log'
      (line CHAR(4000) NULLIF line = blanks))
        LOCATION ('textfile.txt'))
    REJECT LIMIT UNLIMITEDThen use normal sql to search the contents of that file.
    I have not done any benchmarks on this, but my gut feel is that it would be significantly faster than using utl_file to loop over the lines looking for specific content.
    In one of our warehouse load programs, we process a text file that is typically about 7Mb in under 30 seconds.
    John

  • How do I search for a particular song on iTunes ?

    How do I search for a particular song on itunes - it used to be easy to go to search but that function seems to have disappeared

    The search function is still very much there, upper right corner of the iTunes Window.

Maybe you are looking for

  • Problem opening email documents

    When I try to open a document in my email I get the message: "Windows cannot access the specified device, path or file. You may not have the appropriate permissions to access the item".  I also get this message trying to download Adobe Reader 9. How

  • Can't Open Files with Double Click??

    HI I am on 10.8.3 Mountain Lion and when using the trackpad or my wireless bluetooth mouse, I cannot open files by double clicking all of a sudden.  This is a serious problem as I cannot view my video files in Final Cut Pro.   There is no click speed

  • Is there a way to tell Lightroom where to put Mobile Uploads from Lightroom Mobile app?

    Thanks!

  • Insert data by plsql

    hi, we have the table dep. SQL> select * from dep; DEPTNO DNAME LOC 10 ACCOUNTING NEW YORK 20 RESEARCH DALLAS 30 SALES CHICAGO 40 OPERATIONS BOSTON create sequence se_dep start with 1 increment by 1; I would like to insert the data into that table th

  • SQL Compilation in Logical APPS

    Hi I want to restrict the length of supplier addres in vendor form to 35 using Logical APPS. I had given the form, block and the when validate record event and given the condition in the SQL tab. I had compiled the SQL. But while using the vendor for