A better way to search a string in a 3GB txt file

I am looking for a string(12 characters) in a 3GB txt file. I use the file reader to go though each line and use the startwith() (that 12 characters are from the beginning of each line) to check if that line contain the string I am searching for. Is there a faster way to do it? Thank's.

I would avoid using java to parse/search a 3 GB text file at all costs. Any java solution is going to have terrible performance and you may run into memory problems. I would recomend that you either a) develop a native method for resolving this search and just invoke it from your current java class or b) use Runtime.exec to invoke a native OS command for searching the file (like grep or find) and parse the results. ....
I'm not even sure how I would complete that task with java exclusivly ... maybe break the file into smaller parts, then iterate through each smaller, temporary file ... I would really recomend storing your data in a different format if possible. A 3 GB text file just isn't very partical or secure.
... oh, to answer your original question (which I'm not sure if I all ready did), the fatest way to load the file is to wrap it into some BufferedReader. The fatest way to perform String searches is through the java.util.regex package if you have access to the 1.4 API. Other wise, just use basic String searches, as you all ready are.

Similar Messages

  • Urgent: deleting a specific string form an existing .txt file

    Plz help me with a sample code on how to delete a specific string form an existing .txt file..it is very urgent...
    thanks in advance

    String path = "D:\\text.txt";
    File file = new File(path);
    BufferedReader br = new BufferedReader(new FileReader(file));
    String line = "";
    String text = "";
    while((line = br.readLine()) != null) {
    text += line + "\n";
    br.close();
    String textToFind = "find";
    int start = text.indexOf(textToFind);
    if(start != -1) {
    text = text.substring(0, start) + text.substring(start + textToFind.length());
    BufferedWriter bw = new BufferedWriter(new FileWriter(file));
    bw.write(text, 0, text.length());
    bw.close();
    }

  • Is there any way to send output in BlueJ to a .txt file?

    I want to send the output of my BlueJ program to a .txt file. Is there any command to do it? If yes, then please tell me the syntax of that command along with an example using it?

    Again, you're just misunderstanding the tools you're using. Java should do that, not BlueJ. You have to research this from the perspective of writing Java code to do it. If you search how to make BlueJ do it, you won't find it because BlueJ has nothing to do with it.
    So you know your basic requirements.
    - you use System.out
    - you want that to go to a file in stead of to the command prompt / BlueJ console
    Do you know how to work with files yet? If not, that would be something you need to learn right now. And after that, try a Google for "java System.out file" and see what you get.

  • Is there a better way to re-render audio waveforms (after changing media files)?

    I'm shooting documentary footage on RED cameras at 6K resolution.  That generates a large amount of media.  The RDC format records R3D media files in 4GB chunks, which is actually relatively short compared to my hours-long media captures.  I've discovered that it's very handy to simply delete the R3D files from the RDC folder in cases where nothing is actually happening at that moment.  Since each R3D media file is timecode stamped, these actually stay in sync with my master audio track.  And Premiere Pro sensibly places gray diagonal bars where the underlying R3D files are missing.  All good.
    What's not good is that Premiere Pro confuses itself about the starting point of the PEK files.  Instead of simply greying out the part of the audio waveforms that are missing due to the missing media, it shifts the *waveform* to start at the location of the first R3D clip that is present, which is wrong.  If I clean the cache, quit the project, restart the project, and let it rebuild the cache, the audio waveform is correctly drawn.
    An ideal fix for this problem would be for Premiere Pro to recognize that if it re-links media it should re-render the audio PEK file.  An OK work-around would be to make it easy to just re-render the PEK file for a given media clip.

    A slightly quicker way to do what you described for an images is to select 'Duplicate version' instead of 'New version from Master' and then 'Remove adjustments' from version 1. This will leave you with the first version as the 'before' and the second version as the 'after' without having to use L&S.
    If you need to do this for 108 images, it might be quicker to do the following:
    Export Masters for all 108 images.
    Reimport these into a new project (called 'before', for example)
    You will then have a new project with the 'before' images and your original project with the 'after' images. You could then build an album to group all of these together. When you've finished with the 'before' & 'after' images (build your web gallery or emailed them or whatever), simply delete the 'before' project.
    Regards,
    Steve
    Message was edited by: Stephen Ives

  • Parsing string out of a txt file

    i cannot figure out how to do this, nothing i try am working... here is the data in my file:
                                  ["Zerk"] = {
                                       22, -- [1]
                                       "", -- [2]
                                       "2008-01-13", -- [3]
                                  ["Jaxa"] = {
                                       70, -- [1]
                                       "Apathy", -- [2]
                                       "2008-01-17", -- [3]
                                  ["Sujong"] = {
                                       70, -- [1]
                                       "IRONY", -- [2]
                                       "2008-01-12", -- [3]
                                  ["Henxins"] = {
                                       10, -- [1]
                                       "", -- [2]
                                       "2008-01-13", -- [3]
    i am trying to get the name out of the line like ["name here"], and the file is 2 mb so i cannot do it by hand, so i was trying to write a java program to do it for me, but it is not wanting to work :( any help appreciated... i don't care how its coded, nor efficienty.

    i solved it after messing around with it a little more
    import java.applet.Applet;
    import java.awt.Graphics;
    import java.util.Arrays;
    import java.util.List;
    import java.io.*;
    import java.util.ArrayList;
    public final class nameLeacher extends Applet {
         public String[] users;
         public int countLines() {
              int number = 1;
              try {
                   FileReader input = new FileReader("c:/unform.txt");
                   BufferedReader bufRead = new BufferedReader(input);
                   String line; // String that holds current file line
                   // Read first line
                   line = bufRead.readLine();
                   for (int i = 0; line != null; i++) {
                        line = bufRead.readLine();
                        number = i;
                   bufRead.close();
              } catch (ArrayIndexOutOfBoundsException e) {
              } catch (IOException e) {
                   e.printStackTrace();
              return number;
         public void load() {
              users = new String[countLines()];
              try {
                   FileReader input = new FileReader("c:/unform.txt");
                   BufferedReader bufRead = new BufferedReader(input);
                   String line; // String that holds current file line
                   // Read first line
                   line = bufRead.readLine();
                   for (int i = 0; line != null; i++) {
                        line = bufRead.readLine();
                        try {
                             if(line.contains("[\"")) {
                                  users[i] = line.trim().replace("[","").replace("]","").replace("\"","").replace("=","").replace("{","");
                        } catch(NullPointerException e) {
                   bufRead.close();
              } catch (ArrayIndexOutOfBoundsException e) {
              } catch (IOException e) {
                   e.printStackTrace();
         public void paint(Graphics g) {
              load();
              String[] remove = {null};
              List list = new ArrayList(Arrays.asList(users));
              list.removeAll(new ArrayList(Arrays.asList(remove)));
              users = (String[]) list.toArray(new String[list.size()]);
              for(int i = 0; i < users.length; i++) {
                   System.out.println(users);
         System.exit(1);
    }Edited by: bobert5696 on Jan 20, 2008 11:03 AM                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

  • Looking for a better way to determine string variable from multiple options

    Hi,
    I trying to figure out a better way to determine a string variable from multiple options.
    Say i have five pictures each with a different filename: img1 - img5...these file names could be named anything really but for this example i will keep them as img1, img2, img3, img4 and img5.
    I want to display a messagebox with the string depending on what a certain variable is.
    So for example, we have the number X, if X = 1 then i want the messagebox to show "img1" as the message
    Essentially the way I have been doing it so far is:
    Private Sub WhichImage()
    Dim ImageName As String = ""
    Dim i as integer
    If i = 0 Then
    ImageName = "img1"
    End If
    If i = 1 Then
    ImageName = "img2"
    End If
    If i = 2 Then
    ImageName = "img3"
    End If
    If i = 3 Then
    ImageName = "img4"
    End If
    If i = 4 Then
    ImageName = "img5"
    End If
    MessageBox.show(imagename, "Name of image", MsgBox.Style.OkOnly, MsgBoxResult.Ok)
    end
    Up until now, this has been fine, but what if I have 50 images, do I have to do this for all 50 images? or is there an easier way like putting the image names into a text file and have it read from the file depending on what the variable i equals? If so,
    how do I go about this? Does each image name go on a separate line? can it just be separated by a comma instead? or is there a better way?
    Please note that i know that i have declared "i" above in my code and not intialised it with anything, in reality "i" comes from somewhere else in the program so please ignore that part, it is not what I am concerned with.
    Thanks
    Mersec

    Does each image name go on a separate line? can it just be separated by a comma instead? or is there a better way?
    Arrays are useful for this.
    Dim imagenames() As String = {"img1", "img2", "img3", "img4", "img5"}
    Dim imagename As String = imagenames(i)
    MessageBox.Show(imagename, "Name of image")
    Any sort of collection will do instead of an array, and may be simpler to manage. There are many other options - the most suitable one probably depends on where the names originally come from.  For instance, if you are getting them from a folder
    using the FileSystem.GetFiles method, then they are already in a collection.
    If the files names never change then you may as well include them in the program code, using something like the code above.  If they can change, then you could use a text file, but that means you need a file update routine.  If that is required
    then the way you store the names will dictate how you access them.

  • Is it possible to search for strings containing spaces and special characters?

    In our RoboHelp project, there are figures with text labels such as Figure 1, Figure 3-2, etc.
    When I search for "Figure 3" I get all pages containing "Figure" and "3", even if I surround it in quotes.  Similarly, searching for "3-2" treats the '-' character as a space and searches for all pages containing '3' or '2'.
    Is there a way to search for strings containing spaces and special characters?

    In that case I think the answer is no if you are using the standard search engine. However I believe that Zoom Search does allow this type of searching. Check out this link for further information.
    http://www.grainge.org/pages/authoring/zoomsearch/zoomsearch.htm
      The RoboColum(n)
      @robocolumn
      Colum McAndrew

  • When I share a file to YouTube, where does the output file live? I want also to make a DVD. And is this a best practice or is there a better way?

    I want also to make a DVD, but can't see where the .mov files are.
    And is this a best practice or is there a better way to do this, such as with a master file?
    thanks,
    /john

    I would export to a file saved on your drive as h.264, same frame size. Then import that into youtube.
    I have never used FCP X to make a DVD but I assume that it will build the needed vob mpeg 2 source material for the disk.
      I used to use Toast & IDVD. Toast is great.
    To "see" the files created by FCP 10.1.1 for YouTube, rt. (control) click on the Library Icon in your Movies/show package contents/"project"/share.

  • Increasing the speed of searching a string in a file

    I have to search a String in a big ASCII-file and have to delete it. Now I wrote a program which opens the source file and a target file. I am reading each line from the source file, check it and write it into the target file. It is working fine but it is very slowly.
    I check each line with:
    if (line.equalsIgnoreCase("xxxxx")) { }
    How can I make it much faster.
    Thank you for your help!
    Arthur

    IO is an issue, but I've noticed that changing the case of strings is very slow, probably because in unicode is harder than in ASCII.
    If you could replace the equalsIgnoreCase method by one or more equals, you'll get benefits. If you can, I would try something like the following to minimize the equalsIgnoreCase invocations:
    // Supose you are looking for "abcd"
    if (line.charAt(0) == 'a' || line.charAt(0) == 'A') {
       if (line.equals("xxxxx")) { }
    }Try it and let us know if it worth the effort.

  • The most efficient way to search a large String

    Hi All,
    2 Quick Questions
    QUESTION 1:
    I have about 50 String keywords -- I would like to use to search a big String object (between 300-3000 characters)
    Is the most efficient way to search it for my keywords like this ?
    if(myBigString.indexOf("string1")!=1 || myBigString.indexOf("string2")!=1 || myBigString.indexOf("string1")!=1 and so on for 50 strings.)
    System.out.println("it was found");
    QUESTION 2:
    Can someone help me out with a regular expression search of phone number in the format NNN-NNN-NNNN
    I would like it to return all instances of that pattern found on the page .
    I have done regular expressions, in javascript in vbscript but I have never done regular expressions in java.
    Thanks

    Answer 2:
    If you have the option of using Java 1.4, have a look at the new regular expressions library... whose package name I forget :-/ There have been articles published on it, both at JavaWorld and IBM's developerWorks.
    If you can't use Java 1.4, have a look at the jakarta regular expression projects, of which I think there are two (ORO and Perl-like, off the top of my head)
    http://jakarta.apache.org/
    Answer 1:
    If you have n search terms, and are searching through a string of length l (the haystack, as in looking for a needle in a haystack), then searching for each term in turn will take time O(n*l). In particular, it will take longer the more terms you add (in a linear fashion, assuming the haystack stays the same length)
    If this is sufficient, then do it! The simplest solution is (almost) always the easiest to maintain.
    An alternative is to create a finite state machine that defines the search terms (Or multiple parallel finite state machines would probably be easier). You can then loop over the haystack string a single time to find every search term at once. Such an algorithm will take O(n*k) time to construct the finite state information (given an average search term length of k), and then O(l) for the search. For a large number of search terms, or a very large search string, this method will be faster than the naive method.
    One example of a state-search for strings is the Boyer-Moore algorithm.
    http://www-igm.univ-mlv.fr/~lecroq/string/tunedbm.html
    Regards, and have fun,
    -Troy

  • Search for master page usage; better way to use master pages?

    Hi all,
    I am using Frame 9. I have my files set to always have an even number of pages. For chapters where the text ends on a right-hand page, I assign a custom master page to the final left-hand page -- this custom master page has our logo on it. However, because my files have a lot of conditional text (3 different versions of the publication  get generated from the same source files), the lengths of the chapters often change when switching between different conditional versions. So for example, for version 1, I assign the custom master page to the last page of the chapter, which is page 28. Then when I switch my conditional text settings to show version 2, the chapter is now 36 pages long, but my custom master page is still applied to page 28. I have to remove it from page 28 and reassign it to page 38. Here are my questions:
    1) Is there a way to search for pages that have a specific master page applied? I hoped that I could generate a list of master page usage using the Special > List Of > References option, but apparently not.
    2) Is there any way to get Frame to automatically assign my custom master page when it is appropriate, rather than doing it by hand for each conditional version of each chapter?
    Thanks!

    kelly.usa wrote:
    Hi all,
    I am using Frame 9. I have my files set to always have an even number of pages. For chapters where the text ends on a right-hand page, I assign a custom master page to the final left-hand page -- this custom master page has our logo on it. However, because my files have a lot of conditional text (3 different versions of the publication  get generated from the same source files), the lengths of the chapters often change when switching between different conditional versions. So for example, for version 1, I assign the custom master page to the last page of the chapter, which is page 28. Then when I switch my conditional text settings to show version 2, the chapter is now 36 pages long, but my custom master page is still applied to page 28. I have to remove it from page 28 and reassign it to page 38. Here are my questions:
    1) Is there a way to search for pages that have a specific master page applied? I hoped that I could generate a list of master page usage using the Special > List Of > References option, but apparently not.
    2) Is there any way to get Frame to automatically assign my custom master page when it is appropriate, rather than doing it by hand for each conditional version of each chapter?
    Thanks!
    To identify master pages that control pages in body page view, create an identifier on each master page, as you'd create a watermark with text in a background text frame. Control its visibility with either a condition, or by assigning it a color that's invisible in a color view.
    HTH
    Regards,
    Peter
    Peter Gold
    KnowHow ProServices

  • Any way to search for casts in java source code?

    Anyone know of a decent way to search your java source files to find all the casts?
    I ended up doing a simple text search for
         " = ("(quote marks not included) which works for my code because I am strict about writing my casts with spaces like
         String s = (String) iterator.next();Unfortunately, the above search has all kinds of problems with both false positives and negatives. It picks up lots of irrelevant lines like
         int index = (number > 0) ? 0 : 1;as well as misses casts that appear nested inside expressions like
         ((String) iter.next()).charAt(...)I suppose that one could do a regular expression search for any pair of open and close parens which bound non-blank text, but that would pick up even more non-cast expressions in typical java code.
    I think that the only way to properly do this is to have a tool which understands java syntax.
    Anyone know of an IDE which will do this? Does IntelliJ or Netbeans support this kind of search?
    In case you are wondering why I am interested in this, it is because I am refactoring some code to fully use generics, and searching for casts is one source of identifying candidates for genericity.

    cliffblob wrote:
    Better late than never?Yes!
    cliffblob wrote:
    ...The answer I found to ID unnecessary casts was, using Eclipse IDE, In compiler error and warning preferences to warn on unnecessary casts.Thanks for pointing IDEs out. I just opened IntelliJ, and going back to at least version 7.04 (maybe earlier) they have an inspection for "Redundant type cast".
    cliffblob wrote:
    I would still be interested to know if there is a way to identify casts in general in your source, perhaps now four years later there is a way?The only solutions that I can think of are either a complicated regex search, or you must use some tool like an IDE that understand Java syntax and can determine if a cast is happening.

  • Good way to search JTree

    Hi,
    I have a JTree..and I need to search for a node in the tree by its name. For example if my tree is setup like this:
    root
    -----> FRUITS
    ----> apple
    ----> orange
    and i want to search for orange and retrieve the orange node, what is the best way to do this? I was thinking about starting from the root node, and recursively searching the tree comparing the names of the nodes to the nodeNameToSearchFor, if i find a match i return the node.....is there a better way of doing this??
    thanks.

    import javax.swing.JTree;
    import javax.swing.tree.DefaultMutableTreeNode;
    import java.util.Enumeration;
    public class Test3 {
      public static void main(String[] arghs) {
        DefaultMutableTreeNode root = (DefaultMutableTreeNode)new JTree().getModel().getRoot();
        Enumeration enum = root.breadthFirstEnumeration();
        while (enum.hasMoreElements()) System.out.println(enum.nextElement());
    }

  • A better way to execute a series of external commands?

    Hi,
    Consider the code snippet...
    Process proc1 = rt.exec("Some external command");
    Vector vecOutput = outputGobbler.getOutput() ;
    String[] strAlterCmds = new String[vecOutput.size()];
    for ( int k = 0 ; k < vecOutput.size() ; k++ )
    strAlterCmds[k] = new String();
    strAlterCmds[k] = "cmd.exe /c blah.... " +(String) vecOutput.elementAt( k )+ " ;
    Process proc2 = rt.exec(strAlterCmds[k], null);
    StreamGobbler errorG = new
    StreamGobbler( proc2.getErrorStream(), "Error") ;
    StreamGobbler outputG = new
    StreamGobbler( proc2.getInputStream(), "Output") ;
    errorG.start() ;
    outputG.start() ;
    int exitVal1 = proc2.waitFor();
    The second part of the execution is taking AGES and I am not sure forking a new process for runtime execution is the best alternative here.
    I would be glad if someone can point me to a smarter solution.
    In short: I intend to execute a series of commands using RTE depending on the values I get in the loop.
    Thanks.

    Jared,
    Thank you for responding to my posted message. Rendezvous is a new concept to me, maybe
    it's the solution to my problem. I have been trying to read the on line manual and
    example VIs from National Instruments website. I still have a hard time to understand
    the concept.
    One of the example I saw is using rendezvous to run some sub VIs. But in my case, I have
    only one VI that is a while loop. Inside the while loop, there are a few tasks running
    simultaneously. I don't know whether it will serve my purpose.
    Guangde Wang
    Jared O'Mara wrote:
    > Guangde,
    >
    > Referring to your 2nd method, use rendezvous (under advanced>>synchronize
    > on function palette) to synchronize two processes. There are good examples
    > that come with labview. basically, you cre
    ate a rendezvous, giving it a
    > size. Using the wait on rendezvous vi, a function will not continue until
    > all rendezvous have reached that point. Using this method, you can synchronize
    > your 2 while loops.
    >
    > Jared
    >
    > Guangde Wang wrote:
    > >I tried two ways to control the tempo of my program.>>One is to use the
    > While Loop's counter and a wait. The drawback of this>method is the cycle
    > length is dependent on the measuring load. So if the>program runs for days,
    > it will be significent off the real time.>>The other way is to use the difference
    > of the Tick Count. It provides>accurate timing but the problem is the sychronization
    > of the clock cycle>and the While Loop cycle. I can try to put a little bit
    > wait but still>can not sychronize them very well. So after a while, there
    > will be two>measures get together.>>I don't know whether there are some better
    > ways to control the program>or whether we have some ways to improve either
    > or both of the above
    two>methods to make them work better. Please let me
    > know if you have any>suggestion.>>Thank you in advance,>>Guangde Wang>

  • A better way to activate an LOV

    I want to allow users to activate the LOVs and run queries from them without having to press the enter query button, then move their cursor to the appropriate text box, then press Ctrl+L, then press the execute query button. Is there some way to fire an LOV from a button press or some simpler way than I have right now?

    Thats really a good way.
    <BLOCKQUOTE><font size="1" face="Verdana, Arial">quote:</font><HR>Originally posted by Steven Lietuvnikas ([email protected]):
    i follow what you're saying but i think i found a better way to do it
    i made a button that says search on it and the whenbuttonpressed trigger is simply enter_query
    this then moves the focus to the first item in the record block
    i then made a whennewiteminstance trigger on the first item in the record block that says:
    DECLARE
    DUMMY BOOLEAN;
    BEGIN
    If (:system.Mode = 'ENTER-QUERY') Then
    DUMMY := SHOW_LOV('LOV_PERSONS',15,10);
    EXECUTE_QUERY;
    END IF;
    END;
    this works good<HR></BLOCKQUOTE>
    null

Maybe you are looking for