From efc2c3bf4430c4a8851f6f37eabc26febd4bbbd7 Mon Sep 17 00:00:00 2001 From: Gerd Marmitt Date: Tue, 9 Apr 2024 18:13:56 +0200 Subject: [PATCH] added NIO sample files --- .vscode/launch.json | 106 ++++++++++++++++++ DirList/.vscode/settings.json | 7 ++ DirList/README.md | 18 +++ DirList/src/DirList.java | 48 ++++++++ ExplicitChannelRead/.vscode/settings.json | 7 ++ ExplicitChannelRead/README.md | 18 +++ .../src/ExplicitChannelRead.java | 56 +++++++++ ExplicitChannelRead/test.txt | Bin 0 -> 592 bytes ExplicitChannelRead2/.vscode/settings.json | 7 ++ ExplicitChannelRead2/README.md | 18 +++ .../src/ExplicitChannelRead2.java | 50 +++++++++ ExplicitChannelWrite/.vscode/settings.json | 7 ++ ExplicitChannelWrite/README.md | 18 +++ .../src/ExplicitChannelWrite.java | 42 +++++++ MappedChannelRead/.vscode/settings.json | 7 ++ MappedChannelRead/README.md | 18 +++ MappedChannelRead/src/MappedChannelRead.java | 39 +++++++ MappedChannelRead/test.txt | 1 + MappedChannelWrite/.vscode/settings.json | 7 ++ MappedChannelWrite/README.md | 18 +++ .../src/MappedChannelWrite.java | 38 +++++++ NIOCopy/.vscode/settings.json | 7 ++ NIOCopy/README.md | 18 +++ NIOCopy/src/NIOCopy.java | 35 ++++++ NIOCopy/test.txt | 1 + NIOStreamRead/.vscode/settings.json | 7 ++ NIOStreamRead/README.md | 18 +++ NIOStreamRead/src/NIOStreamRead.java | 28 +++++ NIOStreamRead/test.txt | 1 + NIOStreamWrite/.vscode/settings.json | 7 ++ NIOStreamWrite/README.md | 18 +++ NIOStreamWrite/src/NIOStreamWrite.java | 29 +++++ NIOStreamWrite/test.txt | 1 + 33 files changed, 700 insertions(+) create mode 100644 .vscode/launch.json create mode 100644 DirList/.vscode/settings.json create mode 100644 DirList/README.md create mode 100644 DirList/src/DirList.java create mode 100644 ExplicitChannelRead/.vscode/settings.json create mode 100644 ExplicitChannelRead/README.md create mode 100644 ExplicitChannelRead/src/ExplicitChannelRead.java create mode 100644 ExplicitChannelRead/test.txt create mode 100644 ExplicitChannelRead2/.vscode/settings.json create mode 100644 ExplicitChannelRead2/README.md create mode 100644 ExplicitChannelRead2/src/ExplicitChannelRead2.java create mode 100644 ExplicitChannelWrite/.vscode/settings.json create mode 100644 ExplicitChannelWrite/README.md create mode 100644 ExplicitChannelWrite/src/ExplicitChannelWrite.java create mode 100644 MappedChannelRead/.vscode/settings.json create mode 100644 MappedChannelRead/README.md create mode 100644 MappedChannelRead/src/MappedChannelRead.java create mode 100644 MappedChannelRead/test.txt create mode 100644 MappedChannelWrite/.vscode/settings.json create mode 100644 MappedChannelWrite/README.md create mode 100644 MappedChannelWrite/src/MappedChannelWrite.java create mode 100644 NIOCopy/.vscode/settings.json create mode 100644 NIOCopy/README.md create mode 100644 NIOCopy/src/NIOCopy.java create mode 100644 NIOCopy/test.txt create mode 100644 NIOStreamRead/.vscode/settings.json create mode 100644 NIOStreamRead/README.md create mode 100644 NIOStreamRead/src/NIOStreamRead.java create mode 100644 NIOStreamRead/test.txt create mode 100644 NIOStreamWrite/.vscode/settings.json create mode 100644 NIOStreamWrite/README.md create mode 100644 NIOStreamWrite/src/NIOStreamWrite.java create mode 100644 NIOStreamWrite/test.txt diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..d81ade4 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,106 @@ +{ + // Verwendet IntelliSense zum Ermitteln möglicher Attribute. + // Zeigen Sie auf vorhandene Attribute, um die zugehörigen Beschreibungen anzuzeigen. + // Weitere Informationen finden Sie unter https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "type": "java", + "name": "DirList", + "request": "launch", + "mainClass": "DirList", + "projectName": "PR2-L_ada963eb" + }, + { + "type": "java", + "name": "Current File", + "request": "launch", + "mainClass": "${file}" + }, + { + "type": "java", + "name": "ExplicitChannelRead", + "request": "launch", + "mainClass": "ExplicitChannelRead", + "projectName": "PR2-L_ada963eb" + }, + { + "type": "java", + "name": "ExplicitChannelRead2", + "request": "launch", + "mainClass": "ExplicitChannelRead2", + "projectName": "PR2-L_ada963eb" + }, + { + "type": "java", + "name": "ExplicitChannelWrite", + "request": "launch", + "mainClass": "ExplicitChannelWrite", + "projectName": "PR2-L_ada963eb" + }, + { + "type": "java", + "name": "LeseDatei", + "request": "launch", + "mainClass": "LeseDatei", + "projectName": "PR2-L_ada963eb" + }, + { + "type": "java", + "name": "LeseDatei2", + "request": "launch", + "mainClass": "LeseDatei2", + "projectName": "PR2-L_ada963eb" + }, + { + "type": "java", + "name": "LeseDateiGepuffert", + "request": "launch", + "mainClass": "LeseDateiGepuffert", + "projectName": "PR2-L_ada963eb" + }, + { + "type": "java", + "name": "LeseDateiMitZeilennr", + "request": "launch", + "mainClass": "LeseDateiMitZeilennr", + "projectName": "PR2-L_ada963eb" + }, + { + "type": "java", + "name": "MappedChannelRead", + "request": "launch", + "mainClass": "MappedChannelRead", + "projectName": "PR2-L_ada963eb" + }, + { + "type": "java", + "name": "MappedChannelWrite", + "request": "launch", + "mainClass": "MappedChannelWrite", + "projectName": "PR2-L_ada963eb" + }, + { + "type": "java", + "name": "NIOCopy", + "request": "launch", + "mainClass": "NIOCopy", + "projectName": "PR2-L_ada963eb", + "args": "NIOCopy/test.txt NIOCopy/testkopie.txt" + }, + { + "type": "java", + "name": "NIOStreamRead", + "request": "launch", + "mainClass": "NIOStreamRead", + "projectName": "PR2-L_ada963eb" + }, + { + "type": "java", + "name": "NIOStreamWrite", + "request": "launch", + "mainClass": "NIOStreamWrite", + "projectName": "PR2-L_ada963eb" + } + ] +} \ No newline at end of file diff --git a/DirList/.vscode/settings.json b/DirList/.vscode/settings.json new file mode 100644 index 0000000..0ac215c --- /dev/null +++ b/DirList/.vscode/settings.json @@ -0,0 +1,7 @@ +{ + "java.project.sourcePaths": ["src"], + "java.project.outputPath": "bin", + "java.project.referencedLibraries": [ + "lib/**/*.jar" + ] +} diff --git a/DirList/README.md b/DirList/README.md new file mode 100644 index 0000000..a43b9f6 --- /dev/null +++ b/DirList/README.md @@ -0,0 +1,18 @@ +## Getting Started + +Welcome to the VS Code Java world. Here is a guideline to help you get started to write Java code in Visual Studio Code. + +## Folder Structure + +The workspace contains two folders by default, where: + +- `src`: the folder to maintain sources +- `lib`: the folder to maintain dependencies + +Meanwhile, the compiled output files will be generated in the `bin` folder by default. + +> If you want to customize the folder structure, open `.vscode/settings.json` and update the related settings there. + +## Dependency Management + +The `JAVA PROJECTS` view allows you to manage your dependencies. More details can be found [here](https://github.com/microsoft/vscode-java-dependency#manage-dependencies). diff --git a/DirList/src/DirList.java b/DirList/src/DirList.java new file mode 100644 index 0000000..871fbf3 --- /dev/null +++ b/DirList/src/DirList.java @@ -0,0 +1,48 @@ +/**Copyright (c) Schildt, H.: "Java. The Complete Reference" + * McGraw-Hill Education, Ninth Edition, 2014 + * Display a directory. Requires JDK 7 or later., p. 714 +*/ + +import java.io.*; +import java.nio.file.*; +import java.nio.file.attribute.*; + +public class DirList { + + public static void main(String[] args) { + String dirname = "/Users/gerdmarmitt/Nextcloud/SoSe_24/PR2-L/"; + + // Obtain and manage a directory stream within a try block + try(DirectoryStream dirstrm = Files.newDirectoryStream(Paths.get(dirname))) + { + System.out.println("Directory of " + dirname); + + // Because DirectoryStream implements Iterable, we + // can use a "foreach" loop to display the directory + for(Path entry : dirstrm) { + BasicFileAttributes attribs = + Files.readAttributes(entry, BasicFileAttributes.class); + + if(attribs.isDirectory()) + System.out.print(" "); + else + System.out.print(" "); + + System.out.println(entry.getName(5)); + } + } + catch(InvalidPathException e) + { + System.out.println("Path Error " + e); + } + catch(NotDirectoryException e) + { + System.out.println(dirname + " is not a directory."); + } + catch(IOException e) + { + System.out.println("I/O Error " + e); + } + } + +} diff --git a/ExplicitChannelRead/.vscode/settings.json b/ExplicitChannelRead/.vscode/settings.json new file mode 100644 index 0000000..0ac215c --- /dev/null +++ b/ExplicitChannelRead/.vscode/settings.json @@ -0,0 +1,7 @@ +{ + "java.project.sourcePaths": ["src"], + "java.project.outputPath": "bin", + "java.project.referencedLibraries": [ + "lib/**/*.jar" + ] +} diff --git a/ExplicitChannelRead/README.md b/ExplicitChannelRead/README.md new file mode 100644 index 0000000..a43b9f6 --- /dev/null +++ b/ExplicitChannelRead/README.md @@ -0,0 +1,18 @@ +## Getting Started + +Welcome to the VS Code Java world. Here is a guideline to help you get started to write Java code in Visual Studio Code. + +## Folder Structure + +The workspace contains two folders by default, where: + +- `src`: the folder to maintain sources +- `lib`: the folder to maintain dependencies + +Meanwhile, the compiled output files will be generated in the `bin` folder by default. + +> If you want to customize the folder structure, open `.vscode/settings.json` and update the related settings there. + +## Dependency Management + +The `JAVA PROJECTS` view allows you to manage your dependencies. More details can be found [here](https://github.com/microsoft/vscode-java-dependency#manage-dependencies). diff --git a/ExplicitChannelRead/src/ExplicitChannelRead.java b/ExplicitChannelRead/src/ExplicitChannelRead.java new file mode 100644 index 0000000..d516ef6 --- /dev/null +++ b/ExplicitChannelRead/src/ExplicitChannelRead.java @@ -0,0 +1,56 @@ +/**Copyright (c) Schildt, H.: "Java. The Complete Reference" + * McGraw-Hill Education, Ninth Edition, 2014 + * Use Channel I/O to read a file. Requires JDK 7 or later., p. 701 +*/ + +import java.io.*; +import java.nio.*; +import java.nio.channels.*; +import java.nio.file.*; + +public class ExplicitChannelRead { + + public static void main(String[] args) { + int count; + Path filepath = null; + + // First, obtain a path to the file. + try { + filepath = Paths.get("ExplicitChannelRead/test.txt"); + } + catch(InvalidPathException e) + { + System.out.println("Path Error " + e); + return; + } + + // Next, obtain a channel to that file within a try-with-resources block + try(SeekableByteChannel fChan = Files.newByteChannel(filepath)) + { + ByteBuffer mBuf = ByteBuffer.allocate(128); + + do { + // Read a buffer + count = fChan.read(mBuf); + + // Stop when end of file is reached. + if(count != -1) { + + // Rewind the buffer so that it can be read. + mBuf.rewind(); + + // Read bytes from the buffer and show + // them on the screen as characters. + for(int i = 0; i < count; i++) + System.out.print((char)mBuf.get()); + } + } + while(count != -1); + System.out.println(); + } + catch(IOException e) + { + System.out.println("I/O Error " + e); + } + } +} diff --git a/ExplicitChannelRead/test.txt b/ExplicitChannelRead/test.txt new file mode 100644 index 0000000000000000000000000000000000000000..0b66e32a01e8dba2efd29779f55937ec1ffee10d GIT binary patch literal 592 zcmdT=NlpVn4CKr!>H(wqL)al<4g1a0ZH=1rGIZM`<@Y$`!iggimVB_vRps*H==kLH z?EK>LYPq_;xxKr8SU)~Jzr4P^AN)yC0#}j_jXiv@aV}9OQR=Y}%v=k&G_Zi2)3)0e zd}xybuhu1&%HKE>Di4}^ti^U*3_{pY^B8sDI#$jdaxo`@&?Y1@yis If you want to customize the folder structure, open `.vscode/settings.json` and update the related settings there. + +## Dependency Management + +The `JAVA PROJECTS` view allows you to manage your dependencies. More details can be found [here](https://github.com/microsoft/vscode-java-dependency#manage-dependencies). diff --git a/ExplicitChannelRead2/src/ExplicitChannelRead2.java b/ExplicitChannelRead2/src/ExplicitChannelRead2.java new file mode 100644 index 0000000..b2c0410 --- /dev/null +++ b/ExplicitChannelRead2/src/ExplicitChannelRead2.java @@ -0,0 +1,50 @@ +/**Copyright (c) Schildt, H.: "Java. The Complete Reference" + * McGraw-Hill Education, Ninth Edition, 2014 + * A more compact way to open a channel. Requires JDK 7 or later., p. 703 +*/ + +import java.io.*; +import java.nio.*; +import java.nio.channels.*; +import java.nio.file.*; + +public class ExplicitChannelRead2 { + + public static void main(String[] args) { + int count; + + // Here, the channel is opened on the Path returned by Paths.get() + // There is no need for the filepath variable + try(SeekableByteChannel fChan = Files.newByteChannel(Paths.get("ExplicitChannelRead2/test.txt"))) + { + ByteBuffer mBuf = ByteBuffer.allocate(128); + + do { + // Read a buffer + count = fChan.read(mBuf); + + // Stop when end of file is reached. + if(count != -1) { + + // Rewind the buffer so that it can be read. + mBuf.rewind(); + + // Read bytes from the buffer and show + // them on the screen as characters. + for(int i = 0; i < count; i++) + System.out.print((char)mBuf.get()); + } + } + while(count != -1); + System.out.println(); + } + catch(InvalidPathException e) + { + System.out.println("Path Error " + e); + } + catch(IOException e) + { + System.out.println("I/O Error " + e); + } + } +} diff --git a/ExplicitChannelWrite/.vscode/settings.json b/ExplicitChannelWrite/.vscode/settings.json new file mode 100644 index 0000000..0ac215c --- /dev/null +++ b/ExplicitChannelWrite/.vscode/settings.json @@ -0,0 +1,7 @@ +{ + "java.project.sourcePaths": ["src"], + "java.project.outputPath": "bin", + "java.project.referencedLibraries": [ + "lib/**/*.jar" + ] +} diff --git a/ExplicitChannelWrite/README.md b/ExplicitChannelWrite/README.md new file mode 100644 index 0000000..a43b9f6 --- /dev/null +++ b/ExplicitChannelWrite/README.md @@ -0,0 +1,18 @@ +## Getting Started + +Welcome to the VS Code Java world. Here is a guideline to help you get started to write Java code in Visual Studio Code. + +## Folder Structure + +The workspace contains two folders by default, where: + +- `src`: the folder to maintain sources +- `lib`: the folder to maintain dependencies + +Meanwhile, the compiled output files will be generated in the `bin` folder by default. + +> If you want to customize the folder structure, open `.vscode/settings.json` and update the related settings there. + +## Dependency Management + +The `JAVA PROJECTS` view allows you to manage your dependencies. More details can be found [here](https://github.com/microsoft/vscode-java-dependency#manage-dependencies). diff --git a/ExplicitChannelWrite/src/ExplicitChannelWrite.java b/ExplicitChannelWrite/src/ExplicitChannelWrite.java new file mode 100644 index 0000000..adb5095 --- /dev/null +++ b/ExplicitChannelWrite/src/ExplicitChannelWrite.java @@ -0,0 +1,42 @@ +/**Copyright (c) Schildt, H.: "Java. The Complete Reference" + * McGraw-Hill Education, Ninth Edition, 2014 + * Write to a file using NIO. Requires JDK 7 or later., p. 706 +*/ + +import java.io.*; +import java.nio.*; +import java.nio.channels.*; +import java.nio.file.*; + +public class ExplicitChannelWrite { + + public static void main(String[] args) { + + // Obtain a channel to a file within a try-with-resources block + try(FileChannel fChan = (FileChannel)Files.newByteChannel(Paths.get("ExplicitChannelWrite/test.txt"), + StandardOpenOption.WRITE, + StandardOpenOption.CREATE)) + { + // Create a buffer + ByteBuffer mBuf = ByteBuffer.allocate(128); + + // Write some bytes to the buffer + for(int i = 0; i <26; i++) + mBuf.put((byte)('A' + i)); + + // Reset the buffer so that it can be written + mBuf.rewind(); + + // Write the buffer to the output file + fChan.write(mBuf); + } + catch(InvalidPathException e) + { + System.out.println("Path Error " + e); + } + catch(IOException e) + { + System.out.println("I/O Error " + e); + } + } +} diff --git a/MappedChannelRead/.vscode/settings.json b/MappedChannelRead/.vscode/settings.json new file mode 100644 index 0000000..0ac215c --- /dev/null +++ b/MappedChannelRead/.vscode/settings.json @@ -0,0 +1,7 @@ +{ + "java.project.sourcePaths": ["src"], + "java.project.outputPath": "bin", + "java.project.referencedLibraries": [ + "lib/**/*.jar" + ] +} diff --git a/MappedChannelRead/README.md b/MappedChannelRead/README.md new file mode 100644 index 0000000..a43b9f6 --- /dev/null +++ b/MappedChannelRead/README.md @@ -0,0 +1,18 @@ +## Getting Started + +Welcome to the VS Code Java world. Here is a guideline to help you get started to write Java code in Visual Studio Code. + +## Folder Structure + +The workspace contains two folders by default, where: + +- `src`: the folder to maintain sources +- `lib`: the folder to maintain dependencies + +Meanwhile, the compiled output files will be generated in the `bin` folder by default. + +> If you want to customize the folder structure, open `.vscode/settings.json` and update the related settings there. + +## Dependency Management + +The `JAVA PROJECTS` view allows you to manage your dependencies. More details can be found [here](https://github.com/microsoft/vscode-java-dependency#manage-dependencies). diff --git a/MappedChannelRead/src/MappedChannelRead.java b/MappedChannelRead/src/MappedChannelRead.java new file mode 100644 index 0000000..647b39f --- /dev/null +++ b/MappedChannelRead/src/MappedChannelRead.java @@ -0,0 +1,39 @@ +/**Copyright (c) Schildt, H.: "Java. The Complete Reference" + * McGraw-Hill Education, Ninth Edition, 2014 + * Use a mapped file to read a file. Requires JDK 7 or later, p. 704 +*/ + +import java.io.*; +import java.nio.*; +import java.nio.channels.*; +import java.nio.file.*; + +public class MappedChannelRead { + + public static void main(String[] args) { + + // Obtain a channel to a file within a try-with-resources block. + try(FileChannel fChan = (FileChannel)Files.newByteChannel(Paths.get("MappedChannelRead/test.txt"))) + { + // Gets the size of the file. + long fSize = fChan.size(); + + // Now, map the file into a buffer + MappedByteBuffer mBuf = fChan.map(FileChannel.MapMode.READ_ONLY, 0, fSize); + + // Read and display bytes from buffer: + for(int i = 0; i < fSize; i++) + System.out.print((char)mBuf.get()); + + System.out.println(); + } + catch(InvalidPathException e) + { + System.out.println("Path Error " + e); + } + catch(IOException e) + { + System.out.println("I/O Error " + e); + } + } +} \ No newline at end of file diff --git a/MappedChannelRead/test.txt b/MappedChannelRead/test.txt new file mode 100644 index 0000000..102374b --- /dev/null +++ b/MappedChannelRead/test.txt @@ -0,0 +1 @@ +Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. diff --git a/MappedChannelWrite/.vscode/settings.json b/MappedChannelWrite/.vscode/settings.json new file mode 100644 index 0000000..0ac215c --- /dev/null +++ b/MappedChannelWrite/.vscode/settings.json @@ -0,0 +1,7 @@ +{ + "java.project.sourcePaths": ["src"], + "java.project.outputPath": "bin", + "java.project.referencedLibraries": [ + "lib/**/*.jar" + ] +} diff --git a/MappedChannelWrite/README.md b/MappedChannelWrite/README.md new file mode 100644 index 0000000..a43b9f6 --- /dev/null +++ b/MappedChannelWrite/README.md @@ -0,0 +1,18 @@ +## Getting Started + +Welcome to the VS Code Java world. Here is a guideline to help you get started to write Java code in Visual Studio Code. + +## Folder Structure + +The workspace contains two folders by default, where: + +- `src`: the folder to maintain sources +- `lib`: the folder to maintain dependencies + +Meanwhile, the compiled output files will be generated in the `bin` folder by default. + +> If you want to customize the folder structure, open `.vscode/settings.json` and update the related settings there. + +## Dependency Management + +The `JAVA PROJECTS` view allows you to manage your dependencies. More details can be found [here](https://github.com/microsoft/vscode-java-dependency#manage-dependencies). diff --git a/MappedChannelWrite/src/MappedChannelWrite.java b/MappedChannelWrite/src/MappedChannelWrite.java new file mode 100644 index 0000000..dcd7df6 --- /dev/null +++ b/MappedChannelWrite/src/MappedChannelWrite.java @@ -0,0 +1,38 @@ +/**Copyright (c) Schildt, H.: "Java. The Complete Reference" + * McGraw-Hill Education, Ninth Edition, 2014 + * Write to a mapped file. Requires JDK 7 or later., p. 708 +*/ + +import java.io.*; +import java.nio.*; +import java.nio.channels.*; +import java.nio.file.*; + +public class MappedChannelWrite { + + public static void main(String[] args) { + + // Obtain a channel to a file within a try-with-resources block + try(FileChannel fChan = (FileChannel) + Files.newByteChannel(Paths.get("MappedChannelWrite/test.txt"), + StandardOpenOption.WRITE, + StandardOpenOption.READ, + StandardOpenOption.CREATE)) + { + // Then, map the file into a buffer + MappedByteBuffer mBuf = fChan.map(FileChannel.MapMode.READ_WRITE, 0, 26); + + // Write some bytes to the buffer + for(int i = 0; i <26; i++) + mBuf.put((byte)('A' + i)); + } + catch(InvalidPathException e) + { + System.out.println("Path Error " + e); + } + catch(IOException e) + { + System.out.println("I/O Error " + e); + } + } +} diff --git a/NIOCopy/.vscode/settings.json b/NIOCopy/.vscode/settings.json new file mode 100644 index 0000000..0ac215c --- /dev/null +++ b/NIOCopy/.vscode/settings.json @@ -0,0 +1,7 @@ +{ + "java.project.sourcePaths": ["src"], + "java.project.outputPath": "bin", + "java.project.referencedLibraries": [ + "lib/**/*.jar" + ] +} diff --git a/NIOCopy/README.md b/NIOCopy/README.md new file mode 100644 index 0000000..a43b9f6 --- /dev/null +++ b/NIOCopy/README.md @@ -0,0 +1,18 @@ +## Getting Started + +Welcome to the VS Code Java world. Here is a guideline to help you get started to write Java code in Visual Studio Code. + +## Folder Structure + +The workspace contains two folders by default, where: + +- `src`: the folder to maintain sources +- `lib`: the folder to maintain dependencies + +Meanwhile, the compiled output files will be generated in the `bin` folder by default. + +> If you want to customize the folder structure, open `.vscode/settings.json` and update the related settings there. + +## Dependency Management + +The `JAVA PROJECTS` view allows you to manage your dependencies. More details can be found [here](https://github.com/microsoft/vscode-java-dependency#manage-dependencies). diff --git a/NIOCopy/src/NIOCopy.java b/NIOCopy/src/NIOCopy.java new file mode 100644 index 0000000..2c74601 --- /dev/null +++ b/NIOCopy/src/NIOCopy.java @@ -0,0 +1,35 @@ +/**Copyright (c) Schildt, H.: "Java. The Complete Reference" + * McGraw-Hill Education, Ninth Edition, 2014 + * Copy a file using NIO. Requires JDK 7 or later., p. 709 +*/ + +import java.io.*; +import java.nio.file.*; + +public class NIOCopy { + + public static void main(String[] args) { + + if(args.length != 2) { + System.out.println("Usage: Copy from to"); + return; + } + + // Obtain a channel to a file within a try-with-resources block + try { + Path source = Paths.get(args[0]); + Path target = Paths.get(args[1]); + + // Copy the file + Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING); + } + catch(InvalidPathException e) + { + System.out.println("Path Error " + e); + } + catch(IOException e) + { + System.out.println("I/O Error " + e); + } + } +} diff --git a/NIOCopy/test.txt b/NIOCopy/test.txt new file mode 100644 index 0000000..102374b --- /dev/null +++ b/NIOCopy/test.txt @@ -0,0 +1 @@ +Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. diff --git a/NIOStreamRead/.vscode/settings.json b/NIOStreamRead/.vscode/settings.json new file mode 100644 index 0000000..0ac215c --- /dev/null +++ b/NIOStreamRead/.vscode/settings.json @@ -0,0 +1,7 @@ +{ + "java.project.sourcePaths": ["src"], + "java.project.outputPath": "bin", + "java.project.referencedLibraries": [ + "lib/**/*.jar" + ] +} diff --git a/NIOStreamRead/README.md b/NIOStreamRead/README.md new file mode 100644 index 0000000..a43b9f6 --- /dev/null +++ b/NIOStreamRead/README.md @@ -0,0 +1,18 @@ +## Getting Started + +Welcome to the VS Code Java world. Here is a guideline to help you get started to write Java code in Visual Studio Code. + +## Folder Structure + +The workspace contains two folders by default, where: + +- `src`: the folder to maintain sources +- `lib`: the folder to maintain dependencies + +Meanwhile, the compiled output files will be generated in the `bin` folder by default. + +> If you want to customize the folder structure, open `.vscode/settings.json` and update the related settings there. + +## Dependency Management + +The `JAVA PROJECTS` view allows you to manage your dependencies. More details can be found [here](https://github.com/microsoft/vscode-java-dependency#manage-dependencies). diff --git a/NIOStreamRead/src/NIOStreamRead.java b/NIOStreamRead/src/NIOStreamRead.java new file mode 100644 index 0000000..83539f7 --- /dev/null +++ b/NIOStreamRead/src/NIOStreamRead.java @@ -0,0 +1,28 @@ +import java.io.*; +import java.nio.file.*; + +public class NIOStreamRead { + public static void main(String args[]) + { + int i; + + // Open the file and obtain an stream linked to it + try(InputStream fin = Files.newInputStream(Paths.get("NIOStreamRead/test.txt"))) + { + do { + i = fin.read(); + + if(i != -1) + System.out.print((char) i); + } while(i != -1); + } + catch(InvalidPathException e) + { + System.out.println("Path Error " + e); + } + catch(IOException e) + { + System.out.println("I/O Error " + e); + } + } +} diff --git a/NIOStreamRead/test.txt b/NIOStreamRead/test.txt new file mode 100644 index 0000000..102374b --- /dev/null +++ b/NIOStreamRead/test.txt @@ -0,0 +1 @@ +Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. diff --git a/NIOStreamWrite/.vscode/settings.json b/NIOStreamWrite/.vscode/settings.json new file mode 100644 index 0000000..0ac215c --- /dev/null +++ b/NIOStreamWrite/.vscode/settings.json @@ -0,0 +1,7 @@ +{ + "java.project.sourcePaths": ["src"], + "java.project.outputPath": "bin", + "java.project.referencedLibraries": [ + "lib/**/*.jar" + ] +} diff --git a/NIOStreamWrite/README.md b/NIOStreamWrite/README.md new file mode 100644 index 0000000..a43b9f6 --- /dev/null +++ b/NIOStreamWrite/README.md @@ -0,0 +1,18 @@ +## Getting Started + +Welcome to the VS Code Java world. Here is a guideline to help you get started to write Java code in Visual Studio Code. + +## Folder Structure + +The workspace contains two folders by default, where: + +- `src`: the folder to maintain sources +- `lib`: the folder to maintain dependencies + +Meanwhile, the compiled output files will be generated in the `bin` folder by default. + +> If you want to customize the folder structure, open `.vscode/settings.json` and update the related settings there. + +## Dependency Management + +The `JAVA PROJECTS` view allows you to manage your dependencies. More details can be found [here](https://github.com/microsoft/vscode-java-dependency#manage-dependencies). diff --git a/NIOStreamWrite/src/NIOStreamWrite.java b/NIOStreamWrite/src/NIOStreamWrite.java new file mode 100644 index 0000000..e0fddbb --- /dev/null +++ b/NIOStreamWrite/src/NIOStreamWrite.java @@ -0,0 +1,29 @@ +/**Copyright (c) Schildt, H.: "Java. The Complete Reference" + * McGraw-Hill Education, Ninth Edition, 2014 + * Demonstrate NIO-based, stream output. Requires JDK 7 or later., p. 711 +*/ + +import java.io.*; +import java.nio.file.*; + +public class NIOStreamWrite { + public static void main(String args[]) + { + // Open the file and obtain a stream linked to it. + try(OutputStream fout = new BufferedOutputStream( + Files.newOutputStream(Paths.get("NIOStreamWrite/test.txt")))) + { + // Write some bytes to the stream + for(int i = 0; i <26; i++) + fout.write((byte)('A' + i)); + } + catch(InvalidPathException e) + { + System.out.println("Path Error " + e); + } + catch(IOException e) + { + System.out.println("I/O Error " + e); + } + } +} diff --git a/NIOStreamWrite/test.txt b/NIOStreamWrite/test.txt new file mode 100644 index 0000000..a6860d9 --- /dev/null +++ b/NIOStreamWrite/test.txt @@ -0,0 +1 @@ +ABCDEFGHIJKLMNOPQRSTUVWXYZ \ No newline at end of file