Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Java input and output Stream

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

Java input and output stream example analysis, in view of this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

1Java input / output stream

All programming languages mention the way copy; interacts with native file systems; Java is no exception. We'll look at how Java handles standard file input and output (including stdin,stout,stderr). When you develop Mini Program on the Internet, you must note that direct file input and output is the key to insecurity. Most users set up their browsers to give you free access to their file systems, but copy; does not allow you to access them. Of course, if you develop your internal application, you may need to access the file directly.

Users of standard input and output Unix, or other command-line system-based users, such as DOS, know what standard input and output means. The standard input file is the keyboard and the standard output file is your terminal screen. The standard error output file also points to the screen and, if necessary, to another file to distinguish it from normal output.

System class Java achieves the function of accessing standard input and output through system class. The three files mentioned above are implemented in this system class: StdinSystem.in implements stdin as an instance of the InputStream class, and you can use the read () and skip (longn) member functions. Read () lets you read one byte from the input, and skip (longn) lets you skip n bytes in the input.

StoutSystem.out as a PrintStream to implement stdout, you can use print () and println () two member functions. These two functions support any basic type of Java as a parameter.

StderrSystem.err implements stderr like stdout. Like System.out, you can access PrintStream member functions.

2 examples of standard input and output

Here is an example of a function like cat or type in Unix:

Importjava.io.*classmyCat {publicvoidmain (Stringargs []) throwsIOException {intb;intcount=0;while ((b=System.in.read ())! =-1) {count++;System.out.print ((char) b);} System.out.println (); / / blanklineSystem.err.println ("counted" + count+ "totalbytes.");}}

3 general input and output class

In addition to basic keyboard input and screen output, we also need to contact the input and output of the file. We will learn the following classes: lFileInputStreamlDataInputStreamlFileOutputStreamlDataOutputStream

For reference, list another copy; application-specific class: lPipedInputStreamlBufferedInputStreamlPushBackInputStreamlStreamTokenizerlPipedOutputStreamlBufferedOutputStreamlRandomAccessFile

We won't discuss the copy; class here, but you can see the member function definition of each class in the JAVA_HOME/src/java/io directory.

4 Files

When we do the file operation, we need to know the information of copy; about the file. The File class copy; a copy; member function to manipulate files and obtain information about copy; files.

4.1 create a new file object

You can create a new file object in three ways:

FilemyFile;myFile=newFile ("etc/motd")

Or

MyFile=newFile ("/ etc", "motd"); / / moreusefulifthedirectoryorfilenamearevariables

Or

FilemyDir=newfile ("/ etc"); myFile=newFile (myDir, "motd")

These three methods depend on how you access the file. For example, if you use only one file in your application, the first structure to create the file is the easiest. But if you open several files in the same directory, the second or third structure is better-copy;.

4.2 File testing and use

One? copy; you create a file object, you can use the following member functions to get information about the file:

File name lStringgetName () lStringgetPath () lStringgetAbslutePath () lStringgetParent () lbooleanrenameTo (FilenewName)

File test lbooleanexists () lbooleancanWrite () lbooleancanRead () lbooleanisFile () lbooleanisDirectory () lbooleanisAbsolute ()

General file information llonglastModified () llonglength ()

Directory usage lbooleanmkdir () lString [] list ()

4.3 example program for obtaining file information

Here is a separate program that displays the basic information of the file, which is transferred through command line parameters:

Importjava.io.*;classfileInfo {FilefileToCheck;publicstaticvoidmain (Stringargs []) throwsIOException {if (args.length > 0) {for (inti=0;i)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report