In this lab you will experiment with FileReader / FileWriter objects, as well as
ID: 3816784 • Letter: I
Question
In this lab you will experiment with FileReader / FileWriter objects, as well as StreamReader / StreamWriter objects. Create an application project. Accept all of the defaults except Project Location. Go to File view on your project and copy UnixDBAEssentials.pdf into your Lab4 folder. Return to the Project view.
Let’s start with some simple File objects. First, create a File object (in) for an existing file, “UnixDBAEssentials.pdf”. Then create a couple File objects for files that don’t exist: “WriterTest.pdf” (wOut) and “StreamTest.pdf” (sOut). Use the File methods to see if the DBA Essentials file exists and if we can read from it. Write the results of these queries to Standard Out.
Then create a BufferedReader object for “UnixDBAEssentials.pdf”, and a BufferedWriter object for the other file. Also create a char buffer to hold the reads and writes. Declare all variables outside of the try/catch block that you will eventually need. The actual constructors will occur within a try/catch block.
char[] cBuffer = new char[2048]; // 2K char array to hold our IO
bw =new BufferedWriter(new FileWriter(wOut));
br = new BufferedReader(new FileReader(in));
We will use the int read(char[] cbuf, int off, int len)method of the Reader to read our data, and the void write(char[] cbuf, int off, int len) of the Writer object to output our data.
Read from the input file and write to the output file. Close both files when you’re done and check your directory. The output file should have the same length as the input file. Try to open the file that you created and see what happens.
Now add the code to create a couple of Stream objects to read and write the same file. Change the buffer from char to byte. Use a try with resources for this part.
byte[] bBuffer = new byte[2048]; // this will be our array for output
Resources:
bis = new BufferedInputStream(new FileInputStream(in));
bos = new BufferedOutputStream(new FileOutputStream(sOut));
We will use the int read(byte[] b, int off, int len) to read the file, and the void write(byte[] b, int off, int len) to write the file.
Try the appropriate read/write code in order to copy the file. Once again, try to open the output file.
Submit your completed code, along with a comment that :
1.shows the size of each of your output files
2.explains why they’re different
3.Try to open each file and explain why one opens and one doesn’t
I already have the code done, I just need help with the comment part.
Explanation / Answer
/*The above compiler command produces an object file, usually named main.o, from the source file main.c*/
/*
Create file if not exist. If file exits, clear its content:
/* File methods to see if the DBA Essentials file exists and if we can read from it. */
he "ls" command lists all files and directories in the specified directory. If no location is defined it acts on the current directory.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.