forked from rakhi2207/java-programs-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRandomEx.java
29 lines (25 loc) · 1011 Bytes
/
RandomEx.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import java.io.*;
public class RandomEx {
static String FILEPATH ="C:/java programs/name7.txt";
public static void main(String[] args) throws IOException {
writeToFile(FILEPATH, "Computer World is a creative world", 31);
String k= String.valueOf(readFromFile(FILEPATH, 16, 18));
System.out.println(k);
}
private static byte[] readFromFile(String filePath, int position, int size)
throws IOException {
RandomAccessFile file = new RandomAccessFile(filePath, "r");
file.seek(position);
byte[] bytes = new byte[size];
file.read(bytes);
file.close();
return bytes;
}
private static void writeToFile(String filePath, String data, int position)
throws IOException {
RandomAccessFile file = new RandomAccessFile(filePath, "rw");
file.seek(position);
file.write(data.getBytes());
file.close();
}
}