1ое Переделанное
unknown
java
3 years ago
1.8 kB
8
Indexable
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.nio.file.Paths;
public class BufferTask {
public static void RefactorFile() throws IOException {
Path inputFilePath = Paths.get("File.txt");
Path outputFilePath = Paths.get("newFile.txt");
try (FileChannel inputChannel = FileChannel.open(inputFilePath);
FileChannel outputChannel = FileChannel.open(outputFilePath)) {
ByteBuffer inputBuffer = ByteBuffer.allocate(1024);
ByteBuffer outputBuffer = ByteBuffer.allocate(1024);
while (inputChannel.read(inputBuffer) != -1 || inputBuffer.position() > 0) {
inputBuffer.flip();
StringBuilder modifiedData = new StringBuilder();
while (inputBuffer.hasRemaining()) {
byte b = inputBuffer.get();
if (Character.isLetter(b) || b == ' ') {
modifiedData.append((char) b);
}
}
outputBuffer.clear();
outputBuffer.put(modifiedData.toString().getBytes(StandardCharsets.UTF_8));
outputBuffer.flip();
while (outputBuffer.hasRemaining()) {
outputChannel.write(outputBuffer);
}
inputBuffer.compact();
}
}
}
public static void main(String[] args) {
try {
RefactorFile();
System.out.println("Файл успешно преобразован.");
} catch (IOException e) {
e.printStackTrace();
}
}
}
Editor is loading...