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.

Page 1 of 2 12 LastLast
Results 1 to 25 of 33

Thread: open text file and edit any line start with name

  1. #1

    Default open text file and edit any line start with name

    I have a text file
    How can I write java swing code that opens the file and adds end of line this character </ok> each line that starts with the <ok>

    The following items are available in the text file
    <li>123>
    <ok>235>
    <li>345:1:2>
    <ok>345:1:2>
    It should be like this
    <li>123>
    <ok>235</ok>
    <li>345:1:2>
    <ok>345:1:2</ok>
    Last edited by cnmeysam; March 21st, 2021 at 06:31 PM.

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

    Default Re: open text file and edit any line start with name

    What have you tried?
    What are the steps the program needs to take to provide the desired solution?
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    cnmeysam (March 21st, 2021)

  4. #3

    Default Re: open text file and edit any line start with name

    Unfortunately, I do not know any solution and I have not tried anything
    I searched on Google and did not find a way
    Last edited by cnmeysam; March 21st, 2021 at 10:17 AM.

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

    Default Re: open text file and edit any line start with name

    Can you describe the steps to take without considering any specific programming language?
    What if the lines in the file were pieces of paper with words written on them and you had a pair of scissors, tape and a pen to make changes to the what was written on the paper.
    What steps would you take to get the desired output?

    Once you get the list of steps we can work on what statements would do that in java.
    If you don't understand my answer, don't ignore it, ask a question.

  6. The Following User Says Thank You to Norm For This Useful Post:

    cnmeysam (March 21st, 2021)

  7. #5

    Default Re: open text file and edit any line start with name

    It must first open a saved text file
    Access the values inside that file
    Find the lines that start with <OK>
    Recognize the number of characters after <OK>
    don't touch or edit that characters
    Scroll to the end of the line
    delete > from end of that line
    and add </OK> to end of that line ( or add </OK ( before > )end of that line)
    Repeat this until all lines starting with <OK> end with </OK>
    And ignore all other lines
    Last edited by cnmeysam; March 21st, 2021 at 11:04 AM.

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

    Default Re: open text file and edit any line start with name

    It must first open a saved text file
    Access the values inside that file
    Ok, work on those steps first.
    Define a File class object with the path to the text file.
    Define a Scanner class object that receives that File class object in its constructor.

    For a test:
    read the first file of the file into a String variable by using the Scanner class's nextLine method.
    Print the contents of that line to show that you have read the first line ok.
    If you don't understand my answer, don't ignore it, ask a question.

  9. The Following User Says Thank You to Norm For This Useful Post:

    cnmeysam (March 21st, 2021)

  10. #7

    Default Re: open text file and edit any line start with name

    some think like that?
    File file = new File("G:\\aaa\\1.txt");
             try (Scanner sc = new Scanner(file, StandardCharsets.UTF_8.name())) {
                while (sc.hasNextLine()){
                    System.out.println(sc.nextLine());
                }
            }
            catch (IOException e) {
                e.printStackTrace();
            }
    Last edited by cnmeysam; March 21st, 2021 at 11:26 AM.

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

    Default Re: open text file and edit any line start with name

    What happened when you compiled and executed the program?
    If you don't understand my answer, don't ignore it, ask a question.

  12. The Following User Says Thank You to Norm For This Useful Post:

    cnmeysam (March 21st, 2021)

  13. #9

    Default Re: open text file and edit any line start with name

    All text inside the text box was printed
    <Azan ID="1">
               <day>Sunday</day>
                <date>1400/01/01</date>
                <morning>04:43:02</morning>
                <Rise>06:06:49
                <Noon>12:12:09
                <Sunset>18:17:30
                <Sunsets>18:35:33
                <midnight>23:30:16
            </Azan>
     
    <Azan ID="2">
               <day>Monday</day>
                <date>1400/01/02</date>
                <morning>04:44:02</morning>
                <Rise>06:07:49
                <Noon>12:14:09
                <Sunset>18:19:31
                <Sunsets>18:37:33
                <midnight>23:35:10
            </Azan>
    .
    .
    .
    .
    .
    Last edited by cnmeysam; March 21st, 2021 at 11:37 AM.

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

    Default Re: open text file and edit any line start with name

    Ok then the program works so far.
    Next these steps:
    Find the lines that start with <OK>
    Recognize the number of characters after <OK>
    don't touch or edit that characters
    Scroll to the end of the line
    delete > from end of that line
    Look at the String class for methods to do those steps to the String read by the nextLine method.
    To remove the ending character of a line use the substring method that starts at 0 ends one char short of the end.

    For debugging: Be sure to print out the new String contents after each new String is created from the last String.
    If you don't understand my answer, don't ignore it, ask a question.

  15. The Following User Says Thank You to Norm For This Useful Post:

    cnmeysam (March 21st, 2021)

  16. #11

    Default Re: open text file and edit any line start with name

    i cant understand how must do it!!

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

    Default Re: open text file and edit any line start with name

    Ok, one step at a time:
    Find the lines that start with <OK>
    Look at the String class's methods.
    Find the method that looks at the starting characters in the String.

    Here is a link to the API doc for the Java SE 8 classes:
    https://docs.oracle.com/javase/8/docs/api/index.html
    Find String in the lefthand window, click it and the API doc for String will be displayed,
    Scroll down to the Method Summary section and read them
    If you don't understand my answer, don't ignore it, ask a question.

  18. The Following User Says Thank You to Norm For This Useful Post:

    cnmeysam (March 21st, 2021)

  19. #13

    Default Re: open text file and edit any line start with name

    something like this
    String myStr = sc.nextLine();
    System.out.println(myStr.startsWith("<ok>"));
    but just i see
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false
    false
    false
    false
    true
    false
    false
    false
    false
    false
    false
    false

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

    Default Re: open text file and edit any line start with name

    You need to print out the value in myStr so you can see if the results of the test are correct.
    If you don't understand my answer, don't ignore it, ask a question.

  21. The Following User Says Thank You to Norm For This Useful Post:

    cnmeysam (March 21st, 2021)

  22. #15

    Default Re: open text file and edit any line start with name

    now how i must say if myStr is true add end of line </ok> ?

    --- Update ---

    Yes, it printed all the lines starting with <OK> correctly

    --- Update ---

    this is my code

    File file = new File("G:\\aaa\\oq.txt");
            try (Scanner sc = new Scanner(file, StandardCharsets.UTF_8.name())) {
                while (sc.hasNextLine()) {
    //                System.out.println(sc.nextLine());
                    String myStr = sc.nextLine();
    //                System.out.println(myStr.startsWith("<ok>"));
                    if(myStr.startsWith("<ok>")==true){
                        System.out.println(myStr);
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

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

    Default Re: open text file and edit any line start with name

    Use an id statement:
    if(tested condition) {
        // do something if condition true
    }else {
       // do this if false
    }

    add end of line </ok>
    Use the + concatenation operator to concatenate one String to another:
    oneString + anotherString
    If you don't understand my answer, don't ignore it, ask a question.

  24. The Following User Says Thank You to Norm For This Useful Post:

    cnmeysam (March 21st, 2021)

  25. #17

    Default Re: open text file and edit any line start with name

    Now how do I say </ok> at the end of those lines and update text file?
    run:
    <ok>04:43:02
    <ok>05:41:30
    <ok>05:39:58
    <ok>05:38:26
    <ok>05:36:53
    <ok>05:35:20
    <ok>05:33:46
    <ok>05:32:12
    <ok>05:30:38
    <ok>05:29:04
    <ok>05:27:29
    <ok>05:25:55
    <ok>05:24:20
    <ok>05:22:45
    <ok>05:21:10
    <ok>05:19:34
    <ok>05:17:59
    <ok>05:16:24
    <ok>05:14:49
    <ok>05:13:14
    <ok>05:11:39
    <ok>05:10:04
    <ok>05:08:29
    <ok>05:06:54
    <ok>05:05:20
    <ok>05:03:46
    <ok>05:02:12
    <ok>05:00:39
    <ok>04:59:06
    <ok>04:57:33
    <ok>04:56:01
    <ok>04:54:29
    <ok>04:52:58
    <ok>04:51:27
    <ok>04:49:57
    <ok>04:48:28
    <ok>04:46:59
    <ok>04:45:31
    <ok>04:44:04
    <ok>04:42:37
    <ok>04:41:11
    <ok>04:39:46
    <ok>04:38:22
    <ok>04:36:59
    <ok>04:35:37
    <ok>04:34:16
    <ok>04:32:57
    <ok>04:31:38
    <ok>04:30:20
    <ok>04:29:04
    <ok>04:27:49
    <ok>04:26:35
    <ok>04:25:23
    <ok>04:24:12
    <ok>04:23:02
    <ok>04:21:54
    <ok>04:20:47
    <ok>04:19:42
    <ok>04:18:39
    <ok>04:17:37
    <ok>04:16:38
    <ok>04:15:39
    <ok>04:14:43
    <ok>04:13:49
    <ok>04:12:56
    <ok>04:12:06
    <ok>04:11:17
    <ok>04:10:30
    <ok>04:09:46
    <ok>04:09:04
    <ok>04:08:23
    <ok>04:07:45
    <ok>04:07:10
    <ok>04:06:36
    <ok>04:06:05
    <ok>04:05:36
    <ok>04:05:09
    <ok>04:04:45
    <ok>04:04:23
    <ok>04:04:04
    <ok>04:03:47
    <ok>04:03:33
    <ok>04:03:21
    <ok>04:03:11
    <ok>04:03:04
    <ok>04:03:00
    <ok>04:02:58
    <ok>04:02:58
    <ok>04:03:01
    <ok>04:03:06
    <ok>04:03:14
    <ok>04:03:24
    <ok>04:03:37
    <ok>04:03:52
    <ok>04:04:10
    <ok>04:04:29
    <ok>04:04:52
    <ok>04:05:16
    <ok>04:05:42
    <ok>04:06:11
    <ok>04:06:42
    <ok>04:07:15
    <ok>04:07:50
    <ok>04:08:27
    <ok>04:09:06
    <ok>04:09:47
    <ok>04:10:29
    <ok>04:11:14
    <ok>04:12:00
    <ok>04:12:47
    <ok>04:13:37
    <ok>04:14:27
    <ok>04:15:20
    <ok>04:16:13
    <ok>04:17:08
    <ok>04:18:04
    <ok>04:19:01
    <ok>04:19:59
    <ok>04:20:59
    <ok>04:21:59
    <ok>04:23:00
    <ok>04:24:02
    <ok>04:25:05
    <ok>04:26:09
    <ok>04:27:13
    <ok>04:28:17
    <ok>04:29:23
    <ok>04:30:28
    <ok>04:31:35
    <ok>04:32:41
    <ok>04:33:48
    <ok>04:34:55
    <ok>04:36:02
    <ok>04:37:10
    <ok>04:38:17
    <ok>04:39:25
    <ok>04:40:33
    <ok>04:41:40
    <ok>04:42:48
    <ok>04:43:55
    <ok>04:45:03
    <ok>04:46:10
    <ok>04:47:17
    <ok>04:48:24
    <ok>04:49:31
    <ok>04:50:37
    <ok>04:51:43
    <ok>04:52:49
    <ok>04:53:54
    <ok>04:55:00
    <ok>04:56:04
    <ok>04:57:09
    <ok>04:58:13
    <ok>04:59:16
    <ok>05:00:20
    <ok>05:01:22
    <ok>05:02:25
    <ok>05:03:27
    <ok>05:04:28
    <ok>05:05:29
    <ok>05:06:30
    <ok>05:07:30
    <ok>05:08:30
    <ok>05:09:29
    <ok>05:10:28
    <ok>05:11:27
    <ok>05:12:25
    <ok>05:13:22
    <ok>05:14:19
    <ok>05:15:16
    <ok>05:16:12
    <ok>05:17:08
    <ok>05:18:03
    <ok>05:18:59
    <ok>05:19:53
    <ok>05:20:48
    <ok>05:21:41
    <ok>05:22:35
    <ok>05:23:28
    <ok>05:24:21
    <ok>05:25:14
    <ok>05:26:06
    <ok>05:26:58
    <ok>05:27:50
    <ok>05:28:41
    <ok>04:29:32
    <ok>04:30:23
    <ok>04:31:14
    <ok>04:32:04
    <ok>04:32:54
    <ok>04:33:44
    <ok>04:34:34
    <ok>04:35:24
    <ok>04:36:13
    <ok>04:37:03
    <ok>04:37:52
    <ok>04:38:41
    <ok>04:39:30
    <ok>04:40:19
    <ok>04:41:08
    <ok>04:41:56
    <ok>04:42:45
    <ok>04:43:34
    <ok>04:44:22
    <ok>04:45:11
    <ok>04:46:00
    <ok>04:46:48
    <ok>04:47:37
    <ok>04:48:26
    <ok>04:49:14
    <ok>04:50:03
    <ok>04:50:52
    <ok>04:51:41
    <ok>04:52:30
    <ok>04:53:19
    <ok>04:54:08
    <ok>04:54:57
    <ok>04:55:47
    <ok>04:56:36
    <ok>04:57:26
    <ok>04:58:16
    <ok>04:59:05
    <ok>04:59:55
    <ok>05:00:45
    <ok>05:01:36
    <ok>05:02:26
    <ok>05:03:16
    <ok>05:04:07
    <ok>05:04:57
    <ok>05:05:48
    <ok>05:06:39
    <ok>05:07:30
    <ok>05:08:21
    <ok>05:09:12
    <ok>05:10:03
    <ok>05:10:54
    <ok>05:11:45
    <ok>05:12:36
    <ok>05:13:27
    <ok>05:14:18
    <ok>05:15:09
    <ok>05:16:00
    <ok>05:16:51
    <ok>05:17:42
    <ok>05:18:33
    <ok>05:19:23
    <ok>05:20:14
    <ok>05:21:04
    <ok>05:21:54
    <ok>05:22:44
    <ok>05:23:33
    <ok>05:24:22
    <ok>05:25:11
    <ok>05:25:59
    <ok>05:26:47
    <ok>05:27:34
    <ok>05:28:21
    <ok>05:29:08
    <ok>05:29:54
    <ok>05:30:39
    <ok>05:31:24
    <ok>05:32:08
    <ok>05:32:51
    <ok>05:33:33
    <ok>05:34:15
    <ok>05:34:56
    <ok>05:35:36
    <ok>05:36:15
    <ok>05:36:53
    <ok>05:37:31
    <ok>05:38:07
    <ok>05:38:42
    <ok>05:39:16
    <ok>05:39:49
    <ok>05:40:21
    <ok>05:40:52
    <ok>05:41:21
    <ok>05:41:49
    <ok>05:42:16
    <ok>05:42:41
    <ok>05:43:05
    <ok>05:43:28
    <ok>05:43:49
    <ok>05:44:09
    <ok>05:44:28
    <ok>05:44:45
    <ok>05:45:00
    <ok>05:45:14
    <ok>05:45:26
    <ok>05:45:36
    <ok>05:45:45
    <ok>05:45:53
    <ok>05:45:58
    <ok>05:46:03
    <ok>05:46:05
    <ok>05:46:05
    <ok>05:46:04
    <ok>05:46:02
    <ok>05:45:57
    <ok>05:45:51
    <ok>05:45:43
    <ok>05:45:33
    <ok>05:45:22
    <ok>05:45:08
    <ok>05:44:53
    <ok>05:44:37
    <ok>05:44:18
    <ok>05:43:58
    <ok>05:43:36
    <ok>05:43:12
    <ok>05:42:47
    <ok>05:42:19
    <ok>05:41:50
    <ok>05:41:20
    <ok>05:40:47
    <ok>05:40:13
    <ok>05:39:38
    <ok>05:39:00
    <ok>05:38:21
    <ok>05:37:41
    <ok>05:36:58
    <ok>05:36:14
    <ok>05:35:29
    <ok>05:34:42
    <ok>05:33:53
    <ok>05:33:03
    <ok>05:32:12
    <ok>05:31:19
    <ok>05:30:24
    <ok>05:29:28
    <ok>05:28:31
    <ok>05:27:32
    <ok>05:26:32
    <ok>05:25:30
    <ok>05:24:28
    <ok>05:23:24
    <ok>05:22:18
    <ok>05:21:12
    <ok>05:20:04
    <ok>05:18:55
    <ok>05:17:44
    <ok>05:16:33
    <ok>05:15:21
    <ok>05:14:07
    <ok>05:12:52
    <ok>05:11:37
    <ok>05:10:20
    <ok>05:09:02
    <ok>05:07:43
    <ok>05:06:24
    <ok>05:05:03
    <ok>05:03:42
    <ok>05:02:20
    <ok>05:00:56
    <ok>04:59:33
    <ok>04:58:08
    <ok>04:56:42
    <ok>04:55:16
    <ok>04:53:49
    <ok>04:52:22
    <ok>04:50:54
    <ok>04:49:25
    <ok>04:47:55
    <ok>04:46:25
    <ok>04:44:55

    i printed by this code
    System.out.println(myStr.concat("</ok>"));
    but i don't now how i must update text file (G:\\aaa\\oq.txt)
    Last edited by cnmeysam; March 21st, 2021 at 12:29 PM.

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

    Default Re: open text file and edit any line start with name

    Is the code now updating lines as desired?
    One way to update the file is to copy and edit all the lines into a List, then write the contents of the List to the file.
    It is not possible to update the lines in the file while they are on the disk when the length of the lines is changing.

    Look at the PrintWriter class's println method for writing the lines from the List to the file.
    If you don't understand my answer, don't ignore it, ask a question.

  27. The Following User Says Thank You to Norm For This Useful Post:

    cnmeysam (March 21st, 2021)

  28. #19

    Default Re: open text file and edit any line start with name

    Can you write me the code to write the file?

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

    Default Re: open text file and edit any line start with name

    You are making good progress. You should be able to do it yourself.
    Create a File for the output file
    Create a PrintWriter using that File object in its constructor.
    use the PrintWriter class's println() method to write the lines to the file.
    Call the PrintWriter's close method after all the lines are written.
    If you don't understand my answer, don't ignore it, ask a question.

  30. The Following User Says Thank You to Norm For This Useful Post:

    cnmeysam (March 21st, 2021)

  31. #21

    Default Re: open text file and edit any line start with name

    it is my all code but write all lines in 1 line
    how i can fix it?

            File file = new File("oq.txt");
            File files = new File("testFile1.txt");
     
            try (Scanner sc = new Scanner(file, StandardCharsets.UTF_8.name())) {
                FileWriter writer = new FileWriter(files);
                while (sc.hasNextLine()) {
    //                System.out.println(sc.nextLine());
                    String myStr = sc.nextLine();
                    if (myStr.startsWith("<Azan") == true) {
                        System.out.println(myStr);
                        writer.write(myStr);
     
                    }
                    if (myStr.startsWith("<Day>") == true) {
                        System.out.println(myStr);
                        writer.write(myStr);
     
                    }
                    if (myStr.startsWith("<Date>") == true) {
                        System.out.println(myStr);
                        writer.write(myStr);
     
                    }
                    if (myStr.startsWith("<Morning>") == true) {
    //                    myStr.concat(myStr+"</Morning>");
    //                    System.out.println(myStr);
                        System.out.println(myStr.concat("</Morning>"));
                        writer.write(myStr.concat("</Morning>"));
                    }
                    if (myStr.startsWith("<Sunrise>") == true) {
                        System.out.println(myStr.concat("</Sunrise>"));
                        writer.write(myStr.concat("</Sunrise>"));
                    }
                    if (myStr.startsWith("<Noon>") == true) {
                        System.out.println(myStr.concat("</Noon>"));
                        writer.write(myStr.concat("</Noon>"));
     
                    }
                    if (myStr.startsWith("<sunset>") == true) {
                        System.out.println(myStr.concat("</sunset>"));
                        writer.write(myStr.concat("</sunset>"));
     
                    }
                    if (myStr.startsWith("<evening>") == true) {
                        System.out.println(myStr.concat("</evening>"));
                        writer.write(myStr.concat("</evening>"));
     
                    }
                    if (myStr.startsWith("<midnight>") == true) {
                        System.out.println(myStr.concat("</midnight>"));
                        writer.write(myStr.concat("</midnight>"));
     
                    }
                    if (myStr.startsWith("</Azan") == true) {
                        System.out.println(myStr);
                        writer.write(myStr);
     
                    }
     
    //Create the file
                    if (file.createNewFile()) {
                        System.out.println("File is created!");
     
                    } else {
                        System.out.println("File already exists.");
                    }
                }
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }


    --- Update ---

    i use this
    writer.write(myStr+"\n");
    but i have just last problem
    If my text consists of 2 words and the between of this word is space, it deletes space when printing, for example, if it is Hello World, it writes HelloWorld in all text file

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

    Default Re: open text file and edit any line start with name

    The code has a list of if statements. Do you ever expect more than one of them to be true at the same time?
    If you expect only one if statement to be true, then you should this technique: if/else if/else where the ending else statement would be executed if none of the preceding if statements were true.

    write all lines in 1 line
    The output file should not be created inside of the loop. That would create a new file every time the loop iterates.
    If you want all the lines written to a single file, then create the file outside of the loop, write the lines to it inside the loop and close the file after the loop finishes.
    If you don't understand my answer, don't ignore it, ask a question.

  33. The Following User Says Thank You to Norm For This Useful Post:

    cnmeysam (March 21st, 2021)

  34. #23

    Default Re: open text file and edit any line start with name

    but i have just last problem
    If my text consists of 2 words and the between of this word is space, it deletes space when printing, for example, if it is Hello World, it writes HelloWorld This happens to all words in all text file

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

    Default Re: open text file and edit any line start with name

    Please post the current version of the code.
    If you don't understand my answer, don't ignore it, ask a question.

  36. The Following User Says Thank You to Norm For This Useful Post:

    cnmeysam (March 21st, 2021)

  37. #25

    Default Re: open text file and edit any line start with name

    now it is my cod and work correctly just If my text consists of 2 words and the between of this word is space, it deletes space when printing, for example, if it is Hello World in original text, it writes HelloWorld This happens to all words in new text file for example in original text is
    <Azan ID="1">
    in new file write
    <AzanID="1">

    File file = new File("oq.txt");
            File files = new File("testFile1.txt");
     
            try (Scanner sc = new Scanner(file, StandardCharsets.UTF_8.name())) {
                FileWriter writer = new FileWriter(files);
                while (sc.hasNextLine()) {
    //                System.out.println(sc.nextLine());
                    String myStr = sc.nextLine();
                    if (myStr.startsWith("<Azan") == true) {
                        System.out.println(myStr);
                        writer.write(myStr+"\n");
     
                    }
                    else if (myStr.startsWith("<Day>") == true) {
                        System.out.println(myStr);
                        writer.write(myStr+"\n");
     
                    }
                    else if (myStr.startsWith("<Date>") == true) {
                        System.out.println(myStr);
                        writer.write(myStr+"\n");
     
                    }
                    else if (myStr.startsWith("<morning>") == true) {
    //                    myStr.concat(myStr+"</morning>");
    //                    System.out.println(myStr);
                        System.out.println(myStr.concat("</morning>"));
                        writer.write(myStr.concat("</morning>")+"\n");
                    }
                    else if (myStr.startsWith("<Sunrise>") == true) {
                        System.out.println(myStr.concat("</Sunrise>"));
                        writer.write(myStr.concat("</Sunrise>")+"\n");
                    }
                    else if (myStr.startsWith("<Noon>") == true) {
                        System.out.println(myStr.concat("</Noon>"));
                        writer.write(myStr.concat("</Noon>")+"\n");
     
                    }
                    else if (myStr.startsWith("<sunset>") == true) {
                        System.out.println(myStr.concat("</sunset>"));
                        writer.write(myStr.concat("</sunset>")+"\n");
     
                    }
                    else if (myStr.startsWith("<evening>") == true) {
                        System.out.println(myStr.concat("</evening>"));
                        writer.write(myStr.concat("</evening>")+"\n");
     
                    }
                    else if (myStr.startsWith("<midnight>") == true) {
                        System.out.println(myStr.concat("</midnight>"));
                        writer.write(myStr.concat("</midnight>")+"\n");
     
                    }
                    else if (myStr.startsWith("</Azan") == true) {
                        System.out.println(myStr);
                        writer.write(myStr+"\n");
     
     
                    }
     
    //Create the file
                    else if (file.createNewFile()) {
                        System.out.println("File is created!");
     
                    } else {
                        System.out.println("File already exists.");
                    }
                }
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 21
    Last Post: October 31st, 2019, 05:22 AM
  2. open multiple text file to input to mysql
    By csharp100 in forum JDBC & Databases
    Replies: 2
    Last Post: November 26th, 2012, 09:58 PM
  3. Replies: 10
    Last Post: September 16th, 2011, 07:49 PM
  4. Allow user input to edit text file??
    By dannyyy in forum Java Theory & Questions
    Replies: 2
    Last Post: April 6th, 2011, 06:53 AM
  5. Open Text file
    By java_kiddy in forum File I/O & Other I/O Streams
    Replies: 7
    Last Post: October 5th, 2010, 02:52 AM

Tags for this Thread