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!