Program reads in a file of phone numbers without area codes

// 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);
}
}
}

Solution

Hi,

I have fixed the code. Highlighted the code changes below

DebugThirteen2.java

import java.nio.file.*;
import java.io.*;
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 = reader.readLine();
while(phone != null)

{
phone = areaCode + phone + System.getProperty(\"line.separator\");
byte[] phoneBytes = phone.getBytes();
output.write(phoneBytes);
phone = reader.readLine();
}
input.close();
}
catch (IOException e)
{
System.out.println(e);
}
}
}

// Program reads in a file of phone numbers without area codes // inserts \
// Program reads in a file of phone numbers without area codes // inserts \

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site