Modifying a single line in a text document?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Modifying a single line in a text document?

  1. #1
    Join Date
    Dec 2011
    Posts
    9

    Modifying a single line in a text document?

    Hi all,

    Is it possible to change a single line in a text file?
    The text document looks like this:
    Example1 : 12 : 1
    Example2 : 13 : 2
    Example3 : 16 : 45

    So for example I want to change the 2nd line too Example2 : 13 : 83.

    This is the current coding Iíve got only itís pretty useless at it will increment the last element only once and append the text document

    public static void IncrementDays(String ID, boolean daystozero){
    BufferedWriter bw;
    BufferedReader br;
    try{
    bw = new BufferedWriter(new FileWriter("txtDB.txt", true));
    br = new BufferedReader(new FileReader("txtDB.txt"));
    String str;
    while((str = br.readLine()) != null){
    System.out.println("fffff");
    if(str.contains(ID) == true){
    String[] temp = str.split(":");
    String Car_Reg = temp[1];
    int x = Integer.parseInt(temp[2]);
    if(daystozero == false)
    bw.append(ID + " : " + Car_Reg + " : " + ++x);
    else
    bw.append(ID + " : " + Car_Reg + " : " + 0);
    }
    }
    bw.close();
    br.close();
    }catch(Exception e){
    System.out.println("Error : " + e);
    }
    }


    Many Thanks

    Edward Francis

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,474

    Re: Modifying a single line in a text document?

    There are several possible solutions:

    1. Use a database to store the info
    2. If it has to be flat file based use an XML format - Java has built in support for parsing, editing and saving XML files.
    3. If it has to be a simple text file do either:
    3a. Use java.io.RandomAccessFile but you have to be careful as you can only overwrite and not insert. ie the new data must be exactly the same length as the data you are replacing
    3b. Use the type of technique you are currently using but do it as follows:
    Open a Reader
    Open a Writer to a temp file
    Read in each line and write it to the temp file until you get to the line you want to edit.
    Write the new data to the temp file.
    Read in the remaining lines and write them to the temp file.
    Delete the original file or rename it to .bak
    Rename the temp file to the original file name.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    Join Date
    Dec 2011
    Location
    Issaquah, WA
    Posts
    9

    Re: Modifying a single line in a text document?

    +1 to the above.

    Just copied this directly from my framework's text reader.

    Using a FileReader, you can read in each line to return a string array. Using the line number - 1, you can use it as the indexer to change the specified line of the array. Then use a FileWriter object to write the array back into the file. =)

    Code:
    public String[] ReadFileLines(String filepath) throws FileNotFoundException, IOException {
         this.ValidateFilePath(filepath);
    
         ArrayList<String> lines = new ArrayList<String>();
    
         java.io.FileReader reader = new java.io.FileReader(filepath);
         java.io.BufferedReader buffer = new java.io.BufferedReader(reader);
    
         String line = null;
    
         while ((line = buffer.readLine()) != null) {
              lines.add(line);
         }
    
         return lines.toArray(new String[lines.size()]);
    }
    Now... what logic you are going to use to determine which line you want is a completely different question.

  4. #4
    Join Date
    Dec 2011
    Posts
    9

    Re: Modifying a single line in a text document?

    Thank you for your replys

    I used the method described in 3b. Works Fine

    Many Thanks Ed

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center