IT 2650 Java File Creation

Verified

Added on  2019/09/16

|3
|561
|65
Homework Assignment
AI Summary
This Java program, created for IT 2650, demonstrates file handling by creating two files, 'InStateCusts.txt' and 'OutOfStateCusts.txt', based on customer state. The program prompts the user for customer details, including ID, name, state, and balance, and writes this data into the appropriate file using file channels and buffered writers. The program also includes error handling and uses a loop to allow multiple customer entries until the user enters '999' to quit. The solution includes methods for creating empty files and formatting data before writing to the files.
Document Page
// Micheal Seals
// IT 2650, Java Programing 85072
// Due Date: November 17 2016
// Asssignment 7
import java.nio.file.*;
import java.io.*;
import java.nio.channels.FileChannel;
import java.nio.ByteBuffer;
import static java.nio.file.StandardOpenOption.*;
import java.util.Scanner;
import java.text.*;
public class CreateFilesBasedOnState
{
public static void main(String[] args)
{
Scanner input= new Scanner(System.in);
Path inStateFile=Paths.get("InStateCusts.txt");
Path outOfStateFile= Paths.get("OutOfStateCusts.txt");
final String ID_FORMAT= "000";
final String NAME_FORMAT= " ";
final int NAME_LENGTH= NAME_FORMAT.length();
final String HOME_STATE= "WI";
final String BALANCE_FORMAT= "0000.00";
String delimiter= ",";
String s= ID_FORMAT + delimiter + NAME_FORMAT + delimiter + HOME_STATE +
delimiter +
BALANCE_FORMAT + System.getProperty("line.separator");
final int RECSIZE=s.length();
FileChannel fcIn= null;
FileChannel fcOut= null;
String idString;
int id;
String name;
String state;
double balance;
final String QUIT="999";
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
createEmptyFile(inStateFile, s);
createEmptyFile(outOfStateFile, s);
try
{
fcIn=(FileChannel)Files.newByteChannel(inStateFile, CREATE, WRITE);
fcOut=(FileChannel)Files.newByteChannel(outOfStateFile, CREATE, WRITE);
System.out.print("Enter customer account number >> ");
idString= input.nextLine();
while(!(idString.equals(QUIT)))
{
id= Integer.parseInt(idString);
System.out.print("Enter name for customer >> ");
name=input.nextLine();
StringBuilder sb= new StringBuilder(name);
sb.setLength(NAME_LENGTH);
name=sb.toString();
System.out.print("Enter state >> ");
state=input.nextLine();
System.out.print("Enter a balance >> ");
balance= input.nextDouble();
input.nextLine();
DecimalFormat df= new DecimalFormat(BALANCE_FORMAT);
s=idString + delimiter + name + delimiter + state + delimiter + df.format(balance) +
System.getProperty("line.separator");
byte data[]= s.getBytes();
ByteBuffer buffer= ByteBuffer.wrap(data);
if(state.equals(HOME_STATE))
{
fcIn.position(id * RECSIZE);
fcIn.write(buffer);
}
else
{
fcOut.position(id * RECSIZE);
fcOut.write(buffer);
}
System.out.print("Enter next customer account number or " + QUIT + " to quit >> ");
idString=input.nextLine();
Document Page
}
fcIn.close();
fcOut.close();
}
catch(Exception e)
{
System.out.print("Error message: " + e);
}
}
public static void createEmptyFile(Path file, String s)
{
final int NUMRECS= 1000;
try
{
OutputStream outputStr= new BufferedOutputStream(Files.newOutputStream(file,
CREATE));
BufferedWriter writer= new BufferedWriter(new OutputStreamWriter(outputStr));
for(int count=0; count < NUMRECS; ++count)
writer.write(s, 0, s.length());
writer.close();
}
catch(Exception e)
{
System.out.println("Error message: " + e);
}
}
}
chevron_up_icon
1 out of 3
circle_padding
hide_on_mobile
zoom_out_icon
[object Object]