Question

In: Computer Science

Java Programming: Using textI/O models, create a Write file, Append file, and a Read file. Write...

Java Programming:

Using textI/O models, create a Write file, Append file, and a Read file. Write 10 fahrenheit temperature to a text file, append 5 more temperatures, then read these numbers calculating, in a method, the Celsius temperature, and printing the results via another method.

Solutions

Expert Solution

// Program in JAVA

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

class Main
{
//main function
public static void main (String[] args) throws FileNotFoundException, IOException
{
// variables for path of the files
String readFilePath = "C:\\\\Users\\\\shubham\\\\Desktop\\\\Read.txt";
String writeFilePath = "C:\\\\Users\\\\shubham\\\\Desktop\\\\Write.txt";
String appendFilePath = "C:\\\\Users\\\\shubham\\\\Desktop\\\\Append.txt";
  
// create read, write & append files
File readFile = new File(readFilePath);
File writeFile = new File(writeFilePath);
File appendFile = new File(appendFilePath);

// check if files are created and display the prompt accordingly
if (readFile.createNewFile() && writeFile.createNewFile() && appendFile.createNewFile())
{
System.out.println("Files are created!");
}
else
{
System.out.println("One(or more) file(s) already exist!");
}
  
//Write Content read from readFile to writeFile
FileWriter writer = new FileWriter(writeFile);
// store the readFileFun() returned string in a variable
String temp = readFileFun(readFilePath);
// split the string by newline
String lines[] = temp.split("\n");
// write each line to file
int i=0;
while(i!=lines.length)
{
writer.write(lines[i] + "\n");
i++;
}
// close the file
writer.close();
  
//Write Content read from appendFile to writeFile by setting second parameter to true
FileWriter append = new FileWriter(writeFile, true);
// create new printwriter object
PrintWriter pw = new PrintWriter(append);
// append to file using that object
pw.println(readFileFun(appendFilePath));
// close the file
pw.close();
  
//Now read the data from the readfile to display the converted temperature on the screen
String readData = readFileFun(readFilePath);
// split the string by newline
String eachLines[] = readData.split("\n");
i=0;
while(i != eachLines.length)
{
System.out.println("Original temperature(in Fahrenheit): " + eachLines[i]
+ " Converted temperature(in Celsius): " + ((Integer.valueOf(eachLines[i])-32)*5/9));
i++;
}
}
  
// function to readFile from a file with path being provided as a parameter
private static String readFileFun(String pathOfFile)
{
// create a new StringBuilder object to build a string by appending read data
StringBuilder sb = new StringBuilder();
// try reading the file by bufferedreader object
try (BufferedReader br = new BufferedReader(new FileReader(pathOfFile)))
{
// variable to store each line
String line;
while ((line = br.readLine()) != null)
{
// append each line to the stringbuilder object
sb.append(line).append("\n");
}
}
// catch exception if file is not found
catch (IOException e)
{
e.printStackTrace();
}
// return the object as string
return sb.toString();
}
}

// Contents of Read.txt

40
44
88
144
56
75
68
94
55
66

// Contents of Append.txt

65
55.5
77.7
45.6
89.9

// Contents of Write.txt (Note that this may not appear formatted(with newline) in notepad, try better editors for desired formatting)

40
44
88
144
56
75
68
94
55
66
65
55.5
77.7
45.6
89.9

// Output

One(or more) file(s) already exist!
Original temperature(in Fahrenheit): 40 Converted temperature(in Celsius): 4
Original temperature(in Fahrenheit): 44 Converted temperature(in Celsius): 6
Original temperature(in Fahrenheit): 88 Converted temperature(in Celsius): 31
Original temperature(in Fahrenheit): 144 Converted temperature(in Celsius): 62
Original temperature(in Fahrenheit): 56 Converted temperature(in Celsius): 13
Original temperature(in Fahrenheit): 75 Converted temperature(in Celsius): 23
Original temperature(in Fahrenheit): 68 Converted temperature(in Celsius): 20
Original temperature(in Fahrenheit): 94 Converted temperature(in Celsius): 34
Original temperature(in Fahrenheit): 55 Converted temperature(in Celsius): 12
Original temperature(in Fahrenheit): 66 Converted temperature(in Celsius): 18

