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

demostrate the folllowing methods , writeArray and readArray,in a program: publi

ID: 3538481 • Letter: D

Question

demostrate the folllowing methods ,writeArray and readArray,in a program:

   

public static void readArray(String fileName, int[] content)throws Exception{
        File f = new File(fileName);
        //here we have to check the file not found
        if(!f.exists()){
            System.out.println("File not found...");
            return;
        }
        //here we are checking is entered file is directory.
        else if(f.isDirectory()){
            System.out.println("entered file is directory");
            return;
        }
       
        //if above conditions are success
        //start read the file.
        //in order to read file create bufferedinputstream
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(f));
        int ch = -1;
        int index = 0;
        try{
            //read char by char and checking is -1(end)
            //here index < content.length checks, if the array size is less than file size.
            //it will not fail to store.
            //it will store the content, it can fit into array.
            while((ch = bis.read()) != -1 && index < content.length){
                //add content to int array
                content[index] = ch;
                index++;
            }
        }catch(Exception e){
            System.out.println(e);
        }
        //closing file
        finally{
            bis.close();
        }
    }
   
    public static void writeArray(String fileName, int[] content)throws Exception{
        File f = new File(fileName);
       
        //create bufferedoutputstream class
        //if file not found it will create it
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(f));
        int ch = -1;
        int index = 0;
        try{
            //read content from int array
            for(int i=0;i                //write the char in file
                bos.write(content[i]);
            }
        }catch(Exception e){
            System.out.println(e);
        }
        //closeing file
        finally{
            bos.close();
        }
    }
};i++){>

Explanation / Answer

In order to test the functionality, Please use below main.

public static void main(String args[])throws Exception{
        //take sample file as "in.txt"
        String inputFile = "in.txt";
        //where we can store the "out.txt"
        String outputFile = "out.txt";
        //Create File object to check the file size.
        File f = new File("in.txt");
        //create the int array for store the data.
        int content[] = new int[(int)f.length()];
        //read the file to content
        readArray(inputFile, content);
        //displaying the content in the array
        display(content);
        //write the content into other file
        //That means copy.
        writeArray(outputFile, content);
    }
   
    public static void display(int[] content){
        for(int i=0;i<content.length;i++){
            System.out.print((char)content[i]);
        }
    }


in.txt content.


I am writing program for read and write the file
First we have to read the file
Next we have to write the content to other file
I mean to say, just copy the file.


Output It will display the content of in.txt and write to out.txt:


I am writing program for read and write the file
First we have to read the file
Next we have to write the content to other file
I mean to say, just copy the file.