File operations for streams (files)

Source: Internet
Author: User

First, the classification of the flow:

1, the flow according to the direction of classification: divided into the input stream and output stream, the operation of the flow is relative to memory.

Definition of the input stream: When we read the data from the data source into memory, it is called the input stream, also called the read stream.

Definition of output stream: When we write data that is processed in memory to a data source, it is called the output stream, which is written to the stream.

2, stream according to the content classification: can be divided into byte stream, character stream, object flow.

The data is transmitted at the bottom level in a binary way, so no matter what kind of stream, it is actually a byte stream. The character stream and object flow are just a layer of encapsulation on the basis of the byte, so that the characters and object data can be manipulated.

Second, the flow of the parent class

1, all the word stream of the parent class are: InputStream and outputstream;

2. All the parent classes of character streams are: Reader and writer.

Three, the operation of the flow of steps

1. Create a stream. The so-called set-up flow is to establish the channel between the memory and the data source to transmit data.

2, Operation Flow. The data transferred by the operation.

3. Close the stream. Closes the channel between the memory and the data source, freeing the resource.

Cases:

Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import java.io.IOException;
Import Java.io.InputStream;

public class streamtest{
Public Streamtest () {
InputStream in = null;
try {
Create a Stream
in = new FileInputStream ("Info.txt");
int data = 0;
An operation flow that reads one byte at a time. If 1, then the read is complete
while (data = In.read ())! =-1) {
SYSTEM.OUT.PRINTLN (data);
}
} catch (Exception e) {
E.printstacktrace ();
}finally{
try {
Close the stream
In.close ();
} catch (Exception e) {
E.printstacktrace ();
}
}
}
}

Note: After the stream is used, it must be closed, and if not closed, memory resources are wasted, and the write stream may not write data. The order of the close must be closed after the first established stream, and then the established stream is first closed.

The purpose of the construction flow is to complete the conversion of the memory data and the disk data.

Five, the following is a partial flow of file operations

The second argument is true, which means that data is written to the resource file in an appended manner, several times the code is run, and the resource file appends data several times and does not overwrite the original data of the resource file.

OutputStream out = null;

out = new FileOutputStream ("Info.txt", true);

Out.write ("Java Technology". GetBytes ());

Note: When writing to the stream using Ooutputstream, if the file does not exist, the read flow throws a FileNotFoundException exception. The write stream automatically creates a new file. But the premise is that the directory where the file resides must exist.

To create a file object

File F = new file ("C:/abc/a.txt");

Or get the file name

String name = F.getname ();

SYSTEM.OUT.PRINTLN (name);

Get the directory where the files are

String dir = f.getparent ();

System.out.println (dir);

Get the absolute path to the file

String path = F.getabsolutepath ();

SYSTEM.OUT.PRINTLN (path);

Determines whether the file or directory represented by the file object exists, and if so, the result is true

Boolean isex = F.exists ();

System.out.println (Isex);

Determines whether a file object represents a file that exists

System.out,pritln (F.isfile ());

Determines whether a file object represents a directory

System.out.println (F.isdirectory ());

Get a list of files in a directory that contains files and directories, but does not contain content from subdirectories

File F = new file ("D:/kwdownload");

file[] fs = F.listfiles ();

for (File Ft:fs) {

System.out.println (Ft.getname ());

}

Delete files under the specified directory

File F = new file ("D:/kwdownload/a.docx");

F.delete ();

Delete the file or directory represented by the file object, if it must be an empty file to delete

File F = new file ("D:/kwdownload/aaa");

F.delete ();

Get files in all files and subfolders in the specified directory

public static void (string[] args) {

List (new File ("D:/kwdownload"));

}

public static void list (File dirfile) {

file[] fs = Dirfile.listfiles ();

for (File F:fs) {

if (F.isfile ()) {

System.out.println (F.getabsolutepath ());

}else{

List (f)

}

}

}

Create a directory to develop a path

File F = new file ("D:/aaa");

F.mkdir ();

Create a file Selection object

JFileChooser JFC = new JFileChooser;

File operations for streams (files)

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.