Results 1 to 7 of 7
  1. #1
    toya is offline Member
    Join Date
    Apr 2012
    Posts
    3
    Rep Power
    0

    Default BufferedWriter method: write

    hello all(:

    i'm trying to write something using file.write(String, int off, int len);
    but when i enter the amount of characters in my string there is an error of exceeding count limit or something like that.
    i was just wondering what the limit (highest number) is to use in int len to fix my code accordingly?
    thanks!

  2. #2
    doWhile is online now Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,609
    Rep Power
    5

    Default Re: BufferedWriter method: write

    Posting your code as an SSCCE and all errors in their entirety does wonders to help yourself get help

  3. #3
    toya is offline Member
    Join Date
    Apr 2012
    Posts
    3
    Rep Power
    0

    Default Re: BufferedWriter method: write

    sorry, i'm new to this.
    okay so my code is:

    finalFile.write(newRead,0,40);

    where finalFile is the file i'm trying to write to and i want it to write string newRead which is about 40 characters long but when i run it the error says :

    Error: String index out of range: 40

  4. #4
    doWhile is online now Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,609
    Rep Power
    5

    Default Re: BufferedWriter method: write

    See Short, Self Contained, Correct Example
    Given what you posted I can guess (note: guesses can often be wrong) - your String is less than 40 characters long. Try passing in the len parameter of the method the actual length of the String you are writing.

  5. #5
    toya is offline Member
    Join Date
    Apr 2012
    Posts
    3
    Rep Power
    0

    Default Re: BufferedWriter method: write

    thanks! let's see if this works now.
    let's try again:

    qqqint readLength = newRead.length(); //gets length of text
    qqqqqtry{
    qqqqqqqqFileWriter file = new FileWriter("out.txt");
    qqqqqqqqBufferedWriter finalFile = new BufferedWriter(file);
    qqqqqqqqfinalFile.write(newRead,0,readLength);

    when i use this code it'll write part of the text (the last line of the three i have) even though i told it to write the length of the whole string...

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Moderator
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    9,947
    Rep Power
    16

    Default Re: BufferedWriter method: write

    You've been asked twice to post a SSCCE, the second time with a link. If you can't be bothered to do that to help us help you, why should members here be bothered reading through uncompilable code snippets?

    Also, as a new member you should have gone through the Forum Rules where you would have found a link to the Guide For New Members, and would have known how to post code here.

    db
    Why do they call it rush hour when nothing moves? - Robin Williams

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    10,481
    Rep Power
    16

    Default Re: BufferedWriter method: write

    Have you closed the BufferedWriter?
    Please do not ask for code as refusal often offends.

Similar Threads

  1. How to Write to a File Using a Method
    By Anagha in forum New To Java
    Replies: 7
    Last Post: 04-06-2011, 06:50 AM
  2. Writing a method to write a method?
    By balla in forum New To Java
    Replies: 5
    Last Post: 04-01-2011, 12:05 PM
  3. BufferedWriter
    By Karthika in forum New To Java
    Replies: 6
    Last Post: 04-22-2010, 09:23 AM
  4. how to write method syntax ?
    By Bader in forum New To Java
    Replies: 2
    Last Post: 03-14-2010, 05:58 PM
  5. Trying to write toString method
    By curious in forum New To Java
    Replies: 3
    Last Post: 10-28-2009, 02:43 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •