Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Comparison of C Sharp and Java
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== Input/output === Example illustrating how to copy text one line at a time from one file to another, using both languages. {| class="wikitable" |- ! Java ! C# |- |valign=top|<syntaxhighlight lang="Java" style="font-size:90%"> import java.nio.file.*; class FileIOTest { public static void main(String[] args) throws Exception { var lines = Files.readAllLines(Paths.get("input.txt")); Files.write(Paths.get("output.txt"), lines); } } </syntaxhighlight> |valign=top|<syntaxhighlight lang="csharp" style="font-size:90%"> using System.IO; class FileIOTest { public static void Main(string[] args) { var lines = File.ReadLines("input.txt"); File.WriteAllLines("output.txt", lines); } } </syntaxhighlight> |- |valign=top|Notes on the Java implementation: * {{code|Files.readAllLines}} method returns a List of String, with the content of the text file, Files has also the method {{mono|readAllBytes}}, returns an array of {{mono|Strings}}. * {{code|Files.write}} method writes byte array or into an output file, indicated by a Path object. * {{code|Files.write}} method also takes care of buffering and closing the output stream. |valign=top|Notes on the C# implementation: * The {{mono|ReadLines}} method returns an enumerable object that upon enumeration will read the file one line at a time. * The {{mono|WriteAllLines}} method takes an enumerable and retrieves a line at a time and writes it until the enumeration ends. * The underlying reader will automatically allocate a buffer, thus there is no need to explicitly introduce a buffered stream. * {{mono|WriteAllLines}} automatically closes the output stream, also in the case of an abnormal termination. |}
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)