Chapter 14 Job JAVA

Keywords: Java Back-end

1. What are the classifications of streams in Java?
(1) Flow direction: generally divided into input and output streams
Input stream: For example, System.in is an InputStream type input stream
Output stream: For example, System.out is a PrintStream type output stream
(2) From the reading type: generally divided into byte stream and character stream
Byte stream: For example, System.in is an InputStream type byte stream
Character stream: For example, new InputStreamReader(System.in) is a character stream object
(3) Flow from the source of occurrence: divided into node flow and filter flow classes
Node stream: directly manipulate the stream corresponding to the target device
Such as file stream, standard input output stream
Filter Stream: Inherit stream with keyword Filter
Used to wrap operation node streams for easy reading and writing of various types of data
2. What are the subclasses of byte streams InputStream and OutputStream? Examples of scenarios are provided. What are the corresponding character streams?
(1)ByteArrayInputStream and ByteArrayOutputStream build bridges between byte arrays and streams.
(2)FileInputStream and FileOutputStream build bridges between files and streams.
(3)PipedInputStream and ipedOutputStream are commonly used to connect the output of one program to the input of another program. The output stream acts as the sender end of the pipeline and the input stream acts as the receiver end of the pipeline. You need to call the connect method to connect the output stream to the input stream before using it. Typically, one thread writes the output stream of a pipeline and the other reads the input stream of a pipeline.
(4) Corresponding character streams: Reader family
Writer Class Family

3. What is the conversion between byte stream and character stream? What support does Java provide for this?
(1) inputstreamReader is used to construct the input byte stream into a character stream:
InputStreamReader(InputStream in)

For example:
InputStreamReader ins = new InputStreamReader(System.in);
InputStreamReader ins = new InputStreamReader(new
FileInputStream("test.txt"));
(2) Output character stream to byte stream OutputStreamWriter or PrintWriter construction method: OutputStreamWriter(OutputStream out)
PrintWriter(OutputStream out)
For example:
OutputStreamWriter outs = new OutputStreamWriter(new
FileOutputStream("test.txt"));
4. What is the use of filter streams (assembly of streams) in Java? Examples of commonly used filter streams are provided.
Filter Stream: BufferedInputStream and BufferedOutputStream, which are cached to assemble expensive read-write node streams for file disks, network devices, terminals, etc. to improve read-write performance.
Use of filter stream BufferedReader: for caching character streams, read line by line:

import java.io.*;
public class inDataSortMaxMinIn { 
    public static void main(String args[]) {
     try{
        BufferedReader keyin = new BufferedReader(new 
                                     InputStreamReader(System.in)); 
        String c1;
        int i=0;
        int[] e = new int[10];   
        while(i<10){
           try{
               c1 = keyin.readLine();
               e[i] = Integer.parseInt(c1);
               i++;
            }  
            catch(NumberFormatException ee){
                   System.out.println("Please enter the correct number!");
            }
       }
    }
    catch(Exception e){
        System.out.println("System Error");
 }}}

5. What is object serialization and deserialization? What support does Java provide for this?
(1) Serialization: Converts an object that implements the Serializable interface into a sequence of bytes, which in the future can be completely restored to the original object, also known as deserialization.
(2)Java support for serialization:
ObjectInputStream classes and
ObjectOutputStream class
6. What does the File class of Java mean? What's the use?
The File class refers not only to files in the system, but also to directories, which are special files.
Role: Represents a file object, an abstract representation of the path name of a file (or directory).

7. What support does Java provide for reading and writing files?
(1) File backup Example: Copy the Write1.txt file to the backup folder of the current directory

import java.io.*;
import java.util.Date;
import java.text.SimpleDateFormat;
public class UpdateFile {
    public static void main(String args[]) throws IOException {
        String fname = "Write1.txt";       //File name to copy
        String childdir = "backup";        //Subdirectory Name
        new UpdateFile().update(fname,childdir);
    }
    public void update(String fname,String childdir) throws IOException{ 
        File f1,f2,child;
        f1 = new File(fname);              //Create file object f1 in current directory
        child = new File(childdir);        //Create file object child in current directory
        if (f1.exists()){ 
            if (!child.exists())           //Create subdirectories when child does not exist
                child.mkdir();
            f2 = new File(child,fname);    //Create file path f2 in subdirectory child
            if (!f2.exists() ||            //If f2 does not exist or exists but has an earlier date
                 f2.exists()&&(f1.lastModified() > f2.lastModified())) 
                copy(f1,f2);               //copy
            getinfo(f1);
            getinfo(child);
        }
        else
            System.out.println(f1.getName()+" file not found!");
    }

(2) File copy

public void copy(File f1,File f2) throws IOException { 
                                         //Create File Input Stream Object
        FileInputStream  rf = new FileInputStream(f1); //Write1.txt
                                         //Create File Output Stream Object
        FileOutputStream  wf = new FileOutputStream(f2); //backup\\Write1.txt
        int count,n=512;
        byte buffer[] = new byte[n];
        count = rf.read(buffer,0,n);       //Read Input Stream
        while (count != -1) {
            wf.write(buffer,0,count);      //Write Output Stream
            count = rf.read(buffer,0,n);   //Continue reading data from file to buffer
        }
        System.out.println("CopyFile  "+f2.getName()+" !");
        rf.close();                        //Close Input Stream
        wf.close();                        //Close Output Stream
    }

(3) Obtaining file information

public static void getinfo(File f1) throws IOException{   
        SimpleDateFormat sdf;
        sdf= new SimpleDateFormat("yyyy year MM month dd day hh time mm branch");
        if (f1.isFile()) //file
            System.out.println("<File>\t"+f1.getAbsolutePath()+"\t"+
              f1.length()+"\t"+sdf.format(new Date(f1.lastModified())));
        else{           //Catalog
            System.out.println("<Dir>\t"+f1.getAbsolutePath());
            File[] files = f1.listFiles(); //List all File objects in the current directory
            for (int i=0;i<files.length;i++)
                getinfo(files[i]); //Recursive call
        }
    }
}

(4) Random file operation: random access to binary primes.bin,
Write 2 as the minimum prime number to the file first, then test odd numbers within 100 in turn, if prime,
Write to the end of the file

import java.io.*;
public class PrimesFile {
    RandomAccessFile raf; //Data Members
    public static void main(String args[]) throws IOException { 
         (new PrimesFile()).createprime(100);  
    }
    public void createprime(int max) throws IOException{
        raf=new RandomAccessFile("primes.bin","rw"); Create File Object
        raf.seek(0);                                //File pointer is 0
        raf.writeInt(2);                            //Write Integer
        int k=3;
        while (k<=max) {
            if (isPrime(k))
                raf.writeInt(k);
            k = k+2;  
        }
        output(max);                                //Output prime number
        raf.close();                                //Close File
    }

Posted by Gayathri on Tue, 30 Nov 2021 10:03:13 -0800