forked from Labore/PR2-L
1
0
Fork 0

added NIO sample files

main
Gerd Marmitt 2024-04-09 18:13:56 +02:00
parent b8c5365f9f
commit efc2c3bf44
33 changed files with 700 additions and 0 deletions

106
.vscode/launch.json vendored 100644
View File

@ -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"
}
]
}

7
DirList/.vscode/settings.json vendored 100644
View File

@ -0,0 +1,7 @@
{
"java.project.sourcePaths": ["src"],
"java.project.outputPath": "bin",
"java.project.referencedLibraries": [
"lib/**/*.jar"
]
}

18
DirList/README.md 100644
View File

@ -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).

View File

@ -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<Path> 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("<DIR> ");
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);
}
}
}

View File

@ -0,0 +1,7 @@
{
"java.project.sourcePaths": ["src"],
"java.project.outputPath": "bin",
"java.project.referencedLibraries": [
"lib/**/*.jar"
]
}

View File

@ -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).

View File

@ -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);
}
}
}

Binary file not shown.

View File

@ -0,0 +1,7 @@
{
"java.project.sourcePaths": ["src"],
"java.project.outputPath": "bin",
"java.project.referencedLibraries": [
"lib/**/*.jar"
]
}

View File

@ -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).

View File

@ -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);
}
}
}

View File

@ -0,0 +1,7 @@
{
"java.project.sourcePaths": ["src"],
"java.project.outputPath": "bin",
"java.project.referencedLibraries": [
"lib/**/*.jar"
]
}

View File

@ -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).

View File

@ -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);
}
}
}

View File

@ -0,0 +1,7 @@
{
"java.project.sourcePaths": ["src"],
"java.project.outputPath": "bin",
"java.project.referencedLibraries": [
"lib/**/*.jar"
]
}

View File

@ -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).

View File

@ -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);
}
}
}

View File

@ -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.

View File

@ -0,0 +1,7 @@
{
"java.project.sourcePaths": ["src"],
"java.project.outputPath": "bin",
"java.project.referencedLibraries": [
"lib/**/*.jar"
]
}

View File

@ -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).

View File

@ -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);
}
}
}

7
NIOCopy/.vscode/settings.json vendored 100644
View File

@ -0,0 +1,7 @@
{
"java.project.sourcePaths": ["src"],
"java.project.outputPath": "bin",
"java.project.referencedLibraries": [
"lib/**/*.jar"
]
}

18
NIOCopy/README.md 100644
View File

@ -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).

View File

@ -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);
}
}
}

1
NIOCopy/test.txt 100644
View File

@ -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.

View File

@ -0,0 +1,7 @@
{
"java.project.sourcePaths": ["src"],
"java.project.outputPath": "bin",
"java.project.referencedLibraries": [
"lib/**/*.jar"
]
}

View File

@ -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).

View File

@ -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);
}
}
}

View File

@ -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.

View File

@ -0,0 +1,7 @@
{
"java.project.sourcePaths": ["src"],
"java.project.outputPath": "bin",
"java.project.referencedLibraries": [
"lib/**/*.jar"
]
}

View File

@ -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).

View File

@ -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);
}
}
}

View File

@ -0,0 +1 @@
ABCDEFGHIJKLMNOPQRSTUVWXYZ