-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWriteStudentData.java
More file actions
25 lines (22 loc) · 940 Bytes
/
Copy pathWriteStudentData.java
File metadata and controls
25 lines (22 loc) · 940 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.util.Scanner;
public class WriteStudentData {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter student name: ");
String name = scanner.nextLine();
System.out.print("Enter roll number: ");
String rollNumber = scanner.nextLine();
System.out.print("Enter grade: ");
String grade = scanner.nextLine();
try (FileWriter writer = new FileWriter("student.txt", true)) { // Append mode
writer.write("Name: " + name + ", Roll Number: " + rollNumber + ", Grade: " + grade + "\n");
System.out.println("Student data successfully written to file.");
} catch (IOException e) {
System.out.println("Error writing to file.");
} finally {
scanner.close();
}
}
}