COSC 2425- Program Set #1
TextFileExamples/FileReadDemo.java
TextFileExamples/FileReadDemo.java
import
java
.
util
.
Scanner
;
// Needed for the Scanner class
import
java
.
io
.
*
;
// Needed for the File class
/**
This program reads data from a file.
*/
public
class
FileReadDemo
{
public
static
void
main
(
String
[]
args
)
throws
IOException
{
// Create a Scanner object for keyboard input.
Scanner
keyboard
=
new
Scanner
(
System
.
in
);
// Get the filename.
System
.
out
.
print
(
"Enter the filename: "
);
String
filename
=
keyboard
.
nextLine
();
// Open the file.
File
file
=
new
File
(
filename
);
Scanner
inputFile
=
new
Scanner
(
file
);
// Read lines from the file until no more are left.
while
(
inputFile
.
hasNext
())
{
// Read the next name.
String
friendName
=
inputFile
.
nextLine
();
// Display the last name read.
System
.
out
.
println
(
friendName
);
}
// Close the file.
inputFile
.
close
();
}
}
TextFileExamples/FileWriteDemo.java
TextFileExamples/FileWriteDemo.java
import
java
.
util
.
Scanner
;
// Needed for Scanner class
import
java
.
io
.
*
;
// Needed for File I/O classes
/**
This program writes data to a file.
*/
public
class
FileWriteDemo
{
public
static
void
main
(
String
[]
args
)
throws
IOException
{
String
filename
;
// File name
String
friendName
;
// Friend's name
int
numFriends
;
// Number of friends
// Create a Scanner object for keyboard input.
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.
PrintWriter
outputFile
=
new
PrintWriter
(
filename
);
// Get data and write it to the file.
for
(
int
i
=
1
;
i
<=
numFriends
;
i
++
)
{
// Get the name of a friend.
System
.
out
.
print
(
"Enter the name of friend "
+
"number "
+
i
+
": "
);
friendName
=
keyboard
.
nextLine
();
// Write the name to the file.
outputFile
.
println
(
friendName
);
}
// Close the file.
outputFile
.
close
();
System
.
out
.
println
(
"Data written to the file."
);
}
}
TextFileExamples/InputFile.cpp
TextFileExamples/InputFile.cpp
// This program lets the user enter a filename.
#include
<
iostream
>
#include
<
string
>
#include
<
fstream
>
using
namespace
std
;
int
main
()
{
ifstream inputFile
;
string filename
;
int
number
;
// Get the filename from the user.
cout
<<
"Enter the filename: "
;
cin
>>
filename
;
// Open the file.
inputFile
.
open
(
filename
);
// If the file successfully opened, process it.
if
(
inputFile
)
{
// Read the numbers from the file and
// display them.
while
(
inputFile
>>
number
)
{
cout
<<
number
<<
endl
;
}
// Close the file.
inputFile
.
close
();
}
else
{
// Display an error message.
cout
<<
"Error opening the file.\n"
;
}
return
0
;
}
TextFileExamples/OutputFile.cpp
TextFileExamples/OutputFile.cpp
// This program writes user input to a file.
#include
<
iostream
>
#include
<
fstream
>
// Needed to use files
#include
<
string
>
using
namespace
std
;
int
main
()
{
ofstream outputFile
;
string name
;
// Open the output file
outputFile
.
open
(
"Friends.txt"
);
// Use a loop to get the names of three friends
// and write each name in the output file
cout
<<
"Enter the names of three friends.\n"
;
for
(
int
count
=
1
;
count
<=
3
;
count
++
)
{
cout
<<
"Friend #"
<<
count
<<
": "
;
cin
>>
name
;
outputFile
<<
name
<<
endl
;
}
// Close the file
outputFile
.
close
();
cout
<<
"The names were saved to a file.\n"
;
return
0
;
}