`
lazing
  • 浏览: 23653 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

Java IO note 1

阅读更多

Return value of read()

For instance, according to the Java class library documentation, the read( ) method of java.io.InputStream returns "the next byte of data, or -1 if the end of the stream is reached." Upon reflection, this sounds suspicious. How is a -1 that appears as part of the stream data to be distinguished from a -1 indicating end of stream? In point of fact, the read( ) method does not return a byte; its signature shows that it returns an int:

1 public   abstract   int  read( )  throws  IOException

This int is not a Java byte with a value between -128 and 127 but a more general unsigned byte with a value between 0 and 255. Hence, -1 can easily be distinguished from valid data values read from the stream.


JVM and byte, byte array

In fact, a single byte still takes up four bytes of space inside the Java virtual machine, but a byte array occupies only the amount of space it actually needs. The virtual machine includes special instructions for operating on byte arrays but does not include any instructions for operating on single bytes. They're just promoted to ints.


Input, Output, Reader and Writer

For the most part, these classes have methods that are extremely similar to the equivalent stream classes. Often the only difference is that a byte in the signature of a stream method becomes a char in the signature of the matching reader or writer method. For example, the java.io.OutputStream class declares these three write( ) methods:

        

The java.io.Writer class, therefore, declares these three write( ) methods:

        

As you can see, the signatures match except that in the latter two methods the byte array data has changed to a char array. There's also a less obvious difference not reflected in the signature. While the int passed to the OutputStream write( ) method is reduced modulo 256 before being output, the int passed to the Writer write( ) method is reduced modulo 65,536. This reflects the different ranges of chars and bytes.

java.io.Writer also has two more write( ) methods that take their data from a string:

        public void write(String s) 
throws IOException
public void write(String s, int offset, int length) throws IOException

Because streams don't know how to deal with character-based data, there are no corresponding methods in the java.io.OutputStream class.


Hello World!

 

1   byte [] hello  =   { 72 101 108 108 111 32 87 111 114 108 100 33 10 , 13 } ;
2 System.out.write(hello);

Del.icio.us : io, java

Powered by Zoundry



HIMMEL 2007-01-03 17:25 发表评论
分享到:
评论

相关推荐

    Java.note.zip

    NIO(New IO)是Java对传统IO的一个扩展,提供了非阻塞的I/O操作,提高了效率。网络编程方面,Java的Socket编程允许创建客户端和服务器端的应用,实现数据的传输。 数据库连接是Java应用程序与数据库交互的桥梁,...

    java-note.rar_记事本

    2. **文件I/O操作**:Java的`java.io`包提供了读写文件所需的方法,如`File`, `BufferedReader`, `BufferedWriter`等,用于实现保存和打开文件的功能。 3. **事件监听**:在Java GUI中,需要为按钮和其他交互元素...

    Java_Note.zip_Java笔记

    这份"Java_Note.zip"压缩包包含了一份详细的Java笔记,对于初学者来说,它是一份宝贵的资源,可以帮助理解语言基础,掌握核心概念,并通过实例加深理解。 笔记可能会涵盖以下几个主要知识点: 1. **Java基础**:这...

    java-note.rar_我的

    【标题】"java-note.rar_我的"所代表的知识点主要集中在Java编程语言的学习与实践上。这个压缩包可能包含了一个简单的Java记事本程序的完整开发过程,是学习Java初级到中级开发者常用的示例项目之一。 首先,让我们...

    JavaNote-main

    JavaNote-main是一个可能包含全面Java学习资料的压缩包,它可能是某个开发者或教育者整理的一套Java编程教程或笔记集合。这个压缩包的核心内容可能涵盖了Java语言的基础到高级概念,帮助学习者系统地掌握Java编程...

    java study note

    1. **基础语法** - Java的基础始于它的语法,这可能涉及到变量声明、数据类型(如整型、浮点型、字符型和布尔型)、运算符、流程控制(如if-else、switch-case、for、while循环)以及方法定义。 2. **类与对象** - ...

    cloudnote_java_

    3. **IO流**:Java的IO流用于读写文件,这对于云笔记应用来说至关重要,因为需要将用户的笔记保存到本地或云端。FileInputStream和FileOutputStream用于文件的读写,而ObjectInputStream和ObjectOutputStream则可以...

    java实现解析从中国知网上导出的EndNote、NoteExpresss、NoteFirst、Refworks格式的文件

    1. **文件读取**:使用`java.io`或`java.nio`包中的类,如`BufferedReader`或`Files`,读取文件内容。 2. **格式识别**:通过文件扩展名或文件头信息判断文件格式,比如检查文件是否以XML声明开始。 3. **XML解析**...

    Java记事本制作note

    Java提供`java.io`包,包含`FileReader`, `FileWriter`, `BufferedReader`, `BufferedWriter`等类,用于实现文本文件的读取和写入。 7. **Swing构件的布局管理**: 为了在窗口中合理地排列组件,我们需要使用布局...

    java源码包---java 源码 大量 实例

     Java二进制IO类与文件复制操作实例,好像是一本书的例子,源代码有的是独立运行的,与同目录下的其它代码文件互不联系,这些代码面向初级、中级Java程序员。 Java访问权限控制源代码 1个目标文件 摘要:Java源码,...

    Java简单的记事本程序(完整代码含实验报告)

    在这个记事本程序中,`java.io.File`类用于表示文件对象,`java.io.FileInputStream`和`java.io.FileOutputStream`则分别用于读取和写入文件。为了保存或加载文本数据,程序会使用`BufferedReader`和`BufferedWriter...

    JAVA_API1.6文档(中文)

    java.security.interfaces 提供的接口用于生成 RSA Laboratory Technical Note PKCS#1 中定义的 RSA(Rivest、Shamir 和 Adleman AsymmetricCipher 算法)密钥,以及 NIST 的 FIPS-186 中定义的 DSA(数字签名算法)...

    java_note:java笔记

    Java的NIO(New IO)库进一步提升了IO操作的性能和并发能力。 【多线程】 Java内置对多线程的支持,允许程序同时执行多个任务。通过继承Thread类或实现Runnable接口,可以创建并启动线程。线程同步机制如...

    javaNote 笔记

    14. **Java标准库**:深入理解Collections、IO、NIO、并发工具包等核心API的使用。 15. **JVM原理**:虚拟机的工作原理,如类加载机制、内存模型、字节码执行等。 16. **Spring框架**:依赖注入、AOP(面向切面...

    java-note-example:Java笔记示例

    "java-note-example" 提供的是一份Java学习的笔记示例,旨在帮助初学者和有经验的开发者更好地理解和掌握Java的核心概念以及实际应用。 首先,Java的基础包括语法特性,如封装、继承和多态,这是面向对象编程的关键...

    Java 1.6 API 中文 New

    java.security.interfaces 提供的接口用于生成 RSA Laboratory Technical Note PKCS#1 中定义的 RSA(Rivest、Shamir 和 Adleman AsymmetricCipher 算法)密钥,以及 NIST 的 FIPS-186 中定义的 DSA(数字签名算法)...

    corejava note

    1. **缩进**:在Java中,良好的缩进习惯有助于提高代码的可读性。通常推荐使用4个空格来代替Tab键进行缩进。这样做能够确保所有开发者在查看代码时看到一致的格式。 2. **注释**:编写清晰的注释对于理解和维护代码...

    java源码包3

     Java二进制IO类与文件复制操作实例,好像是一本书的例子,源代码有的是独立运行的,与同目录下的其它代码文件互不联系,这些代码面向初级、中级Java程序员。 Java访问权限控制源代码 1个目标文件 摘要:Java源码...

    达内java学习笔记全套

    核心Java是Java学习的基础,包括面向对象编程、类和对象、封装、继承、多态、异常处理、集合框架、IO流、线程、反射等主题。这部分笔记将详细讲解这些概念,并提供练习题以帮助巩固理解。 4. **Hibernate note**:...

    java源码包2

     Java二进制IO类与文件复制操作实例,好像是一本书的例子,源代码有的是独立运行的,与同目录下的其它代码文件互不联系,这些代码面向初级、中级Java程序员。 Java访问权限控制源代码 1个目标文件 摘要:Java源码...

Global site tag (gtag.js) - Google Analytics