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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值