Java file reading and writing

Source: Internet
Author: User

1, Flow:
It is an abstraction of the process of transferring data from a producer (such as a keyboard, disk file, memory, or other device) to a consumer (such as a screen, file, or memory, for example) that receives the data through a buffering mechanism.
2, the relevant Java package:
The Java.io package includes many classes that provide many aspects of the file's operations.
3, about the file name and directory Name class: File class is independent of the system platform, using the constructor
File (String path),
File (string path, String FileName),
File objects, such as file dir, String name, and so on, using member functions such as CanRead (), CanWrite (), GetParent (), GetPath (), and so on to implement the operations of the individual properties of the files.
Import java.io.*;
public class Filetest
{public static void main (String []args)
{
String filename= "C:\\temp\\myfile.dat"
File Myfile=new file (FileName);
If (! myFile. Exists ())
{System.err.println ("Can ' t find" + FileName);
Return
}
System.out.println ("File" + FileName + "is" +myfile.length () + "bytes Long!");
If (MyFile. isdirectory ())
{System.err.println ("File" + FileName + "is a Directory!");
Return
}
}
}
4, about the file content (data) operation of the class:
4.1 Input and output abstract base class Inputstream/outputstream, the basic function function of realizing file content operation read (), write (), close (), skip (), etc. Typically, you create a derived class object that completes the specified special function to enable the file to read and write. The technique of exception handling should be paid attention to in the programming of file reading and writing.
4.2 Fileinputstream/fileoutputstream:
For local file read and write (binary format read and write and is sequential read-write, read and write to create separate file stream objects);
The basic process for programming local file reads and writes is:
① generates file stream objects (should be FileInputStream class for file read operations, and file write should be FileOutputStream Class);
② calls a function function in the FileInputStream or FileOutputStream class such as read (), write (int b), and so on to read and write the contents of the file;
③ closes the file (Close ()).
4.3 Pipedinputstream/pipedoutputstream:
For pipe input and output (the output of one program or one thread is directly connected to another program or to the input port of a thread) to achieve direct transmission of both data. Need to be connected during operation);
Connections for 4.3.1 pipelines:
One method is to use the output of one program directly as input to another program through a constructor, indicating the target pipe object when defining the object
PipedInputStream pinput=new PipedInputStream ();
PipedOutputStream poutput= New PipedOutputStream (pinput);
The second method is to connect () with any member function in the two classes.
PipedInputStream pinput=new PipedInputStream ();
PipedOutputStream poutput= new PipedOutputStream ();
Pinput.connect (Poutput);
Input and output of 4.3.2 pipeline:
The output pipe object invokes the write () member function to output data (that is, sending data to the input end of the pipe), and the input pipe object calls the Read () member function to read the data (that is, to obtain data from the output pipe). This is mainly through the system provided by the buffer mechanism to achieve.
4.4. Random file reading and writing:
The Randomaccessfile class, which inherits directly from the object class rather than the Inputstream/outputstream class, enables you to read and write data from any location in the file (just a pointer to the file's read-write location).
The basic process of random file reading and writing programming is:
① generates a Stream object and indicates a read-write type;
② Mobile Read and write position;
③ Read and write file contents;
④ closes the file.
StringBuffer buf=new StringBuffer ();
Char ch;
while ((Ch= (char) System.in.read ())!= ' \ n ')
{
Buf.append (CH);
}//Read-write can be "R" or "RW"
Randomaccessfile myfilestream=new randomaccessfile ("MyFile.dat", "RW");
myFileStream. Seek (Myfilestream.length ());
Myfilestream.writebytes (Buf.tostring ()); Add the user's input from the keyboard to the end of the file
Myfilestream.close ();
4.5 Datainput/dataoutput Interface: Implement a variety of machine-independent data format reading and writing (such as Readchar (), ReadInt (), Readlong (), Readfloat (), and ReadLine () will return a string). The Randomaccessfile class implements the interface and has more flexible data reading and writing than FileInputStream or FileOutputStream classes.
4.6 standard input/output streams: system.in (for example: Char C=system.in.read ()) and System.out (e.g. System.out.println (), System.out.println ()).
Try
{char ch=system.in.read ();//return binary data (ASCII code with a low 8-bit keyboard)
}
catch (IOException E)
{
}
4.7, the general method of file operation:
(1) Generates an object of the input and output file class (depending on the type of action to be operated);
(2) Calling the member function of this class to realize the reading and writing of the file data content;
(3) Close this file.




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.