// Screenshot of Code & Output (for reference to indentation)

(Thank You!!)


Related Solutions

Write a java program that can create, read, and append a file. Assume that all data...
Write a java program that can create, read, and append a file. Assume that all data written to the file are string type. One driver class and a class that contains the methods. The program should have three methods as follows: CreateFile - only creating a file. Once it create a file successfully, it notifies to the user that it creates a file successfully. ReadingFile - It reads a contents of the file. This method only allow to read a...
how to read, write and append a text file using a switch case in java.I need...
how to read, write and append a text file using a switch case in java.I need help with the code.
Using the Java programming language: Create and implement a class Car that models a car. A...
Using the Java programming language: Create and implement a class Car that models a car. A Car is invented to have a gasoline tank that can hold a constant max of 12.5 gallons, and an odometer that is set to 0 for a new car. All cars have an original fuel economy of 23.4 miles per gallon, but the value is not constant. Provide methods for constructing an instance of a Car (one should be zero parameter, and the other...
Using Java create an application that will read a tab-delimited file that displays the MLB standings...
Using Java create an application that will read a tab-delimited file that displays the MLB standings Below: League AL East Tampa Bay 37 20 NY Yankees 32 24 Toronto 29 27 Baltimore 23 33 Boston 22 34 League AL Central Minnesota 35 22 Chi White Sox 34 23 Cleveland 33 24 Kansas City 23 33 Detroit 22 32 League AL West Oakland 34 21 Houston 28 28 LA Angels 26 31 Seattle 25 31 Texas 19 37 League NL East...
Answer the following in Java programming language Create a Java Program that will import a file...
Answer the following in Java programming language Create a Java Program that will import a file of contacts (contacts.txt) into a database (Just their first name and 10-digit phone number). The database should use the following class to represent each entry: public class contact {    String firstName;    String phoneNum; } Furthermore, your program should have two classes. (1) DatabaseDirectory.java:    This class should declare ArrayList as a private datafield to store objects into the database (theDatabase) with the...
Java Programming create a program that calculates the average of the prime values in the file....
Java Programming create a program that calculates the average of the prime values in the file. Process: Read Lines from "Values.txt" Determine if the line is an integer Determine if the integer is prime Keep running average of primes Print running average of primes (no formatting necessary "Values.txt" 751 1090 971 1054 7 300 19 834 100 751 549 288 641 989 479 1388 1319 584 Code Outline: public class ChapterSixMain {    public static void main(String[] args) {   ...
This for Java Programming Write a java statement to import the java utilities. Create a Scanner...
This for Java Programming Write a java statement to import the java utilities. Create a Scanner object to read input. int Age;     Write a java statement to read the Age input value 4 . Redo 1 to 3 using JOptionPane
Please write a java program to write to a text file and to read from a...
Please write a java program to write to a text file and to read from a text file.
Read previously created binary file using ObjectInputStream Create an appropriate instance of HashMap from Java library...
Read previously created binary file using ObjectInputStream Create an appropriate instance of HashMap from Java library Populate the HashMap with data from binary file Display the information read in a user friendly format Problem: Given: Words.dat – a binary file consisting of all the words in War and Peace Goal: Read the words in from the binary file and figure out how many times each word appears in the file. Display the results to the user. Use a HashMap with...
Using Java Summary Create a Loan class, instantiate and write several Loan objects to a file,...
Using Java Summary Create a Loan class, instantiate and write several Loan objects to a file, read them back in, and format a report. Project Description You’ll read and write files containing objects of the Loan class. Here are the details of that class: Instance Variables: customer name (String) annual interest percentage (double) number of years (int) loan amount (double) loan date (String) monthly payment (double) total payments (double) Methods: getters for all instance variables setters for all instance variables...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT