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 2 of 2 FirstFirst 12
Results 26 to 33 of 33

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

  1. #26
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

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

    Why is createNewFile being called inside of the loop? Why is it trying to create the input file?
    What is in oq.txt when the program finishes?

    What is shown on the console when the program is executed?
    If you don't understand my answer, don't ignore it, ask a question.

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

    cnmeysam (March 21st, 2021)

  3. #27

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

    To write values line by line
    The basic data is in it oq.txt
    All data retrieved from the original file will be displayed in the console after editing ( The same data that is stored in the new file )

    I do not understand, except for the part related to space, the program works properly. what is the problem?

  4. #28
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

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

    Please copy what is printed on the console and paste it here.

    Why is createNewFile being called inside of the loop?

    Is there other code that reads/writes the output file? I don't see any statements that would remove the space.
    The code does not remove a space for me.
    If you don't understand my answer, don't ignore it, ask a question.

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

    cnmeysam (March 21st, 2021)

  6. #29

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

    run:
    File already exists.
    File already exists.
    File already exists.
    File already exists.
    File already exists.
    <AzanID="1">
    <Day>Sunday</Day>
    <date>1400/01/01</date>
    <morning>04:43:02</morning>
    <Sunrise>06:06:49</Sunrise>
    <Noon>12:12:09</Noon>
    <sunset>18:17:30</sunset>
    <evening>18:35:33</evening>
    <midnight>23:30:16</midnight>
    </Azan>
    File already exists.
    <AzanID="2">
    <Day>Monday</Day>
    <date>1400/01/02</date>
    <morning>05:41:30</morning>
    <Sunrise>07:05:23</Sunrise>
    <Noon>13:11:51</Noon>
    <sunset>19:18:20</sunset>
    <evening>19:36:23</evening>
    <midnight>00:29:55</midnight>
    </Azan>
    File already exists.
    not any code just this is

    Quote Originally Posted by cnmeysam View Post

    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();
            }
    <AzanID="1">
    <AzanID="2">
    must be
    <Azan ID="1">
    <Azan ID="2">
    like original file

    Why is createNewFile being called inside of the loop? where i must write that?

    --- Update ---

    in the oq.txt file is:
    <Azan ID="1">
    <Day>Sunday</Day>
    <date>1400/01/01</date>
    <morning>04:43:02
    <Sunrise>06:06:49
    <Noon>12:12:09
    <sunset>18:17:30
    <evening>18:35:33
    <midnight>23:30:16
    </Azan>
     
    <Azan ID="2">
    <Day>Monday</Day>
    <date>1400/01/02</date>
    <morning>05:41:30
    <Sunrise>07:05:23
    <Noon>13:11:51
    <sunset>19:18:20
    <evening>19:36:23
    <midnight>00:29:55
    </Azan>

    in new file

    <AzanID="1">
    <Day>Sunday</Day>
    <date>1400/01/01</date>
    <morning>04:43:02</morning>
    <Sunrise>06:06:49</Sunrise>
    <Noon>12:12:09</Noon>
    <sunset>18:17:30</sunset>
    <evening>18:35:33</evening>
    <midnight>23:30:16</midnight>
    </Azan>
     
    <AzanID="2">
    <Day>Monday</Day>
    <date>1400/01/02</date>
    <morning>05:41:30</morning>
    <Sunrise>07:05:23</Sunrise>
    <Noon>13:11:51</Noon>
    <sunset>19:18:20</sunset>
    <evening>19:36:23</evening>
    <midnight>00:29:55</midnight>
    </Azan>


    --- Update ---

    i think
    myStr.startsWith
    Cannot recognize space. because when before or after a word is placed space, it does not read space
    Last edited by cnmeysam; March 21st, 2021 at 04:37 PM.

  7. #30
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

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

    I copied the contents of the oq.txt file that was posted and saved it on my PC.
    When I run the posted code I get this in the testFile1.txt:
    <Azan ID="1">
    <Day>Sunday</Day>
    <morning>04:43:02</morning>
    <Sunrise>06:06:49</Sunrise>
    <Noon>12:12:09</Noon>
    <sunset>18:17:30</sunset>
    <evening>18:35:33</evening>
    <midnight>23:30:16</midnight>
    </Azan>
    <Azan ID="2">
    <Day>Monday</Day>
    <morning>05:41:30</morning>
    <Sunrise>07:05:23</Sunrise>
    <Noon>13:11:51</Noon>
    <sunset>19:18:20</sunset>
    <evening>19:36:23</evening>
    <midnight>00:29:55</midnight>
    </Azan>
    There is a space before the ID=
    Also Notice there is NOT a blank line between </Azan> and <Azan ID="2">
    Also this line is NOT in the new file: <date>1400/01/01</date>
    I do not understand how what you say is in new file came from the posted code.

    There must be a different program being used to create the new file.
    If you don't understand my answer, don't ignore it, ask a question.

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

    cnmeysam (March 21st, 2021)

  9. #31

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

    you cant see <date>1400/01/01</date> because date must be Date
    i can't understand in my project Why remove space before ID

    --- Update ---

    I'm very, very sorry I was wrong. The original file had a problem

  10. #32
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

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

    Is your problem solved now?
    If you don't understand my answer, don't ignore it, ask a question.

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

    cnmeysam (March 21st, 2021)

  12. #33

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

    ok thank my problem solved now

Page 2 of 2 FirstFirst 12

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