-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileWriteDemo.java
42 lines (31 loc) · 914 Bytes
/
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
/*
name: Jamie Johnson
Assignment:
Problem:
*/
import java.util.Scanner;
import java.io.*;
public class FileWriteDemo
{
public static void main(String args[]) throws IOException
{
String filename;
String friend_name;
int num_friends;
Scanner keyboard = new Scanner(System.in);
System.out.print("How many friends do you have? ");
num_friends = keyboard.nextInt();
keyboard.nextLine();
System.out.print("Enter a file name: ");
filename = keyboard.nextLine();
PrintWriter outputFile = new PrintWriter(filename);
for ( int i = 1; i <= num_friends; i++)
{
System.out.print("Enter the name of friend number " + i + ": ");
friend_name = keyboard.nextLine();
outputFile.println(friend_name);
}
outputFile.close();
System.out.println("Data written to file.");
}
}