站内搜索: 请输入搜索关键词

当前页面: 开发资料首页J2SE 专题请教输入输出 | ~输入输出

请教输入输出 | ~输入输出

摘要: 请教输入输出 | ~输入输出


今天查询jdk文档,察看RandomAccessFile时发现:


public class RandomAccessFile

input operations read bytes starting at the file pointer and advance the file pointer past the bytes read. 为什么输入操作是读取bytes呢?难道这个input不是通常意义上的输入(写操作)吗?
output operations write bytes starting at the file pointer and advance the file pointer past the bytes written. 为什么输出操作是写bytes呢?output不是通常意义上的输出(读操作)吗?


String readLine()
Reads the next line of text from this file 而这里的读操作就是通常认识上的读取呢?
writeChars(String s)
Writes a string to the file as a sequence of characters 这里的写操作就是通常认识的写呀?

能帮我解释解释吗?谢谢!!!!


小弟急切等待!!
谢谢!


求救!!


Note : 随机存取文件的行为类似存储在文件系统中的一个大型字节数组.
读的时候,会有一个指针,一个一个移动读取每一个字节
写的时候,会由指针一个一个的写入字节

下面给出readLine()的实现方法
public final String readLine() throws IOException {
StringBuffer input = new StringBuffer();
int c = -1;
boolean eol = false;

while (!eol) {
switch (c = read()) {
case -1:
case '/n':
eol = true;
break;
case '/r':
eol = true;
long cur = getFilePointer();
if ((read()) != '/n') {
seek(cur);
}
break;
default:
input.append((char)c);
break;
}
}

if ((c == -1) && (input.length() == 0)) {
return null;
}
return input.toString();
}


public final void writeChars(String s) throws IOException {
int clen = s.length();
int blen = 2*clen;
byte[] b = new byte[blen];
char[] c = new char[clen];
s.getChars(0, clen, c, 0);
for (int i = 0, j = 0; i < clen; i++) {
b[j++] = (byte)(c[i] >>> 8);
b[j++] = (byte)(c[i] >>> 0);
}
writeBytes(b, 0, blen);
}



不是很懂~
学习一下~


input operations read bytes starting at the file pointer and advance the file pointer past the bytes read. 为什么输入操作是读取bytes呢?难道这个input不是通常意义上的输入(写操作)吗?
output operations write bytes starting at the file pointer and advance the file pointer past the bytes written. 为什么输出操作是写bytes呢?output不是通常意义上的输出(读操作)吗?
-------------------------------------------------------------------------
在计算机系统中的输入和输出操作通常是以内存为研究目标的,数据从外设进入内存称“输入”反之是“输出”,例如:

从文件(在外存上,如硬盘)读数据到内存称为“输入”
从内存写数据到文件(在外存上,如硬盘)称为“输出”

-------------------------------------------------------------------------

String readLine()
Reads the next line of text from this file 而这里的读操作就是通常认识上的读取呢?
writeChars(String s)
Writes a string to the file as a sequence of characters 这里的写操作就是通常认识的写呀?
-----------------------------------------------------------------------------------
如果在内存中转移数据,如上面的readLine和writeChars方法,是流的操作,实际上就是在内存中转移数据,这时,如果你考虑的是数据从哪儿获得,则一般称为“读”;考虑的是将数据放到什么目标,一般称为“写”。

注意不要刻意把读写与输入输出联系在一起。


↑返回目录
前一篇: 对象继承和overload的问题。比较难的一道SCJP考题,我晕了。
后一篇: 如何把.java程序变成.exe可执行文件?