Data update
This commit is contained in:
parent
4bb20c9b71
commit
cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions
29
Task/Fixed-length-records/Java/fixed-length-records-1.java
Normal file
29
Task/Fixed-length-records/Java/fixed-length-records-1.java
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
import module java.base;
|
||||
|
||||
public final class FixedLengthRecords {
|
||||
|
||||
public static void main() throws IOException {
|
||||
try ( FileChannel reader = FileChannel.open(Path.of("./infile.dat"), StandardOpenOption.READ);
|
||||
FileChannel writer = FileChannel.open(Path.of("./outfile.dat"), StandardOpenOption.CREATE,
|
||||
StandardOpenOption.WRITE) ) {
|
||||
|
||||
ByteBuffer buffer = ByteBuffer.allocate(BUFFER_SIZE);
|
||||
|
||||
while ( reader.read(buffer) != END_OF_FILE ) {
|
||||
for ( int i = 0, j = BUFFER_SIZE - 1; i < j; j--, i++ ) {
|
||||
final byte temp = buffer.get(i);
|
||||
buffer.put(i, buffer.get(j));
|
||||
buffer.put(j, temp);
|
||||
}
|
||||
|
||||
buffer.flip();
|
||||
writer.write(buffer);
|
||||
buffer.flip();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static final int BUFFER_SIZE = 80;
|
||||
private static final int END_OF_FILE = -1;
|
||||
|
||||
}
|
||||
67
Task/Fixed-length-records/Java/fixed-length-records-2.java
Normal file
67
Task/Fixed-length-records/Java/fixed-length-records-2.java
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
import module java.base;
|
||||
|
||||
public final class FixedLengthRecordsBonusRound {
|
||||
|
||||
public static void main() throws IOException {
|
||||
// Create and display the 'forth.txt' file obtained from the 'sample.txt' file used in the standard task
|
||||
textToBlock("./sample.txt", "./forth.blk");
|
||||
blockToText("./forth.blk", "./forth.txt");
|
||||
Files.lines(Path.of("./forth.txt")).forEach(IO::println);
|
||||
|
||||
// Check the result of a round trip conversion: text => block => text
|
||||
textToBlock("./forth.txt", "./forth_2.blk");
|
||||
blockToText("./forth_2.blk", "./forth_2.txt");
|
||||
final boolean mismatch = ( Files.mismatch(Path.of("./forth.txt"), Path.of("./forth_2.txt")) > 0 );
|
||||
IO.println("Files matched? " + ! mismatch);
|
||||
}
|
||||
|
||||
// Read the lines of a text file and write them as 64 byte records
|
||||
private static void textToBlock(String inputFilePath, String outputFilePath) {
|
||||
try ( BufferedReader reader = Files.newBufferedReader(Path.of(inputFilePath));
|
||||
BufferedWriter writer = Files.newBufferedWriter(Path.of(outputFilePath)) ) {
|
||||
|
||||
int lineCount = 0;
|
||||
String line;
|
||||
while ( ( line = reader.readLine() ) != null ) {
|
||||
if ( line.length() > LINE_SIZE ) {
|
||||
line = line.substring(0, LINE_SIZE);
|
||||
} else if ( line.length() < LINE_SIZE ) {
|
||||
line += " ".repeat(LINE_SIZE - line.length());
|
||||
}
|
||||
|
||||
writer.write(line);
|
||||
lineCount += 1;
|
||||
}
|
||||
|
||||
// Complete the block with empty lines
|
||||
final int emptyLineCount = ( LINE_COUNT - lineCount % LINE_COUNT ) % LINE_COUNT;
|
||||
for ( int i = 0; i < emptyLineCount; i++ ) {
|
||||
writer.write(EMPTY_LINE);
|
||||
}
|
||||
} catch (IOException ioe) {
|
||||
ioe.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
// Read 64 bytes records and write them as lines with trailing spaces removed
|
||||
private static void blockToText(String inputFilePath, String outputFilePath) {
|
||||
try ( FileChannel reader = FileChannel.open(Path.of(inputFilePath), StandardOpenOption.READ);
|
||||
BufferedWriter writer = Files.newBufferedWriter(Path.of(outputFilePath)) ) {
|
||||
|
||||
ByteBuffer buffer = ByteBuffer.allocate(LINE_SIZE);
|
||||
|
||||
while ( reader.read(buffer) != END_OF_FILE ) {
|
||||
writer.write( new String(buffer.array()).stripTrailing() + System.lineSeparator() );
|
||||
buffer.flip();
|
||||
}
|
||||
} catch (IOException ioe) {
|
||||
ioe.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private static final int LINE_SIZE = 64;
|
||||
private static final int LINE_COUNT = 16;
|
||||
private static final int END_OF_FILE = -1;
|
||||
private static final String EMPTY_LINE = " ".repeat(LINE_SIZE);
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue