Java: bufferedReader. readLine ()

Source: Internet
Author: User

The Code implements a normal line feed of the read content and copies the content to the system clipboard.

<Span style = "font-size: 14px"> public static void ReadAlart () {try {String encoding = "UTF-8"; File file = new File ("Alart.txt "); if (file. isFile () & file. exists () {// determine whether the file exists InputStreamReader read = new InputStreamReader (new FileInputStream (file), encoding); // take into account the encoding format BufferedReader bufferedReader = new BufferedReader (read ); string lineTxt = ""; String AlartTxt = ""; while (lineTxt = bufferedReader. rea DLine ())! = Null) {lineTxt + = '\ n'; AlartTxt + = lineTxt;} // System. out. println (AlartTxt); Clipboard sysClip = Toolkit. getdefatooltoolkit (). getSystemClipboard (); Transferable text = new StringSelection (AlartTxt); sysClip. setContents (text, null); read. close ();} else {System. out. println ("the specified file cannot be found");} catch (Exception e) {System. out. println ("An error occurred while reading the file content"); e. printStackTrace () ;}</span>

 

Java: bufferedReader. fileInputStream is used to read readLine () from the file. It is in the byte format. So when reading Chinese characters, the byte stream will contain garbled characters, therefore, it is necessary to use the desired stream for reading. When reading a file using FileInputStream, it is read using the read () method in it. It is read directly in one breath, it will leave a line feed of the original file, but the read using BufferedReader is read using readLine (), which means reading in a row, so that when reading the carriage return, return the string first, and then read the next row!

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.