-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileWriteDemo.java
48 lines (34 loc) · 1.17 KB
/
FileWriteDemo.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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import jdk.internal.cmm.SystemResourcePressureImpl;
import java.io.FileWriter;
import java.util.Scanner;
import java.io.PrintWriter;
import java.io.IOException;
public class FileWriteDemo
{
public static void main(String[] args) throws IOException
{
String filename;
String friendName;
int numFriends;
Scanner keyboard = new Scanner(System.in);
// Get the number of friends.
System.out.print("How many friends do you have? ");
numFriends = keyboard.nextInt();
// Consume the remaining newline character.
keyboard.nextLine();
// Get the filename.
System.out.print("Enter the filename: ");
filename = keyboard.nextLine();
// Open the file.
FileWriter fw = new FileWriter(filename, true);
PrintWriter outputFile = new PrintWriter(fw);
for(int i =1 ; i<= numFriends; i++)
{
System.out.print("Enter the name of friend" + "number" + i + ":");
friendName = keyboard.nextLine();
outputFile.println(friendName);
}
outputFile.close();
System.out.print("Data written to the file.");
}
}