Njupt Java language Stream processing program design

Source: Internet
Author: User

First, the purpose and requirements of the experiment

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 reads a line of string from the keyboard, writes it to a text file, and then writes another program to read the string from a text file and display it in a command-line window.


Second, the experimental code

Write file

Package Think;import Java.io.bufferedreader;import Java.io.bufferedwriter;import java.io.file;import Java.io.fileinputstream;import Java.io.filenotfoundexception;import Java.io.fileoutputstream;import Java.io.filereader;import Java.io.filewriter;import Java.io.ioexception;import Java.io.InputStreamReader;import Java.io.objectinputstream.getfield;public class Java_exp_03iostream {public static void main (string[] args) {// Character buffer stream BufferedReader br = null; BufferedWriter bw = null;try {//br=newbufferedreader (Newfilereader ("D:\\buffered.txt"));//Read byte stream from keyboard convert into character stream wrapped into buffer character stream BR = new BufferedReader (new InputStreamReader (system.in)), bw = new BufferedWriter (New FileWriter ("D:\\buffered.txt")); for (String str; (str= br.readline ())! = NULL; ) {if (Str.equalsignorecase ("E") | | Str.equalsignorecase ("Exit")) {System.out.println ("exit"); else {bw.write (str); Bw.newline ();//Line Delimiter}}} catch (IOException e) {e.printstacktrace ();//Todo:handle Exception} Finally//Close two buffered objects {try {if (br! = NULL) br.close();} catch (IOException e) {//Todo:handle exceptione.printstacktrace ();} try {if (bw! = NULL) Bw.close ();} catch (IOException e) {//Todo:handle exceptione.printstacktrace ();}}}} <span style= "Font-weight:bold;" ></span>

console input string, enter e exits and writes the string to the file.



The second program reads the contents of the file.

<span style= "FONT-SIZE:12PX;" ><strong>p</strong>ackage Think;import Java.io.bufferedreader;import Java.io.FileReader;import  Java.io.ioexception;public class Java_exp_03read {public static void main (string[] args) {BufferedReader br = null;try {BR = new BufferedReader (New FileReader ("D:buffered.txt")); for (String str; (str= br.readline ())! = NULL; ) {System.out.println (str);}} catch (IOException e) {//Todo:handle exceptione.printstacktrace ();} Finally{try {if (br! = null) Br.close ();} catch (IOException e) {//Todo:handle exceptione.printstacktrace ();}}}} </span>





Iii. Summary of the experiment

IO Stream Learning Summary:
*fileinputstream
*fileoutputstream byte file input/output stream
*filereader
*filereader character file input/output stream
*buffered+* buffer Stream class belongs to the filter flow wrapper file stream commonly used
*inputstreamreader
*outputstreamreader Conversion Stream
*datainputstream
*dataoutputstream Data Flow
*printstream
*printwriter Print Flow
*objectoutputstream
*objectinputstream Object Flow


the whole idea:


* The input and output of the file can be implemented in many ways, the difference is in efficiency, the characters are coded differently, and the way they are used may be different.


problems encountered and how to resolve them:


* Problem: The first method to be adopted is fileinputstream,fileoutputstream. If it's written in English or a number, no problem. If it is in Chinese, there is a problem when I enter it from the file. Output to file no problem. Baidu + thought for a while, the result is as follows: Unicode encoding, Chinese is two bytes encoded. In the way of byte stream from the file read in, encountered Chinese characters, the Chinese split into two bytes read, so it will be garbled. When the byte stream is output to a file, the Chinese character is converted into a byte array, a byte-byte output file according to the default encoding of the platform, and Windows Notepad can automatically recognize the Chinese characters. Therefore, the text content needs to read, generally do not use byte stream.


* Optimization: After using the character stream input and output, the buffer stream is used to improve the efficiency. First, read or write to the contents of the file, first saved in the buffer array, one-time write or output, improve efficiency. I wrote the program can be very well reflected: for example, I write characters from the console to the file, enter E or exit end, and no input e and exit, when opened buffered, will find that there is no content. After the end, the content is not displayed.


* Experience:The input and output of the c,c++ file have been learned, but it is useless to write small software at ordinary times. But input and output are important.


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Njupt Java language Stream processing program design

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.