Why can java nio only write bytes?












0














Is it true that java nio can only write bytes? For instance:



    Path path=FileSystems.getDefault().getPath("(a path)")
Files.write(path, "test string".getBytes())


I cannot pass only a string to the second parameter of Files.write.
If this is the case, then why is it that only bytes can be written?










share|improve this question


















  • 9




    everything can only write bytes
    – Scary Wombat
    Nov 12 at 1:56






  • 5




    Bytes are what files contain. Anything that looks like it's writing strings, is actually converting them to bytes and writing those. nio is mostly meant as a fast, efficient interface, so it makes sense that it wouldn't have those helpers.
    – yshavit
    Nov 12 at 2:02
















0














Is it true that java nio can only write bytes? For instance:



    Path path=FileSystems.getDefault().getPath("(a path)")
Files.write(path, "test string".getBytes())


I cannot pass only a string to the second parameter of Files.write.
If this is the case, then why is it that only bytes can be written?










share|improve this question


















  • 9




    everything can only write bytes
    – Scary Wombat
    Nov 12 at 1:56






  • 5




    Bytes are what files contain. Anything that looks like it's writing strings, is actually converting them to bytes and writing those. nio is mostly meant as a fast, efficient interface, so it makes sense that it wouldn't have those helpers.
    – yshavit
    Nov 12 at 2:02














0












0








0







Is it true that java nio can only write bytes? For instance:



    Path path=FileSystems.getDefault().getPath("(a path)")
Files.write(path, "test string".getBytes())


I cannot pass only a string to the second parameter of Files.write.
If this is the case, then why is it that only bytes can be written?










share|improve this question













Is it true that java nio can only write bytes? For instance:



    Path path=FileSystems.getDefault().getPath("(a path)")
Files.write(path, "test string".getBytes())


I cannot pass only a string to the second parameter of Files.write.
If this is the case, then why is it that only bytes can be written?







java nio






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 12 at 1:53









luke kong

113




113








  • 9




    everything can only write bytes
    – Scary Wombat
    Nov 12 at 1:56






  • 5




    Bytes are what files contain. Anything that looks like it's writing strings, is actually converting them to bytes and writing those. nio is mostly meant as a fast, efficient interface, so it makes sense that it wouldn't have those helpers.
    – yshavit
    Nov 12 at 2:02














  • 9




    everything can only write bytes
    – Scary Wombat
    Nov 12 at 1:56






  • 5




    Bytes are what files contain. Anything that looks like it's writing strings, is actually converting them to bytes and writing those. nio is mostly meant as a fast, efficient interface, so it makes sense that it wouldn't have those helpers.
    – yshavit
    Nov 12 at 2:02








9




9




everything can only write bytes
– Scary Wombat
Nov 12 at 1:56




everything can only write bytes
– Scary Wombat
Nov 12 at 1:56




5




5




Bytes are what files contain. Anything that looks like it's writing strings, is actually converting them to bytes and writing those. nio is mostly meant as a fast, efficient interface, so it makes sense that it wouldn't have those helpers.
– yshavit
Nov 12 at 2:02




Bytes are what files contain. Anything that looks like it's writing strings, is actually converting them to bytes and writing those. nio is mostly meant as a fast, efficient interface, so it makes sense that it wouldn't have those helpers.
– yshavit
Nov 12 at 2:02












1 Answer
1






active

oldest

votes


















3














Another method, Files.write(Path,Iterable,Charset,OpenOption...), can be used to write CharSequences directly (a String is a CharSequence).




Write lines of text to a file. Each line is a char sequence and is written to the file in sequence with each line terminated by the platform's line separator, as defined by the system property line.separator. Characters are encoded into bytes using the specified charset.



The options parameter specifies how the file is created or opened. If no options are present then this method works as if the CREATE, TRUNCATE_EXISTING, and WRITE options are present. In other words, it opens the file for writing, creating the file if it doesn't exist, or initially truncating an existing regular-file to a size of 0. The method ensures that the file is closed when all lines have been written (or an I/O error or other runtime exception is thrown). If an I/O error occurs then it may do so after the file has been created or truncated, or after some bytes have been written to the file.




