Why can java nio only write bytes?
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
add a comment |
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
9
everything can only write bytes
– Scary Wombat
Nov 12 '18 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 '18 at 2:02
add a comment |
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
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
java nio
asked Nov 12 '18 at 1:53
luke kongluke kong
113
113
9
everything can only write bytes
– Scary Wombat
Nov 12 '18 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 '18 at 2:02
add a comment |
9
everything can only write bytes
– Scary Wombat
Nov 12 '18 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 '18 at 2:02
9
9
everything can only write bytes
– Scary Wombat
Nov 12 '18 at 1:56
everything can only write bytes
– Scary Wombat
Nov 12 '18 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 '18 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 '18 at 2:02
add a comment |
1 Answer
1
active
oldest
votes
Another method, Files.write(Path,Iterable,Charset,OpenOption...)
, can be used to write CharSequence
s 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 of0
. 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 of0
.
This method also has an overload that doesn't require a Charset
, using UTF-8
as a default again.
Under the hood, however, the CharSequence
s 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.
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Another method, Files.write(Path,Iterable,Charset,OpenOption...)
, can be used to write CharSequence
s 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 of0
. 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 of0
.
This method also has an overload that doesn't require a Charset
, using UTF-8
as a default again.
Under the hood, however, the CharSequence
s 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.
add a comment |
Another method, Files.write(Path,Iterable,Charset,OpenOption...)
, can be used to write CharSequence
s 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 of0
. 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 of0
.
This method also has an overload that doesn't require a Charset
, using UTF-8
as a default again.
Under the hood, however, the CharSequence
s 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.
add a comment |
Another method, Files.write(Path,Iterable,Charset,OpenOption...)
, can be used to write CharSequence
s 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 of0
. 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 of0
.
This method also has an overload that doesn't require a Charset
, using UTF-8
as a default again.
Under the hood, however, the CharSequence
s 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.
Another method, Files.write(Path,Iterable,Charset,OpenOption...)
, can be used to write CharSequence
s 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 of0
. 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 of0
.
This method also has an overload that doesn't require a Charset
, using UTF-8
as a default again.
Under the hood, however, the CharSequence
s 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.
edited Nov 12 '18 at 5:18
answered Nov 12 '18 at 5:04
SlawSlaw
8,21631033
8,21631033
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
9
everything can only write bytes
– Scary Wombat
Nov 12 '18 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 '18 at 2:02