Summary of Java file and file stream operations

Source: Internet
Author: User
Tags flushes

One: The related concepts of data flow

Data flow

In Java, the data transfer between different source and program is abstracted as "stream" to achieve a relatively uniform and simple input/output operation mode. Data in transit is the same as flowing water, also known as data flow.

2. How I/O data streams are categorized

The data flow is divided into two types: input stream and output stream. The input stream can read only and cannot be written. The output stream can only be written and unreadable. (Here is the point of view of the program to determine the direction of access, the data from the outside of the program to the program called "Input" data, the program data transferred to the external predicate "output" data.

3. Buffer Stream (Buffered stream)

Each operation on the data stream is done in bytes, either writing a byte to the output stream or reading a byte from the input stream. Obviously inefficient, usually using a buffer stream, which is to configure a buffer for a stream, a buffer is a piece of memory dedicated to transmitting data.

4. Data SOURCC: Refers to where data can be provided, including keyboards, disk files, network interfaces, etc.

5. Data Sink: Refers to a place where data can be received, such as disk files, network interfaces, and external devices such as monitors and printers. (data host can also be considered as the destination of data transfer)


node flow and processing flow

Depending on whether the data stream is associated with a data source or other data stream, it can be divided into node stream and processing stream (processing stream)

A node stream can read/write data from/to a specific place.

The processing flow is the connection and encapsulation of an existing stream, which implements enhanced data read/write functionality through the functional invocation of the encapsulated stream, and the processing stream is not directly connected to the data source.



Character Stream

1. Character Input stream reader

The reader class is a superclass of all character-oriented input streams, declared as abstract classes in java.io.

public int Read (): reads a character and returns the one that is read. If you read to the end of the stream, return-1.

public int read (char[] cbuf): The read characters are stored in the specified array, and the number of characters actually read is returned. If you read to the end of the stream, return-1.

public abstract int Read (char[] cbuf,int off,int len): The read character is stored at the specified position (off) of the array, with a maximum of Len characters per read, returning the actual number of characters read. If you read to the end of the stream, return-1.

Close (): Read characters its utility is the function of the window system, after the use is complete, the release of resources.

Character output stream writer

The Weiter class is a superclass of all character-oriented output streams, declared as abstract classes in java.io.

public void write (int c): Writes a character to the stream.

public void Write (char[]): Writes the characters in the array sequentially.

public abstract void Write (char[] bcbuf,int off,int len): Writes out the Len characters that start with the subscript off in the array.

public void Write (string): Writes a string to the stream.

public abstract void Flush (): Refreshes the stream, flushes the data in the stream to the destination, and the stream exists.

Public abstreact Void Close (): Closes the resource, calls flush before closing, flushes the data in the stream to the destination, and then the stream closes.



Use of FileWriter

There are no unique methods for this class. Only their own number of constructs.

Characteristics of the class:

1. For working with text files.

2. There is a default encoding table in this class.

3. There is a temporary buffer in this class.


Two: file and data stream stream related operations


1: Data Flow byte array


public static byte[] Getbytebyinputstream (InputStream fileinputstream) {
Bytearrayoutputstream BAOs = new Bytearrayoutputstream ();
int read = 0;
byte[] bytes = new byte[1024];


try {
while (read = Fileinputstream.read (bytes))! =-1) {
if (Baos.size () > 16 * 1024 * 1024-1) {
throw new RuntimeException ("File size is larger than 16M");
}
Baos.write (bytes, 0, read);


}
} catch (IOException e) {
E.printstacktrace ();
}


byte[] array0 = Baos.tobytearray ();


return array0;


}

2: Uploading Files


public static void Upload (byte[] array0, String FilePath) {
try {
FileOutputStream fos = new FileOutputStream (FilePath);
Fos.write (ARRAY0);
Fos.close ();
} catch (Exception e) {
E.printstacktrace ();
}


}


3: Get File size

public static String bytes2kb (long bytes) {
BigDecimal filesize = new BigDecimal (bytes);
BigDecimal megabyte = new BigDecimal (1024 * 1024);
Float returnvalue = Filesize.divide (megabyte, 2, BIGDECIMAL.ROUND_UP)
. Floatvalue ();
if (ReturnValue > 1)
Return (returnvalue + "MB");
BigDecimal kilobyte = new BigDecimal (1024);
returnvalue = Filesize.divide (kilobyte, 2, bigdecimal.round_up)
. Floatvalue ();
Return (returnvalue + "KB");
}


4: Create a file directory

public static Boolean Createdir (String destdirname) {
File dir = new file (destdirname);
if (dir.exists ()) {
SYSTEM.OUT.PRINTLN ("Create directory" + destDirName + "failed, target directory already exists");
return false;
}
if (!destdirname.endswith (File.separator)) {
destDirName = destDirName + file.separator;
}
Create a Directory
if (Dir.mkdirs ()) {
SYSTEM.OUT.PRINTLN ("Create directory" + destDirName + "Success!") ");
return true;
} else {
SYSTEM.OUT.PRINTLN ("Create directory" + destDirName + "failed! ");
return false;
}
}


5: Delete Files


public static Boolean DeleteFile (String spath) {
Boolean flag = false;
File File = new file (spath);
The path is a file and is not empty to delete
if (File.isfile () && file.exists ()) {
File.delete ();
Flag = true;
}
return flag;
}

Summary of Java file and file stream operations

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.