Question

In: Computer Science

Consider the following code used to read a file. byte[] buffer = new byte[BUFFER_SIZE]; String str...

Consider the following code used to read a file.

        byte[] buffer = new byte[BUFFER_SIZE];
        String str = "";
        try {
            while (true) {
                int read = infile.read(buffer);
                if (read <= 0) {
                    break;
                }

                str += new String(buffer, 0, read);
            }
        } catch (IOException ex) {
            return;
        }

Consider if BUFFER_SIZE is set to 1000 and the the file is 2775 bytes long.

How many times will infile.read() be called?
What value will infile.read() return on the next to last time it is called?
What value will infile.read() return on the last time it is called?

Solutions

Expert Solution

In the above program. Buffer is an array of type Byte with 1000 elements in it. infile.read() returns the total number of bytes read from the file since the file is 2775 Bytes long ,when we read using buffer Bytes array we will get 1000 bytes in first iteration, another 1000 in next iteration , 775 in second last iteration , and 0 in the final iteration
1) Hence the inFile.read() function will be called a total 4 times
2) The infile.read() will return 775 in the next to last time
3) The infile.read() will return 0 when it is called the last time
   byte[] buffer = new byte[BUFFER_SIZE];
        String str = "";
        try {
            while (true) {
                int read = infile.read(buffer);
                if (read <= 0) {
                    break;
                }

                str += new String(buffer, 0, read);
            }
        } catch (IOException ex) {
            return;
        }

Related Solutions

using java language only write a code Have the function StringChallenge(str) take the str string parameter...
using java language only write a code Have the function StringChallenge(str) take the str string parameter being passed and return the number of words the string contains (e.g. "Never eat shredded wheat or cake" would return 6). Words will be separated by single spaces. Examples Input: "Hello World" Output: 2 Input: "one 22 three" Output: 3 ------- you have the following code edit it to get the result mport java.util.*; import java.io.*; class Main {   public static String StringChallenge(String str)...
Using JAVA The following code is able to read integers from a file that is called...
Using JAVA The following code is able to read integers from a file that is called "start.ppm" onto a 3d array called "startImage". Implement the code by being able to read from another file (make up any file name) and save the data onto another 3d array lets say you call that array "finalImage". The purpose of this will be to add both arrays and then get the average Save the average onto a separte 3darray,lets say you call it...
Java Code Question: The program is supposed to read a file and then do a little...
Java Code Question: The program is supposed to read a file and then do a little formatting and produce a new txt file. I have that functionality down. My problem is that I also need to get my program to correctly identify if a file is empty, but so far I've been unable to. Here is my program in full: import java.io.*; import java.util.Scanner; public class H1_43 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter...
[after §3.22 − easy] String Processing : Consider the following code fragment: 1 int a =...
[after §3.22 − easy] String Processing : Consider the following code fragment: 1 int a = 20; 2 int b; 3 double x = 3.5; 4 String s = "All"; 5 char ch; 6 7 x += a; 8 x--; 9 a /= 4 - 1; 10 b = s.length(); 11 b += 4; 12 s += "is well"; 13 ch = s.charAt(b); 14 System.out.println("a = " + a + ", b = " + b); 15 System.out.println("x = "...
Java: Determine the ouotput of this code ArrayList<String>list=new ArrayList<String>();             list.add("Namath");           &
Java: Determine the ouotput of this code ArrayList<String>list=new ArrayList<String>();             list.add("Namath");             list.add("Sauer");             list.add("Maynard");             list.add("Namath");             list.add("Boozer");             list.add("Snell");             list.add("Namath");             list.add("Atkinson");             list.add("Lammonds");             list.add("Dockery");             list.add("Darnold");             list.remove(2);             list.set(2, "Brady");             list.remove(2);             list.set(4,"Unitas");             list.add(1,"Lamomica");             list.add(3,"Hanratty");             list.remove("Namath");             list.remove(list.size()-1);             list.remove(2);             list.set(7, "Riggins");             Iterator iter = list.iterator();           while (iter.hasNext())         {   System.out.print(iter.next() + " ");                         }                     } }
1.   What is the output of the following code: string s1 = “X”; string s2 =...
1.   What is the output of the following code: string s1 = “X”; string s2 = “A” + s1 + “BC” + s1 + “DEF” + s1 + “G”; cout << s2; 2.   What is the output of the following code: string s1 = “X”; string s2 = “A” + s1 + “BC” + s1 + “DEF” + s1 + “G”; cout << s[0] + s[3]; 3.   What is the output of the following code: string s1 = “X”; string...
Write the following Python code: A string X is an anagram of string Y if X...
Write the following Python code: A string X is an anagram of string Y if X can be obtained by arranging all characters of Y in some order, without removing any characters and without adding new characters. For example, each of the strings "baba", "abab", "aabb" and "abba" is an anagram of "aabb", and strings "aaab", "aab" and "aabc" are not anagrams of "aabb". A set of strings is anagram-free if it contains no pair of strings which are anagrams...
The code in this question shows three ways that one can read data from a file...
The code in this question shows three ways that one can read data from a file in a Python program. Answer the following questions: Using the code as an example, explain why Python is a fixed format programming language. Describe what would be output for each of the print statements in the code listing. Given the difference in output, explain the different roles of readLine(), readLines(), and read() functions. with open("rainfall.txt","r") as inFile:    aLine = inFile.readLine() with open("rainfall.txt", "r") as...
Keep getting error where the code cannot read the text file and create an arraylist of...
Keep getting error where the code cannot read the text file and create an arraylist of objects from it. HouseListTester: import java.util.*; //Hard codes the criteria public class HouseListTester { static HouseList availableHouses; public static void main(String []args) { availableHouses = new HouseList("C:\\Users\\jvs34\\Downloads\\houses.txt"); Criteria c1 = new Criteria(1000, 500000, 100, 5000, 0, 10); Criteria c2 = new Criteria(1000, 100000, 500, 1200, 0, 3); Criteria c3 = new Criteria(100000, 200000, 1000, 2000, 2, 3); Criteria c4 = new Criteria(200000, 300000, 1500,...
HW_6b - Read and write a text file. Include the following in the main.cpp file. Add...
HW_6b - Read and write a text file. Include the following in the main.cpp file. Add code so that the numbers that are read from the data.txt file are written to an output           file named:   results.txt After the numbers are written to the file, the following message should be displayed:    The data has been written to the file. Open the results.txt file and verify that the file was written successfully. Please make the code based on C++,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT