Results 1 to 4 of 4
  1. #1
    Join Date
    Mar 2009
    Posts
    15
    Rep Power
    0

    Default Write to line in file

    Is there some method of script to write to a specific line in a file?

    I tried some things like
    Java Code:
    public String writeFile(int x, String info) throws IOException 
        {
           BufferedWriter writer = new BufferedWriter(new FileWriter("test.txt"));
    
           int startLine = x;
           int endLine = x;
    
           for (int i = 0; i < startLine; i++)
           { 
               info = writer.writeLine(); 
           }
           for (int i = startLine; i < endLine + 1; i++) 
           {
            writer.write(info);
            writer.close();
    
           }
        
        }
    but writeLine is not a method...
    So I was wondering if there were any methods to solve this.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Did you read the Java doc about readLine() ? How can you say that it's not a method, depends on what you say that?

  3. #3
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    No, read the original file, line for line, and write each of those lines to a new temp file. When you get the line that needs to be changed, deleted, inserted, whatever, do that and write it out to the new temp file, then continue reading the original and writing it to the new. Close both streams, delete the original file and rename the new temp file.

  4. #4
    gwithey is offline Member
    Join Date
    Mar 2009
    Posts
    18
    Rep Power
    0

    Default

    Can you not write a writer class and have a method that returns the information and prints it out.
    -Long time no c-
    -:eek:-

Similar Threads

  1. Replies: 5
    Last Post: 06-07-2014, 05:39 PM
  2. Replies: 5
    Last Post: 02-05-2009, 10:28 AM
  3. Replies: 7
    Last Post: 11-19-2008, 02:09 PM
  4. [SOLVED] File Write in File
    By Eku in forum New To Java
    Replies: 4
    Last Post: 10-27-2008, 04:01 AM
  5. Reading in data from file line by line
    By bluekswing in forum New To Java
    Replies: 1
    Last Post: 10-02-2007, 12:19 AM

Posting Permissions

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