Topic 3: Stream processing program Design
Experimental purposes and requirements: Students are required to learn and understand the Java Stream programming theory on the basis of learning and understanding of the classroom learning content, learning and gradually master the programming and debugging of the stream program, to learn the correct choice of different streams according to the processing requirements of the use and combination of methods.
Experimental content:
Design and write a program to read a line of string from the keyboard, write it into a text file,
Then write another program to read the string from the text file and display it in the command-line window.
Program 1:
Reads a line of string from the keyboard, writes it into a text file
Import Java.io.*;import java.util.*;p Ublic class in {public static void main (String args[]) { Scanner input=new Scanne R (system.in); try{ System.out.println ("Please enter:"); String Str=input.nextline ();//read in line string FileWriter output = new FileWriter ("In.txt"); BufferedWriter outtwo = new BufferedWriter (output);//File Buffered stream outtwo.write (str);//write String to file Outtwo.newline () ;//Fetch another row } catch (IOException e) { System.out.println (e); }}}
Program 2:
Reads a string from a text file and displays it in a command-line window.
Import Java.io.*;import java.util.*;p Ublic class out {public static void main (String args[]) {try{string str=null; FileReader input=new FileReader ("In.txt"); BufferedReader intwo=new BufferedReader (input);//build file read buffer stream while ((Str=intwo.readline ())!=null) {//Read into end of file System.out.println (str);//Output } } catch (IOException e) {}}}
Java Lab 3: Stream Handler Design