Last active
July 9, 2018 14:28
-
-
Save steklopod/c8bdafecf4a916914a321dcfdcba7542 to your computer and use it in GitHub Desktop.
Со сравнением
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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