Get the App
SLTechnology News&Howtos  ›  Development  › 

Sample code for input and output streams of JAVA language

Shulou Source: shulou.com Published: 2022-06-02 05:21:38 09月25日 Update

This article mainly introduces the JAVA language input and output stream sample code, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.

Public class IOStreamDemo {

Public void samples () throws IOException {

/ / 1. This is reading a line of data from the keyboard and returning a string.

BufferedReader stdin = new BufferedReader (new InputStreamReader (System.in))

System.out.print ("Enter a line:")

System.out.println (stdin.readLine ())

/ / 2. This is to read the data line by line from the file

BufferedReader in = new BufferedReader (new FileReader ("IOStreamDemo.java"))

String s, S2 = new String ()

While ((s = in.readLine ())! = null)

S2 + = s + ""

In.close ()

/ / 3. This is to read bytes one by one from a string

StringReader in1 = new StringReader (S2)

Int c

While ((c = in1.read ())! =-1)

System.out.print ((char) c)

/ / 4. This is to write a string to the file

Try {

BufferedReader in2 = new BufferedReader (new StringReader (S2))

PrintWriter out1 = new PrintWriter (new BufferedWriter (new FileWriter ("IODemo.out")

Int lineCount = 1

While ((s = in2.readLine ())! = null)

Out1.println (lineCount++ + ":" + s)

Out1.close ()

} catch (EOFException e) {

System.err.println ("End of stream")

}

}

}

Thank you for reading this article carefully. I hope the article "sample Code of JAVA language input and output Stream" shared by the editor will be helpful to you. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Tags: Article this is character string code example language input output data file line value interest at the same time bytes more friends knowledge ribbon Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft OPPO Reno vpn Docker macOS