Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

How to open and use these files on without using a filepath? import java.io.Buff

ID: 3814930 • Letter: H

Question

How to open and use these files on without using a filepath?

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.io.InputStream;
import java.io.InputStreamReader;

  
public class Main {

public static void main(String[] args) {
// TODO Auto-generated method stub

  
// Files to read files
// Put files on your desktop and direct to those files, I couldnt find a way to get around this


(i Need to fix this so that it doesnt use a filepath to find these files)


File preferredFile=new File("C:/Users/bob/Desktop/preferred.dat");
File customersFile=new File("C:/bob/najee/Desktop/customer.dat");
File ordersFile=new File("C:/Users/bob/Desktop/orders.dat");

  
  
Customer[] customers;
PreferredCustomer[] preferredCustomers;
  
//Reading customers file
  
// Readers and important variables
BufferedReader br=null;
BufferedWriter bw=null;
String[] fileData=null;
int count=0;
  
// Checking weather the file exists or not
if(customersFile.exists() && preferredFile.exists())
{
  
try
{
br=new BufferedReader(new FileReader(customersFile));
  
// To count number of customers to declare array size
while(br.readLine()!=null)
{
count++;
}
  
br.close();
br=new BufferedReader(new FileReader(customersFile));
  
// Array size;
customers=new Customer[count];
count=0;
  
String line;
  
  
// Reading to array
  
while((line=br.readLine())!=null)
{
fileData=line.split(" +");
  
customers[count]=new Customer(fileData[1],fileData[2],Integer.parseInt(fileData[0]),Double.parseDouble(fileData[3]));
count++;
}
count=0;
  
  
// Reading preferred customers file
br.close();
br=new BufferedReader(new FileReader(preferredFile));
// Reading number of preferred customers
while(br.readLine()!=null)
{
count++;
}
  
preferredCustomers=new PreferredCustomer[count];
br.close();
br=new BufferedReader(new FileReader(preferredFile));
count=0;
while((line=br.readLine())!=null)
{
fileData=line.split(" +");
preferredCustomers[count]=new PreferredCustomer(fileData[1],fileData[2],Integer.parseInt(fileData[0]),Double.parseDouble(fileData[3]),Double.parseDouble(fileData[4]));
count++;
}
count=0;
  
int customerID;
double radius;
double height;
double ounces;
double ounceprice;
double squareinchprice;
int quantity;
  
double totalCost;
// Processing here based on the order of customers
br.close();
  
br=new BufferedReader(new FileReader(ordersFile));
  
while((line=br.readLine())!=null)
{
fileData=line.split(" +");
customerID=Integer.parseInt(fileData[0]);
radius=Double.parseDouble(fileData[1]);
height=Double.parseDouble(fileData[2]);
ounces=Double.parseDouble(fileData[3]);
ounceprice=Double.parseDouble(fileData[4]);
squareinchprice=Double.parseDouble(fileData[5]);
quantity=Integer.parseInt(fileData[6]);
  

totalCost=3.14*radius*radius*height*squareinchprice*ounces*ounceprice;
  
if(totalCost>=150 && totalCost<200)
{
// Checkig customer existance in customers file
for(int i=0;i<customers.length;i++)
{
if(customers[i].getGuestID()==customerID)
{
  
//Resizing preferredCustomer array
PreferredCustomer[] tempP=new PreferredCustomer[preferredCustomers.length+1];
for(int j=0;j<preferredCustomers.length;j++)
{
tempP[j]=preferredCustomers[j];
}
tempP[preferredCustomers.length]=new PreferredCustomer(customers[i].first_name,customers[i].last_name,customers[i].guestID,customers[i].amountSpent,0.05);
  
  
  
//Resizing customers array
Customer[] tempC=new Customer[customers.length-1];
int index=0;
for(int j=0;j<customers.length;j++)
{
if(customers[i].guestID!=customers[j].guestID)
{
tempC[index]=customers[j];
index++;
}
}
  
break;
  
  
}
}
  
  
}
else if(totalCost>=200 && totalCost<350)
{
// Checkig customer existance in customers file
for(int i=0;i<customers.length;i++)
{
if(customers[i].getGuestID()==customerID)
{
  
//Resizing preferredCustomer array
PreferredCustomer[] tempP=new PreferredCustomer[preferredCustomers.length+1];
for(int j=0;j<preferredCustomers.length;j++)
{
tempP[j]=preferredCustomers[j];
}
tempP[preferredCustomers.length]=new PreferredCustomer(customers[i].first_name,customers[i].last_name,customers[i].guestID,customers[i].amountSpent,0.07);
  
  
  
//Resizing customers array
Customer[] tempC=new Customer[customers.length-1];
int index=0;
for(int j=0;j<customers.length;j++)
{
if(customers[i].guestID!=customers[j].guestID)
{
tempC[index]=customers[j];
index++;
}
}
  
break;
  
  
}
}
  
  
}
else
{
// Checkig customer existance in customers file
for(int i=0;i<customers.length;i++)
{
if(customers[i].getGuestID()==customerID)
{
  
//Resizing preferredCustomer array
PreferredCustomer[] tempP=new PreferredCustomer[preferredCustomers.length+1];
for(int j=0;j<preferredCustomers.length;j++)
{
tempP[j]=preferredCustomers[j];
}
tempP[preferredCustomers.length]=new PreferredCustomer(customers[i].first_name,customers[i].last_name,customers[i].guestID,customers[i].amountSpent,0.1);
  
  
  
//Resizing customers array
Customer[] tempC=new Customer[customers.length-1];
int index=0;
for(int j=0;j<customers.length;j++)
{
if(customers[i].guestID!=customers[j].guestID)
{
tempC[index]=customers[j];
index++;
}
}
  
break;
  
  
}
}
  
  
}
}
  
  
//Writing data to the files
customersFile.delete();
preferredFile.delete();
  
PrintWriter pw=new PrintWriter(new FileWriter(customersFile));
for(int i=0;i<customers.length;i++)
{
pw.println(customers[i].guestID+" "+customers[i].first_name+" "+customers[i].amountSpent);
}
pw.close();
  
pw=new PrintWriter(new FileWriter(preferredFile));
for(int i=0;i<preferredCustomers.length;i++)
{
pw.println(preferredCustomers[i].guestID+" "+preferredCustomers[i].first_name+" "+preferredCustomers[i].amountSpent+" "+preferredCustomers[i].discountPercentage);
}
pw.close();
  
  
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
else
{
System.out.println("Files does not exists");
}
}

}

Explanation / Answer

To make this code work without specifying the file path is:

You have to manually keep your files in the folder where the java compiler is. Which means, you should place the file in the bin folder of the java compiler. Probably, the path will be: C:/Programs/Java/javac/bin/

The File() method will read a file from which you specified. Usually, if you want your program to access a file, the file path should be either specified by you exclusively, or should be the default path. The default path is the java compiler path. So, all you have to do is, just copy the files preferred.dat, customer.dat, orders.dat from the desktop into your java bin folder, so that when writing your code, you need not specify the abosolute path, and will simply specify the file name and now the commands will look:

File preferredFile=new File("preferred.dat");
File customersFile=new File("customer.dat");
File ordersFile=new File("orders.dat");

  

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote