当前位置:编程学习 > JAVA >>

File Input and Output(1)

 Data items can bestored on two broad types of storage devices in a computer system:

Volatile storageis temporary; values that are volatile, such as those stored in variables, arelost when a computer loses power. Random access memory (RAM) is the temporarystorage within a computer. When you write a Java program that stores a value ina variable, you are using RAM. Most computer professionals simply callnonvolatile storage memory.

Nonvolatilestorage is permanent storage; it is not lost when a computer loses power. Whenyou write a Java program and save it to a disk, you are using permanentstorage.

In the windows operatingsystem, the backslash(\) is the oath delimiter- the character used to separatepath components. In the Solaris(UNIX) operating system, a slash(/) is used asthe delimiter.

You use the Pathclass to create objects that contain information about files or directories,such as their locations, creation dates, and whether they even exist. You caninclude either of the following statements in a Java program to use the Pathclass:

import java.nio.file.Path;

importjava.nio.file.*;

public classNIOFile {

   

    public static void main(String[] args){

        FileSystemfs = FileSystems.getDefault();

        Pathpath = fs.getPath("C:\\RHDSetup.txt");

        System.out.println(path.getParent());

    }

 

}

Output:

C:\

 

A file of comma-separated values(CSV) is onein which each value in a record is separated from the next with a comma; Beforean application can use a data file, it must open the file. A Java application opensa file by creating an object and associating a stream of bytes with it.Similarly, when you finish using a file, the program should close the file –that is, make it no longer available to your application.

If you fail to close an output file – a fileto which you are writing data – the data might become inaccessible.

When you perform an input operation in anapplication, you can picture bytes flowing into your program from an input devicethrough a stream, which functions as a pipeline or channel.

A buffer is amemory location where bytes are held after they are logically output, butbefore they are sent to the output device. Using a buffer to accumulate inputor output before issuing the actual IO command improves program performance.When you use an output buffer, you sometimes flush it before closing it.Flushing clears any bytes that have been sent to a buffer for output, but havenot yet been output to a hardware device.

InputStream,outputStream and Reader are subclasses of the object class. All three of theseclasses are abstract. Abstract class contains methods that must be overriddenin their child classes.

String s = "ABCD";

        byte[] data = s.getBytes();

        PrintStream output = null;

        try{

        output= System.out;

        output.write(data);

        output.flush();

        output.close();

        }catch(Exception ex){

        System.err.println(ex.getStackTrace());

       }

Output:

ABCD

 

 

补充:软件开发 , Java ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,