opening text files

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • tonyg

    opening text files

    Hi everyone,

    I was wondering if there is an efficient or fast or preferred way to
    open text files in java. I want to load the text file into a JTextPane.

    The way I do it now is...

    .....
    BufferedReader objBrIn = new BufferedReader( new FileReader(this .file));
    String strTemp;
    String strOut = "";
    try {
    while ((strTemp = objBrIn.readLin e()) != null) {
    strOut += (strTemp + newline);
    }
    objTextPane.set Text(strOut);
    .....

    but that takes forever to open and display even moderately large text files.

    Does anyone know a better way to do this?

    Thanx,
    Tony G.

  • Anthony Borla

    #2
    Re: opening text files


    "tonyg" <tonyg@rollanet .org> wrote in message
    news:TCwzb.1340 43$Fv8.89334@tw ister01.bloor.i s.net.cable.rog ers.com...[color=blue]
    > Hi everyone,
    >
    > I was wondering if there is an efficient or fast or preferred
    > way to open text files in java. I want to load the text file
    > into a JTextPane.
    >
    > The way I do it now is...
    >
    > .....
    > BufferedReader objBrIn = new BufferedReader(
    > new FileReader(this .file));
    > String strTemp;
    > String strOut = "";
    > try {
    > while ((strTemp = objBrIn.readLin e()) != null) {
    > strOut += (strTemp + newline);
    > }
    > objTextPane.set Text(strOut);
    > .....
    >
    > but that takes forever to open and display even moderately
    > large text files.
    >
    > Does anyone know a better way to do this?
    >[/color]

    I don't think your problem is due to 'slow' stream reading as much as it is
    to do with [needlessly] creating too many 'String' objects. For example,
    this:

    strOut += (strTemp + newline);

    is grossly inefficient; far better to do this:

    StringBuffer strOut;
    ...
    strOut.append(s trTemp).append( newline);
    ...
    objTextPane.set Text(strOut.toS tring());

    If this doesn't improve performance [though I beleieve it] try reading the
    file - using a single 'read' call - into an array, then wrap this inside a
    'StringBuffer', and return a 'String' via:

    ...
    objTextPane.set Text(strOut.toS tring());

    as shown above.

    I hope this helps.

    Anthony Borla


    Comment

    • tonyg

      #3
      Re: opening text files

      That looks great!

      I'll give it a try tomorrow first thing.

      thank you,
      Tony G.


      Anthony Borla wrote:[color=blue]
      > "tonyg" <tonyg@rollanet .org> wrote in message
      > news:TCwzb.1340 43$Fv8.89334@tw ister01.bloor.i s.net.cable.rog ers.com...
      >[color=green]
      >>Hi everyone,
      >>
      >>I was wondering if there is an efficient or fast or preferred
      >>way to open text files in java. I want to load the text file
      >>into a JTextPane.
      >>
      >>The way I do it now is...
      >>
      >> .....
      >> BufferedReader objBrIn = new BufferedReader(
      >> new FileReader(this .file));
      >> String strTemp;
      >> String strOut = "";
      >> try {
      >> while ((strTemp = objBrIn.readLin e()) != null) {
      >> strOut += (strTemp + newline);
      >> }
      >> objTextPane.set Text(strOut);
      >> .....
      >>
      >>but that takes forever to open and display even moderately
      >>large text files.
      >>
      >>Does anyone know a better way to do this?
      >>[/color]
      >
      >
      > I don't think your problem is due to 'slow' stream reading as much as it is
      > to do with [needlessly] creating too many 'String' objects. For example,
      > this:
      >
      > strOut += (strTemp + newline);
      >
      > is grossly inefficient; far better to do this:
      >
      > StringBuffer strOut;
      > ...
      > strOut.append(s trTemp).append( newline);
      > ...
      > objTextPane.set Text(strOut.toS tring());
      >
      > If this doesn't improve performance [though I beleieve it] try reading the
      > file - using a single 'read' call - into an array, then wrap this inside a
      > 'StringBuffer', and return a 'String' via:
      >
      > ...
      > objTextPane.set Text(strOut.toS tring());
      >
      > as shown above.
      >
      > I hope this helps.
      >
      > Anthony Borla
      >
      >[/color]

      Comment

      Working...