Java 8 added an overload that doesn't require a Charset and uses UTF-8 as a default.



Java 11 added another method: Files.writeString(Path,CharSequence,Charset,OpenOption...).




Write a CharSequence to a file. Characters are encoded into bytes using the specified charset.



All characters are written as they are, including the line separators in the char sequence. No extra characters are added.



The options parameter specifies how the file is created or opened. If no options are present then this method works as if the CREATE, TRUNCATE_EXISTING, and WRITE options are present. In other words, it opens the file for writing, creating the file if it doesn't exist, or initially truncating an existing regular-file to a size of 0.




This method also has an overload that doesn't require a Charset, using UTF-8 as a default again.



Under the hood, however, the CharSequences are converted to bytes just like Scary Wombat and yshavit mention in the question comments. The documentation of these methods even make this explicit:




Characters are encoded into bytes using the specified charset.







share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53255086%2fwhy-can-java-nio-only-write-bytes%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    3














    Another method, Files.write(Path,Iterable,Charset,OpenOption...), can be used to write CharSequences directly (a String is a CharSequence).




    Write lines of text to a file. Each line is a char sequence and is written to the file in sequence with each line terminated by the platform's line separator, as defined by the system property line.separator. Characters are encoded into bytes using the specified charset.



    The options parameter specifies how the file is created or opened. If no options are present then this method works as if the CREATE, TRUNCATE_EXISTING, and WRITE options are present. In other words, it opens the file for writing, creating the file if it doesn't exist, or initially truncating an existing regular-file to a size of 0. The method ensures that the file is closed when all lines have been written (or an I/O error or other runtime exception is thrown). If an I/O error occurs then it may do so after the file has been created or truncated, or after some bytes have been written to the file.




    Java 8 added an overload that doesn't require a Charset and uses UTF-8 as a default.



    Java 11 added another method: Files.writeString(Path,CharSequence,Charset,OpenOption...).




    Write a CharSequence to a file. Characters are encoded into bytes using the specified charset.



    All characters are written as they are, including the line separators in the char sequence. No extra characters are added.



    The options parameter specifies how the file is created or opened. If no options are present then this method works as if the CREATE, TRUNCATE_EXISTING, and WRITE options are present. In other words, it opens the file for writing, creating the file if it doesn't exist, or initially truncating an existing regular-file to a size of 0.




    This method also has an overload that doesn't require a Charset, using UTF-8 as a default again.



    Under the hood, however, the CharSequences are converted to bytes just like Scary Wombat and yshavit mention in the question comments. The documentation of these methods even make this explicit:




    Characters are encoded into bytes using the specified charset.







    share|improve this answer




























      3














      Another method, Files.write(Path,Iterable,Charset,OpenOption...), can be used to write CharSequences directly (a String is a CharSequence).




      Write lines of text to a file. Each line is a char sequence and is written to the file in sequence with each line terminated by the platform's line separator, as defined by the system property line.separator. Characters are encoded into bytes using the specified charset.



      The options parameter specifies how the file is created or opened. If no options are present then this method works as if the CREATE, TRUNCATE_EXISTING, and WRITE options are present. In other words, it opens the file for writing, creating the file if it doesn't exist, or initially truncating an existing regular-file to a size of 0. The method ensures that the file is closed when all lines have been written (or an I/O error or other runtime exception is thrown). If an I/O error occurs then it may do so after the file has been created or truncated, or after some bytes have been written to the file.




      Java 8 added an overload that doesn't require a Charset and uses UTF-8 as a default.



      Java 11 added another method: Files.writeString(Path,CharSequence,Charset,OpenOption...).




      Write a CharSequence to a file. Characters are encoded into bytes using the specified charset.



      All characters are written as they are, including the line separators in the char sequence. No extra characters are added.



      The options parameter specifies how the file is created or opened. If no options are present then this method works as if the CREATE, TRUNCATE_EXISTING, and WRITE options are present. In other words, it opens the file for writing, creating the file if it doesn't exist, or initially truncating an existing regular-file to a size of 0.




      This method also has an overload that doesn't require a Charset, using UTF-8 as a default again.



      Under the hood, however, the CharSequences are converted to bytes just like Scary Wombat and yshavit mention in the question comments. The documentation of these methods even make this explicit:




      Characters are encoded into bytes using the specified charset.







      share|improve this answer


























        3












        3








        3






        Another method, Files.write(Path,Iterable,Charset,OpenOption...), can be used to write CharSequences directly (a String is a CharSequence).




        Write lines of text to a file. Each line is a char sequence and is written to the file in sequence with each line terminated by the platform's line separator, as defined by the system property line.separator. Characters are encoded into bytes using the specified charset.



        The options parameter specifies how the file is created or opened. If no options are present then this method works as if the CREATE, TRUNCATE_EXISTING, and WRITE options are present. In other words, it opens the file for writing, creating the file if it doesn't exist, or initially truncating an existing regular-file to a size of 0. The method ensures that the file is closed when all lines have been written (or an I/O error or other runtime exception is thrown). If an I/O error occurs then it may do so after the file has been created or truncated, or after some bytes have been written to the file.




        Java 8 added an overload that doesn't require a Charset and uses UTF-8 as a default.



        Java 11 added another method: Files.writeString(Path,CharSequence,Charset,OpenOption...).




        Write a CharSequence to a file. Characters are encoded into bytes using the specified charset.



        All characters are written as they are, including the line separators in the char sequence. No extra characters are added.



        The options parameter specifies how the file is created or opened. If no options are present then this method works as if the CREATE, TRUNCATE_EXISTING, and WRITE options are present. In other words, it opens the file for writing, creating the file if it doesn't exist, or initially truncating an existing regular-file to a size of 0.




        This method also has an overload that doesn't require a Charset, using UTF-8 as a default again.



        Under the hood, however, the CharSequences are converted to bytes just like Scary Wombat and yshavit mention in the question comments. The documentation of these methods even make this explicit:




        Characters are encoded into bytes using the specified charset.







        share|improve this answer














        Another method, Files.write(Path,Iterable,Charset,OpenOption...), can be used to write CharSequences directly (a String is a CharSequence).




        Write lines of text to a file. Each line is a char sequence and is written to the file in sequence with each line terminated by the platform's line separator, as defined by the system property line.separator. Characters are encoded into bytes using the specified charset.



        The options parameter specifies how the file is created or opened. If no options are present then this method works as if the CREATE, TRUNCATE_EXISTING, and WRITE options are present. In other words, it opens the file for writing, creating the file if it doesn't exist, or initially truncating an existing regular-file to a size of 0. The method ensures that the file is closed when all lines have been written (or an I/O error or other runtime exception is thrown). If an I/O error occurs then it may do so after the file has been created or truncated, or after some bytes have been written to the file.




        Java 8 added an overload that doesn't require a Charset and uses UTF-8 as a default.



        Java 11 added another method: Files.writeString(Path,CharSequence,Charset,OpenOption...).




        Write a CharSequence to a file. Characters are encoded into bytes using the specified charset.



        All characters are written as they are, including the line separators in the char sequence. No extra characters are added.



        The options parameter specifies how the file is created or opened. If no options are present then this method works as if the CREATE, TRUNCATE_EXISTING, and WRITE options are present. In other words, it opens the file for writing, creating the file if it doesn't exist, or initially truncating an existing regular-file to a size of 0.




        This method also has an overload that doesn't require a Charset, using UTF-8 as a default again.



        Under the hood, however, the CharSequences are converted to bytes just like Scary Wombat and yshavit mention in the question comments. The documentation of these methods even make this explicit:




        Characters are encoded into bytes using the specified charset.








        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 12 at 5:18

























        answered Nov 12 at 5:04









        Slaw

        6,1642930




        6,1642930






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53255086%2fwhy-can-java-nio-only-write-bytes%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Florida Star v. B. J. F.

            Danny Elfman

            Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues