3 Replies Latest reply: Feb 7, 2011 8:39 AM by aksarben RSS

    Which file type should I use and how I use it?

    803515
      Hello!

      I need to create a file with some data that has been inserted manually.
      In this file, for example, there are:

      ************************************************************************
      ProjectName1: string1, string7… string10, date1
      ProjectName2: string11, string15… string20, date2
      .
      .
      .
      ProjectNamek: string 100, string 116 … string 140, date1
      ************************************************************************

      I know only one thing: one of the strings.
      What I need to find in the file:
      I need to find the string at one of these rows and get its ProjectName and date.

      For example: if I have string15,
      I should find string15 in the file, and get its ProjectName and date:
      ProjectName2, date2

      Which file type should I use and how I use it? What is the best solution?
      Thanks!
        • 1. Re: Which file type should I use and how I use it?
          835839
          Using BufferedReader you can read a file line by line

          http://download.oracle.com/javase/1.4.2/docs/api/java/io/BufferedReader.html

          each line returns String object, using indexOf function you can identified the row which contains the input string (string15).

          Using indexOf function you can get the project name (get the position of ":" character)
          and using lastIndexOf function (get the position of ",") you can read the date value

          so using BufferedReader and String's function like indexOf, lastIndexOf and substring your requirement can be possible.
          • 2. Re: Which file type should I use and how I use it?
            YoungWinston
            800512 wrote:
            I need to create a file with some data that has been inserted manually.
            In this file, for example, there are:
            ProjectName1: string1, string7… string10, date1
            In addition to Pandiya's advice, if the string is always in exactly the above format
            (ie:
            1. ProjectName always starts the line and ends with a ':'.
            2. The rest of the strings on the line are separated by ", " and will never contain ", "
            )
            you might also want to have a look at String.split().

            Winston
            • 3. Re: Which file type should I use and how I use it?
              aksarben
              You didn't say how large a typical file is. If the number of lines is very large (100,000 +, for example), you should re-think whether a file is the best way to store & retrieve the data. For large amounts of data (or where searches are done frequently), a database might be a better solution.

              But again, you didn't give much info on the problem, so only you can decide whether a file is the best way to go.