Java tutorial files




















This method opens or creates a file for writing bytes and returns an unbuffered output stream. The method takes an optional OpenOption parameter. If no open options are specified, and the file does not exist, a new file is created. If the file exists, it is truncated.

The following example opens a log file. If the file does not exist, it is created. If the file exists, it is opened for appending.

The ByteChannel interface provides basic read and write functionality. A SeekableByteChannel is a ByteChannel that has the capability to maintain a position in the channel and to change that position.

A SeekableByteChannel also supports truncating the file associated with the channel and querying the file for its size. The capability to move to different points in the file and then read from or write to that location makes random access of a file possible. See Random Access Files for more information.

Both newByteChannel methods enable you to specify a list of OpenOption options. The same open options used by the newOutputStream methods are supported, in addition to one more option: READ is required because the SeekableByteChannel supports both reading and writing.

Specifying READ opens the channel for reading. Before we start working on a file, we need to first create a file if it does not exist. For this, we can use the createNewFile method that accepts the filename as a parameter. It first checks if the file exists, if it does not exist, then it creates a new file in the given path. The below example uses FileWriter to write information into a file. In this tutorial, we will see how to read a file using the FileReader class.

Using the FileInputStream class we can read the source file content and using the FileOutputStream class, we can write the same content to the destination file. In the below example, we are printing the copied contents from the destination file by using the FileReader class. We can delete a file using the Files class delete method in Java. We can check file permissions using various methods present in the Files class in Java. Scanning and Formatting allows a program to read and write formatted text.

The Path Class introduces the cornerstone class of the java. Path Operations looks at methods in the Path class that deal with syntactic operations.

Checking a File or Directory shows how to check a file's existence and its level of accessibility. Deleting a File or Directory. Copying a File or Directory. Moving a File or Directory. Managing Metadata explains how to read and set file attributes. Reading, Writing and Creating Files shows the stream and channel methods for reading and writing files.



0コメント

  • 1000 / 1000