Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Saving new value to file

  1. #1
    Member
    Join Date
    Apr 2012
    Posts
    57
    My Mood
    Angelic
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Saving new value to file

    hello,

    I'm having issues with writing changed value to a file. In my file I have a string which contains

    Fri Jan 14 15:00:00 CET 1898feeffeg
    I want to change one field foe example I want to replace 15:00 to 18:00 but when I run my code it deletes all the contents and writes in only the new value. But I want to keep the original content and only replace one field.

    Can you please help me to detect th error in my code? Thank you very much

        }
            File file = new File("D:\\...\\events\\" + id + ".events.txt");
     
     
     
     
            try {
                BufferedReader bufferedReader = null;
                FileInputStream fileInputStream = null;
                InputStreamReader inputStreamReader =null;
     
                try {
                    fileInputStream = new FileInputStream(file);
                    inputStreamReader = new InputStreamReader(fileInputStream);
                    bufferedReader = new BufferedReader(inputStreamReader);
     
                } catch (FileNotFoundException e) {
                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                }
     
                System.out.println("Searching for :" + Changefield + " in file: " + file.getName());
     
     
                while((field = bufferedReader.readLine()) != null) {
                    int index = field.indexOf(Changefield);
     
                     System.out.println("Position of " + Changefield + "is at the position: " + index);
                     System.out.println(Changefield.replaceFirst(Changefield, newValue));
                     System.out.println("Old value " + Changefield + " new value : " + newValue);
     
     
                }
                Path from = Paths.get(String.valueOf(file));
                Path to = Paths.get("D:\\...\\events\\" + id + ".events-Temp.txt");
                CopyOption[] option = new CopyOption[]{
                        StandardCopyOption.REPLACE_EXISTING,
                        StandardCopyOption.COPY_ATTRIBUTES
                };
                for (Path path : Files.copy(from, to, option)) {
     
                }
                try{
                    FileWriter fileWriter1 = new FileWriter(file);
                    writer = new BufferedWriter(fileWriter1);
                    writer.write(newValue);
     
                //    writer.flush();
     
            }catch(Exception e){
                System.err.println("Error: " +e.getMessage());
            }finally {
                try{
                    writer.close();
                }catch(IOException e){
                    System.err.println("Error " + e.getMessage());
                }
            }
     
     
            } catch (IOException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
        }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Saving new value to file

    Investigate (search) how to open the file or the mechanism that writes to it by appending data rather than writing to it replacing the existing data.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Saving new value to file

    But I want to keep the original content and only replace one field.
    For short files, the easiest way would be to read in the contents of the file, update it and then write out the new contents.

    If you want to update the bytes of the file directly, the RandomAccessFile class has methods to do that.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Saving new value to file

    It occurred to me after reading Norm's message that you may not have explained the problem thoroughly. If you want to intersperse new data with old data in the lines of the file, like:

    OLD
    new
    OLD
    new
    OLD
    new
    . . . etc.

    Then you'll have to use either of the approaches Norm suggested.

  5. #5
    Member
    Join Date
    Apr 2012
    Posts
    57
    My Mood
    Angelic
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Saving new value to file

    Can you please help me out with it? I'm really lost

    I need sth like this : OLD variable,new variable, OLD, Old

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Saving new value to file

    And there's only one line of data in the whole file?

  7. #7
    Member
    Join Date
    Apr 2012
    Posts
    57
    My Mood
    Angelic
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Saving new value to file

    Yes,only one with 5 variables nd I want to change only 1

  8. #8
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Saving new value to file

    That's trivial. Read the line, change the data in the line, write the line back to the file. No appending or any other tricks are required. Try printing (to the screen) the line as it is read from the file and then after your program has changed it. Work that until what is being printed to the screen after it is changed contains the updated data. Then write that string to the file.

    Look into the method String.split() to break the line of data into its parts so that you can easily find and change the correct item, then reconstruct the line of data from the original array with the new data substituted in for the old.

Similar Threads

  1. saving / loading a file
    By Herah in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: December 9th, 2011, 07:04 AM
  2. returning variable and saving to file
    By ravencrest in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: November 18th, 2011, 09:04 AM
  3. Saving to a text file
    By PsYNus in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 15th, 2011, 11:57 AM
  4. saving xml file
    By LOL in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 18th, 2010, 05:45 AM
  5. [SOLVED] Saving A File?
    By MysticDeath in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: August 1st, 2009, 11:56 AM