//Chapter 13 (File Input and Output), Java Programming, joyce Farrell, 8th edit
ID: 3681539 • Letter: #
Question
//Chapter 13 (File Input and Output), Java Programming, joyce Farrell, 8th edit
//DebugThirteen2.java
//output, need to fix
// Program reads in a file of phone numbers without area codes
// inserts "(312) " in front of each phone number
// and produces an output file with the new complete phone numbers
import java.nio.file.*;
import java.io.*;
import java.nio.channels.FileChannel;
import java.nio.ByteBuffer;
import static java.nio.file.StandardOpenOption.*;
public class DebugThirteen2
{
public static void main(String[] args)
{
Path fileIn =
Paths.get(C:\Java\Chapter.13\DebugData3.txt);
Path fileOut =
Paths.get(C:\Java\Chapter.13\DebugData3New.txt);
String areaCode = "(312) ";
String phone;
InputStream input = null;
OutputStream output = null
try
{
input = Files.newInputStream(fileIn);
BufferedReader reader = new BufferedReader
(new InputStreamReader(input);
output = Files.newOutputStream(fileOut);
phone = input.readLine();
while(phone == null)
{
phone = areaCode + phone + System.getProperty("line.separator");
byte[] phoneBytes = phone.getBytes();
output.writeln(phoneBytes);
phone = reader.readline();
}
input.close();
}
catch (IOException e)
{
System.out.println(e);
}
}
}
------------------------------------------------
//DebugData1.txt
Somewhere over the rainbow.
-----------------------------------------------
//DebugData2.txt
There's no place like home.
Are you a good witch or a bad witch?t
---------------------------------------------------
//DebugData3.txt
435-9845
239-9845
981-9883
384-5656
875-3784
874-8120
----------------------------------------------------
//DebugData4.txt
steak,20
lobster,30
macaroni,8
spaghetti,10
Explanation / Answer
//store the file in the drive where the execution file exist
import java.nio.file.*;
import java.io.*;
import java.nio.channels.FileChannel;
import java.nio.ByteBuffer;
import java.io.FileReader;
import static java.nio.file.StandardOpenOption.*;
public class DebugThirteen2
{
public static void main(String[] args) throws IOException
{
Path fileIn =
Paths.get("DebugData3.txt");
Path fileOut =
Paths.get("DebugData3New.txt");
String areaCode = "(312) ";
String phone;
InputStream input = null;
OutputStream output = null;
try
{
input = Files.newInputStream(fileIn);
BufferedReader reader = new BufferedReader
(new InputStreamReader(input);
output = Files.newOutputStream(fileOut);
phone = input.readLine();
while(phone == null)
{
phone = areaCode + phone + System.getProperty("line.separator");
byte[] phoneBytes = phone.getBytes();
output.writeln(phoneBytes);
phone = reader.readline();
}
input.close();
}
catch (IOException e)
{
System.out.println(e);
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.