7 Replies - 400 Views - Last Post: 16 August 2012 - 06:47 AM Rate Topic: -----

#1 saikrupa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 11-July 12

read from a file and store it in a string

Posted 15 August 2012 - 10:26 AM

i want to read from a file and store it in a single string variable. but the file size is to large can any one help me....................?
Is This A Good Question/Topic? 0
  • +

Replies To: read from a file and store it in a string

#2 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,682
  • Joined: 13-March 10

Re: read from a file and store it in a string

Posted 15 August 2012 - 10:54 AM

read it line by line? character by character?

there are many ways
Was This Post Helpful? 0
  • +
  • -

#3 saikrupa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 11-July 12

Re: read from a file and store it in a string

Posted 15 August 2012 - 11:02 AM

View Postdarek9576, on 15 August 2012 - 10:54 AM, said:

read it line by line? character by character?

there are many ways

but i want to store it in a single string variable is tr any possibility
Was This Post Helpful? 0
  • +
  • -

#4 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,682
  • Joined: 13-March 10

Re: read from a file and store it in a string

Posted 15 August 2012 - 11:33 AM

show us the code. i just told you the solution and you did not get it at all
Was This Post Helpful? 0
  • +
  • -

#5 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1940
  • View blog
  • Posts: 4,028
  • Joined: 11-December 07

Re: read from a file and store it in a string

Posted 15 August 2012 - 03:04 PM

What do you mean by the file size is too large? If the file is too big to fit in memory then you cannot store it all in a variable.
Was This Post Helpful? 0
  • +
  • -

#6 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2656
  • View blog
  • Posts: 11,207
  • Joined: 20-September 08

Re: read from a file and store it in a string

Posted 15 August 2012 - 03:13 PM

http://technojeeves....-string-in-java
Was This Post Helpful? 0
  • +
  • -

#7 saikrupa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 11-July 12

Re: read from a file and store it in a string

Posted 16 August 2012 - 06:41 AM

View Postdarek9576, on 15 August 2012 - 11:33 AM, said:

show us the code. i just told you the solution and you did not get it at all

     inp="";
     try
        {
            FileInputStream fStream=new FileInputStream("C:/Users/RAMESH/Desktop/hai.txt");
            DataInputStream in=new DataInputStream(fStream);
            BufferedReader br=new BufferedReader(new InputStreamReader(in));
            String strLine;
            while((strLine=br.readLine())!=null)
            {
                inp=inp+strLine;
            }
            System.out.println(inp);    
            in.close();
             
        }
        catch(Exception e)
        {
            System.out.println("error"+e.getMessage());
        }   


here the file hai is about 15kb size. in further i want to do some operations on the string inp but it showing error like exceded string limit...........can any one help me for this problem
Was This Post Helpful? 0
  • +
  • -

#8 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2656
  • View blog
  • Posts: 11,207
  • Joined: 20-September 08

Re: read from a file and store it in a string

Posted 16 August 2012 - 06:47 AM

Your error is not likely caused by the size of the file/string as much as the highly inefficient method you're using to create the String. String concatenation should be avoided at the best of times, but your code will cause over-allocation of memory and (possibly) the creation of huge numbers of string buffer -like objects.

Use the code i posted.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1