CodeGym /Courses /Java Collections /Tasks | Level 2 | Lesson 2

Tasks | Level 2 | Lesson 2

Java Collections
Level 2 , Lesson 2
Available

"Hi, Amigo!"

14
Task
Java Collections, level 2, lesson 2
Locked
Writing to an existing file
The main() method accepts three arguments: 1) fileName - the path to a file; 2) number - a position in the file; 3) text - the text. Write text to the fileName file starting at the position indicated by number. The write operation must overwrite the old data in the file.
14
Task
Java Collections, level 2, lesson 2
Locked
Using RandomAccessFile
The main() method accepts three arguments: 1) fileName - the path to a file; 2) number - a position in the file; 3) text - the text. Read text from the file, starting from the position indicated by number. The length is the same as the length of the text passed as the third argument.
Comments (3)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Ruslan Skaldin Level 33, Tashkent, Uzbekistan
6 December 2020
Don't know why but when you compare two string it only accepts result with this construction:

if (text.equals(new String(textInFile))) { ... }
This one doesn't work:

if (text.equals(Arrays.toString(textInFile))) { ... }
Justin Smith Level 41, Greenfield, USA, United States
19 August 2022
I didn't even compare them in String form. What I used:

if(Arrays.equals(readBytes, text.getBytes())
I was able to pass using this comparison.
Ewerton Level 30, Belo Horizonte, Brasil
8 July 2019
Writing to existing file: "If the file is too short", means that number will be bigger than your file length. Using RandomAcessFile - helpful example:

test.txt: Hi, good luck
String[] args = {"C:\test.txt",4,"good"}
Output:

Hi, good lucktrue