Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save steklopod/c8bdafecf4a916914a321dcfdcba7542 to your computer and use it in GitHub Desktop.
Save steklopod/c8bdafecf4a916914a321dcfdcba7542 to your computer and use it in GitHub Desktop.
Со сравнением
package com.javarush.task.task32.task3210;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
/*
Используем RandomAccessFile
*/
//Считать текст с файла начиная с позиции number, длинной такой же как и длинна переданного текста в третьем параметре.
//Если считанный текст такой же как и text, то записать в конец файла строку ‘true‘, иначе записать ‘false‘.
public class Solution {
public static void main(String... args) throws IOException {
String fileName = args[0];
int number = Integer.parseInt(args[1]);
String text = args[2];
try (RandomAccessFile file = new RandomAccessFile(fileName, "rw")) {
byte[] buf = new byte[text.length()];
file.seek(number);
file.read(buf, 0, buf.length);
String lineFromFile = convertByteToString(buf);
String storeToFile = lineFromFile.equals(text) ? "true" : "false";
file.seek(file.length());
file.write(storeToFile.getBytes());
}
}
public static String convertByteToString(byte readBytes[]) {
//return new String(readBytes, StandardCharsets.UTF_8);
return new String(readBytes);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment