Skip to content

Commit fdcd75d

Browse files
authored
joy that a man is born into the world
A woman when she is in travail hath sorrow, because her hour is come: but as soon as she is delivered of the child, she remembereth no more the anguish, for joy that a man is born into the world. (John 16:21)
1 parent a9ca547 commit fdcd75d

1 file changed

Lines changed: 75 additions & 0 deletions

File tree

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
2+
//A woman when she is in travail hath sorrow, because her hour is come: but as soon as she is delivered of the child, she remembereth no more the anguish, for joy that a man is born into the world. (John 16:21)
3+
4+
package com.javarush.task.task32.task3210;
5+
6+
import java.io.IOException;
7+
import java.io.RandomAccessFile;
8+
9+
/*
10+
Используем RandomAccessFile
11+
*/
12+
13+
public class Solution {
14+
public static void main(String... args) throws IOException {
15+
String fileName = args[0];
16+
int number = Integer.parseInt(args[1]);
17+
String text = args[2];
18+
19+
try (RandomAccessFile file = new RandomAccessFile(fileName, "rw")) {
20+
byte[] buf = new byte[text.length()];
21+
file.seek(number);
22+
file.read(buf, 0, buf.length);
23+
String lineFromFile = convertByteToString(buf);
24+
String storeToFile = lineFromFile.equals(text) ? "true" : "false";
25+
file.seek(file.length());
26+
file.write(storeToFile.getBytes());
27+
}
28+
}
29+
30+
31+
public static String convertByteToString (byte readBytes[]) {
32+
//return new String(readBytes, StandardCharsets.UTF_8);
33+
return new String(readBytes);
34+
}
35+
}
36+
37+
/*
38+
Используем RandomAccessFile
39+
40+
В метод main приходят три параметра:
41+
42+
1) fileName - путь к файлу;
43+
44+
2) number - число, позиция в файле;
45+
46+
3) text - текст.
47+
48+
49+
50+
Считать текст с файла начиная с позиции number, длинной такой же как и длинна переданного текста в третьем параметре.
51+
52+
Если считанный текст такой же как и text, то записать в конец файла строку 'true', иначе записать 'false'.
53+
54+
Используй RandomAccessFile и его методы seek(long pos), read(byte b[], int off, int len), write(byte b[]).
55+
56+
Используй convertByteToString(byte readBytes[]) для конвертации считанной строчки в текст.
57+
58+
59+
60+
61+
62+
Требования:
63+
64+
1. В методе main класса Solution необходимо использовать RandomAccessFile, который должен использовать файл, который приходит первым параметром.
65+
66+
2. В методе main класса Solution программа должна устанавливать позицию в файле, которая передана во втором параметре.
67+
68+
3. В методе main класса Solution программа должна считывать данные с файла при помощи метода read(byte b[], int off, int len).
69+
70+
4. Запись должна происходить в конец файла.
71+
72+
5. Если считанный текст такой же как и text, то программа должна записать в конец переданного файла строку 'true'.
73+
74+
6. Если считанный текст НЕ такой же как и text, то программа должна записать в конец переданного файла строку 'false'.
75+
*/

0 commit comments

Comments
 (0)