Create a text file named first.txt and write your first name in the file. You wi
ID: 3693251 • Letter: C
Question
Create a text file named first.txt and write your first name in the file. You will be reading the name of the file from the keyboard as a string, using the string class. Your program will also read your first name from the keyboard. The process of the file creation (name of the file, mode for opening the file, and testing the open success is done in a user-defined function. The function communicates the status of file open to the calling function).
Create a text file named last.txt and write your last name in the file. You will be reading the name of the file from the keyboard as a string, using the string class. Your program will also read your last name from the keyboard. The process of the file creation (name of the file, mode for opening the file and testing the open success is done in a user-defined function. The function communicates the status of file open to the calling function)
Create a text file named flast.txt and write your first name from the file first.txt, and your last name from the file last.txt. You will be reading the name of the file from the keyboard as a string, using the string class. The process of the file creation (name of the file, mode for opening the file and testing the open success is done in a user-defined function. The function communicates the status of file open to the calling function)
At this time, each file will have one line of text in it.
Prepare the files for reading.
Display the results of the three files in the order shown below:
first.txt your first name as written in the file
last.txt your last name as written in the file
flast.txt yourfirstname yourlastname
Note that there is a single space between yourfirstname and yourlastname when you display the contents of the file flast.txt.
You will use a user-defined function to display all the files at once or one at a time.
Close all files. You will use a user-defined function that closes each file separately or all at once.
Run the program again and use your choice of the filenames, and different names, different than what you used above and repeat the process
Explanation / Answer
import java.io.*; class Test{ public static void main(String[] args) { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); try { System.out.println("Enter first name"); String fname = br.readLine(); System.out.println("Enter last name"); String lname = br.readLine(); File file1 = new File("first.txt"); File file2 = new File("last.txt"); if (!file1.exists()) { file1.createNewFile(); } if (!file2.exists()) { file2.createNewFile(); } FileWriter fw1 = new FileWriter(file1.getAbsoluteFile()); BufferedWriter bw1 = new BufferedWriter(fw1); bw1.write(fname); bw1.close(); FileWriter fw2 = new FileWriter(file2.getAbsoluteFile()); BufferedWriter bw2 = new BufferedWriter(fw2); bw2.write(lname); bw2.close(); } catch (IOException e) { e.printStackTrace(); } try { String fname,lname; br = new BufferedReader(new FileReader("first.txt")); fname = br.readLine(); br = new BufferedReader(new FileReader("last.txt")); lname = br.readLine(); File file3 = new File("flast.txt"); if (!file3.exists()) { file3.createNewFile(); } FileWriter fw3 = new FileWriter(file3.getAbsoluteFile()); BufferedWriter bw3 = new BufferedWriter(fw3); bw3.write(fname+" "+lname); bw3.close(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (br != null)br.close(); } catch (IOException ex) { ex.printStackTrace(); } } } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.