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

How to implement File read and write Operation by java

2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article will explain in detail how to read and write files in java. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

File class

It is an abstract representation of file and directory pathnames.

Files and directories can be encapsulated into objects through File.

For File, it doesn't encapsulate a real file, just a pathname. It can exist or it may not exist. In the future, the content of this path will be transformed into concrete existence through specific operations.

File class constructor File (String pathname) / / create a new File instance File (String parent, String child) / / create a new File instance File (File parent, String child) / / create a new File instance creation function public boolean createNewFile () when a file with that name does not exist by converting the given pathname string to an abstract pathname string Create a new empty file named by the abstract pathname public boolean mkdir () create a directory named by this abstract pathname public boolean mkdirs () create a directory named by this abstract pathname Include any necessary but nonexistent parent directories to determine whether public boolean isDirectory () tests whether the File represented by this abstract pathname is a directory public boolean isFile () tests whether the File represented by this abstract pathname is a file public boolean exists () tests whether the File represented by this abstract pathname exists to get the absolute pathname string public String getPath () that returns this abstract pathname public String getPath () converts this abstract pathname Returns the name of a file or directory represented by this abstract pathname public String [] list () for the pathname string public String getName () returns the name array of files and directories in the directory represented by this abstract pathname public File [] listFiles () returns an array of File objects of files and directories in the directory represented by this abstract pathname delete public boolean delete () delete the file or directory represented by this abstract pathname

Relative path and absolute path

Absolute path: a complete pathname that can locate the file it represents without any other information. For example: e:\ cast\ java.txt

Relative paths: must be interpreted using information taken from other pathnames. For example: myFile\ java.txt

IO flow

Introduction to IO flow

IO: input / output (Input/Output) stream: an abstract concept that is a general term for data transmission. In other words, the transmission of data between devices is called stream, the essence of flow is data transmission IO stream is used to deal with the problem of data transmission between devices. Common applications: file copy; file upload; file download

Classification of IO flows

According to the flow of the data

Input stream: reading data

Output stream: writing data

Divided according to data type

Byte stream

Byte input stream

Byte output stream

Character stream

Character input stream

Character output stream

Usage scenarios for IO streams

If you are working with plain text files, use character streams first if you are working with binary files such as pictures, video, audio, etc. Give priority to byte stream if you are not sure of the file type, use byte stream first. Byte flow is an omnipotent stream

Byte stream write data void write (int b) writes the specified bytes to this file output stream void write (byte [] b) writes b.length bytes from the specified byte array to this file output stream writes one byte array data at a time void write (byte [] b, int o write, int len) starts the len bytes from the specified byte array There is a small problem that the output stream of this file can write part of the data of one byte array at a time starting from the offset o _ write

How to change lines?

Enter\ r\ n in windows

Inux:\ n

Mac:\ r

How to implement additional write

Public FileOutputStream (String name,boolean append) creates a file output stream to write to the file with the specified name. If the second parameter is true, the bytes are written to the end of the file instead of the beginning

About exception handling

You can use try {finally {} to force cleanup of file objects

Try {

Code that may have an exception

} catch (exception class name variable name) {

Exception handling code

} finally {

Perform all cleanup operations

}

Byte input stream

FileInputStream (String name): creates a FileInputStream by opening a connection to the actual file, which consists of the text

To read data from the pathname name named byte input stream in a component system

Create a byte input stream object to call the read data method of a byte input stream object

Release resources

Case

This is the end of this article on "how to read and write files in java". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it out for more people to see.

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

Development

Wechat

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

12
Report