-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileIOTest2.java
More file actions
25 lines (22 loc) · 864 Bytes
/
FileIOTest2.java
File metadata and controls
25 lines (22 loc) · 864 Bytes
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
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
public class FileIOTest2 {
public static void main(String[] args) throws IOException {
String content = "Hello, this is a test for file writing!";
char[] charArray = content.toCharArray();
try (Writer writer = new FileWriter("output.txt")) {
writer.write(charArray);
int writtenCnt = charArray.length;
System.out.println("Written characters: ");
for (int i = 0; i < writtenCnt; i++) {
System.out.print(charArray[i]);
}
System.out.println();
System.out.println("writtenCnt = " + writtenCnt);
} catch (IOException e) {
System.out.println("An error occurred while writing to the file.");
e.printStackTrace();
}
}
}