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 4 of 4

Thread: Problem with replacing substring in a file

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

    Default Problem with replacing substring in a file

    Hello,

    I'm writing a piece of code with should modify my file. I have a string :

    Sat Jan 25 21:00:00 CET 25557feee
    And I would like to change only one variable,but I'm not able to do it. Can you help me?

    Here is the code:
      private void modify(String[] tokens)
        {
                if (tokens.length < 3)
            {
               // System.out.println("Illegal arguments");
                System.out.println("modify [id] [field] [newValue] --> Modify the event with the given id and set the value of the specified field to [newValue].");
                System.out.println();
                return;
            }
            // TODO: Add additional input validity checks
            String id = tokens[0];
            String field = tokens[1];
            String newValue = tokens[2];
            // TODO: Modify the given event locally as well as remote
     
            System.out.print("Your choice was: " + choose);
     
    //        if(choose.equals(id)){
    //            System.out.println("You choosed : " + choose);
    //        }
            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{
     
                    BufferedReader reader = new BufferedReader(new FileReader(file));
                    FileWriter fileWriter1 = new FileWriter(file);
                    writer = new BufferedWriter(fileWriter1);
     
                   String line;
                   while (( line = bufferedReader.readLine()) !=null)  {
                        System.out.print(line);
     
                    }
     
     
     
                //    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.
            }
        }

    Tank you for your help


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Problem with replacing substring in a file

    but I'm not able to do it
    Define 'not able to do it'? What is the problem? Reading the file? Writing the file? Parsing the file?

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

    Default Re: Problem with replacing substring in a file

    I cant change only one variable, when I change the old value to a new value and then try to save it. The whole file gets overwritten and nly the new value stays. The old values disappear

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

    Default Re: Problem with replacing substring in a file

    Duplicate?

    This seems the same as your other thread. If so, please return to that one and tell us how using the String.split() method worked out. Please show your updated code there. (Although I guess you're showing pretty much the same code here as you showed there.)

Similar Threads

  1. Replacing a JButton
    By dougie1809 in forum AWT / Java Swing
    Replies: 3
    Last Post: April 10th, 2013, 07:52 AM
  2. Replacing File Content
    By asheshrocky in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 30th, 2012, 09:48 PM
  3. How do we fill in an array using substring, FileReader and a txt file?
    By wholegrain in forum Java Theory & Questions
    Replies: 5
    Last Post: February 12th, 2012, 03:18 PM
  4. [SOLVED] Replacing words in a text file
    By sanelko in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: January 30th, 2012, 01:56 AM
  5. [SOLVED] replacing subString
    By nasi in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 21st, 2010, 09:47 PM