-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileIO.java
More file actions
29 lines (28 loc) · 1.22 KB
/
Copy pathFileIO.java
File metadata and controls
29 lines (28 loc) · 1.22 KB
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
29
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class FileIO {
public static void main(String[] args) {
String sentense = null;
try (FileInputStream fileInputStream = new FileInputStream("input.txt")) {
byte[] data = new byte[fileInputStream.available()];
System.out.println("Number of available bytes: " + fileInputStream.available());
sentense = new String(data);
int bytesRead = fileInputStream.read(data);
System.out.println("Number of bytes read: " + bytesRead);
String content = new String(data);
System.out.println("File content:\n" + content);
} catch (Exception e) {
e.printStackTrace();
} finally {
System.out.println("File operation completed.");
}
try (FileOutputStream fileOutputStream = new FileOutputStream("output.txt")) {
byte[] dataO = sentense.getBytes();
System.out.println("Number of bytes to write: " + dataO.length);
fileOutputStream.write(dataO);
System.out.println("Data written to output.txt successfully.");
} catch (Exception e) {
e.printStackTrace();
}
}
}