- 浏览: 856903 次
- 性别:
- 来自: 北京
-
文章分类
最新评论
-
贝塔ZQ:
读取excel文件,插件pageoffice也可以实现。
POI读写Excel文件(转)- - -
springmvc-freemarker:
java开源项目源码实例下载
java开源项目源代码 -
xuning2516:
第一种方法是个死循环啊。。。
Java NIO 拷贝文件(使用compact) -
u012046856:
...
SessionFactory.getCurrentSession与openSession的区别 -
Season_Wang:
赞一个呀,挺实用的
java swing 中的FileDialog
public class IOStreamDemo { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { try { // // 1. Buffered input file DataInputStream in = new DataInputStream(new BufferedInputStream( new FileInputStream("c:/test.xml"))); String s, s2 = new String(); while ((s = in.readLine()) != null) s2 += s + "\n"; in.close(); System.out.println(s2); PrintWriter writer = new PrintWriter(System.out); // // // // 2. Input from memory StringBufferInputStream in2 = new StringBufferInputStream(s2); int c; while ((c = in2.read()) != -1) System.out.print((char) c); // // 3. Formatted memory input try { DataInputStream in3 = new DataInputStream( new StringBufferInputStream(s2)); while (true) System.out.print((char) in3.readByte()); } catch (EOFException e) { System.out.println("End of stream encountered"); } LineNumberInputStream lineNumberInputStream = new LineNumberInputStream( new StringBufferInputStream(s2)); DataInputStream objectInputStream = new DataInputStream( lineNumberInputStream); PrintStream printStream = new PrintStream(new BufferedOutputStream( System.out)); while (objectInputStream.available() > 0) { printStream.println("Line " + lineNumberInputStream.getLineNumber() + ":" + objectInputStream.readLine()); } printStream.close(); objectInputStream.close(); lineNumberInputStream.close(); // // 4. Line numbering & file output try { LineNumberInputStream li = new LineNumberInputStream( new StringBufferInputStream(s2)); DataInputStream in4 = new DataInputStream(li); PrintStream out1 = new PrintStream(new BufferedOutputStream( new FileOutputStream("IODemo.out"))); while ((s = in4.readLine()) != null) out1.println("Line " + li.getLineNumber() + s); out1.close(); // finalize() not reliable! } catch (EOFException e) { System.out.println("End of stream encountered"); } // // 5. Storing & recovering data try { DataOutputStream out2 = new DataOutputStream( new BufferedOutputStream(new FileOutputStream( "Data.txt"))); out2.writeBytes("Here's the value of pi: \n"); out2.writeDouble(3.14159); out2.close(); DataInputStream in5 = new DataInputStream( new BufferedInputStream(new FileInputStream("Data.txt"))); System.out.println(in5.readLine()); System.out.println(in5.readDouble()); } catch (EOFException e) { System.out.println("End of stream encountered"); } // 6. Reading/writing random access files RandomAccessFile re = new RandomAccessFile("c:/test.txt", "rw"); for (int i = 0; i < 10; i++) { re.writeDouble(10.1 * i); } for (int i = 0; i < 10; i++) { re.writeChar(i + 'A'); } re.close(); re = new RandomAccessFile("c:/test.txt", "r"); for (int i = 0; i < 10; i++) { System.out.println(re.readDouble()); } for (int i = 0; i < 10; i++) { System.out.println(re.readChar()); } RandomAccessFile rf = new RandomAccessFile("rtest.dat", "rw"); for (int i = 0; i < 10; i++) rf.writeDouble(i * 1.414); rf.close(); rf = new RandomAccessFile("rtest.dat", "rw"); rf.seek(5 * 8); rf.writeDouble(47.0001); rf.close(); rf = new RandomAccessFile("rtest.dat", "r"); for (int i = 0; i < 10; i++) System.out.println("Value " + i + ": " + rf.readDouble()); rf.close(); // 7. File input shorthand InFile in6 = new InFile(args[0]); String s3 = new String(); System.out.println("First line in file: " + in6.readLine()); in6.close(); // 8. Formatted file output shorthand PrintFile out3 = new PrintFile("Data2.txt"); out3.print("Test of PrintFile"); out3.close(); // 9. Data file output shorthand OutFile out4 = new OutFile("Data3.txt"); out4.writeBytes("Test of outDataFile\n\r"); out4.writeChars("Test of outDataFile\n\r"); out4.close(); } catch (FileNotFoundException e) { System.out.println("File Not Found:" + args[0]); } catch (IOException e) { e.printStackTrace(); System.out.println("IO Exception"); } } } class InFile extends DataInputStream { public InFile(String filename) throws FileNotFoundException { super(new BufferedInputStream(new FileInputStream(filename))); } public InFile(File file) throws FileNotFoundException { this(file.getPath()); } } class PrintFile extends PrintStream { public PrintFile(String fileName) throws FileNotFoundException { super(new BufferedOutputStream(new FileOutputStream(fileName))); } public PrintFile(File file) throws FileNotFoundException { this(file.getName()); } } class OutFile extends DataOutputStream { public OutFile(String fileName) throws FileNotFoundException { super(new BufferedOutputStream(new FileOutputStream(fileName))); } public OutFile(File file) throws FileNotFoundException { this(file.getName()); } }
Java IO设计
针对不同的类型(文件,网络,console)做不同的处理(随机访问,顺序访问|| 二进制访问,字符访问,按行,按字符访问)。
Java IO库包含旧的IO库和新的IO库。
Java IO分为输入和输出两部分。
输入的基类是InputStream,方法是read
输出的基类是OutputStream,方法是write
但是我们通常不会使用这两个类,而是用复杂的类来利用他们。
通常我们会将多个类堆叠在一起完成多个功能。
因此为了得到一个流,可能得出创建多个类。
InputStream的作用是表示从多个不同来源,包括:
字符串,
byte数组,
file,
pipe,
net,
FileterInputStream。
知识点一: 四大等级结构
java语言的i/o库提供了四大等级结构:InputStream,OutputStream,Reader,Writer四个系列的类。InputStream和OutputStream处理8位字节流数据, Reader和Writer处理16位的字符流数据。InputStream和Reader处理输入, OutputStream和Writer处理输出。大家一定要到J2SE文档中看看这四大等级结构的类继承体系。
除了这四大系列类,i/o库还提供了少数的辅助类,其中比较重要的是InputStreamReader和OutputStreamWriter。InputStreamReader把InputStream适配为Reader, OutputStreamWriter把OutputStream适配为Writer;这样就架起了字节流处理类和字符流处理类间的桥梁。
您使用i/o库时,只要按以上的规则,到相应的类体系中寻找您需要的类即可。
System.out进行标准输出,它已预封装成一个PrintStream对象。System.err同样是一个PrintStream,但System.in是一个原始的InputStream,未进行任何封装处理。这意味着尽管能直接使用System.out和System.err,但必须事先封装System.in,否则不能从中读取数据。
读入一行数据:
System.out.println(new DataInputStream(System.in).readLine());
管道数据流主要用于多线程程序。
Java 1.1的IO流
我们需要与新结构中的类联合使用老结构中的类。为达到这个目的,需要使用一些“桥”类:InputStreamReader将一个InputStream转换成Reader,OutputStreamWriter将一个OutputStream转换成Writer。
所以与原来的IO流库相比,经常都要对新IO流进行层次更多的封装。
之所以在Java 1.1里添加了Reader和Writer层次,最重要的原因便是国际化的需求。老式IO流层次结构只支持8位字节流,不能很好地控制16位Unicode字符。除此之外,新库也对速度进行了优化,可比旧库更快地运行。
Sources & Sinks: |
Corresponding Java 1.1 class |
InputStream |
Reader |
OutputStream |
Writer |
FileInputStream |
FileReader |
FileOutputStream |
FileWriter |
StringBufferInputStream |
StringReader |
(no corresponding class) |
StringWriter |
ByteArrayInputStream |
CharArrayReader |
ByteArrayOutputStream |
CharArrayWriter |
PipedInputStream |
PipedReader |
PipedOutputStream |
PipedWriter |
。
发表评论
-
web开发的一些问题(java方向)
2008-09-08 19:42 14561.分页的解决方案,通用性,分页时参数丢失问题,如何解决? 2 ... -
java开源项目源代码
2008-08-14 23:02 20952java开源项目 源代码 http://www.codase. ... -
对象的序列化 Externalizable Serializable
2008-08-10 21:51 3189对象的序列化是非常有趣的,因为利用它可以实现“有限持久化”。请 ... -
thinking in java notes
2008-08-09 14:42 1156覆盖一个方法时,只能产生已在方法的基础类版本中定义的异常,或者 ... -
Java优化编程(第二版)
2008-08-03 22:25 2488Java优化编程(第二版) ... -
2.2 JVM中对象的生命周期
2008-08-03 22:22 12952.2 JVM中对象的生命周期 在JVM运行空间中,对象的整 ... -
自我参考:Java学习的30个目标
2008-08-03 21:47 1257自我参考:Java学习的30个目标时间:2007-02-10 ... -
解析Java类和对象的初始化过程
2008-08-03 21:46 1227解析Java类和对象的初始化过程本篇教程来源于 完全教程网 原 ... -
Thinking in java notes
2008-08-01 16:02 1387switch 接受char,enum,byte,int cha ... -
Thinking in java notes
2008-07-22 15:14 1204============chapter 1 Basic=== ... -
JavaBeans教程
2008-07-18 10:14 1181JavaBeans教程 作者:Unkown ... -
AWTEventMulticaster
2008-07-16 10:44 2087http://download.java.net/jdk/jd ... -
Developing Java Beans
2008-07-15 17:54 1113Chapter1 Java Bean architect ... -
Java Enumeration (枚举类型) (2) -- switch语句语法有一点点特别
2008-07-15 11:20 2525http://www.blogjava.net/JafeLee ...
相关推荐
根据提供的文件信息,以下是对文件《Thinking in Java 4th Edition Annotated Solutions Guide》中所包含知识点的详细解释: 首先,文件标题《Thinking in Java 4th Edition Annotated Solutions Guide》指出了这是...
《Thinking in Java》第四版由布鲁斯·埃克尔(Bruce Eckel)撰写,他是MindView公司的总裁。这本书被广泛认为是学习Java编程语言的经典教材之一。从读者的反馈来看,《Thinking in Java》不仅覆盖了Java的核心概念...
《Thinking in Java》是Bruce Eckel的经典之作,第四版(TIJ4)更是Java程序员必读的书籍之一。这本书深入浅出地介绍了Java语言的核心概念和技术,包括面向对象编程、集合框架、多线程、网络编程等众多主题。源码是...
研讨课 Hands-on Java研讨课CD Thinking in Objects研讨课 Thinking in Enterprise Java Thinking in Patterns(with Java) Thinking in Patterns研讨课 设计咨询与复审 附录B 资源 软件 编辑器与IDE 书籍 分析与设计...
《Thinking in Java》是Bruce Eckel的经典之作,它深入浅出地介绍了Java语言的核心概念和技术。这本书的练习题是学习Java的重要组成部分,因为它们能够帮助读者巩固理论知识并提升实践能力。以下是对"Thinking in ...
《Thinking in Java》是Bruce Eckel的经典之作,第四版涵盖了Java编程语言的广泛主题,适合初学者和有经验的程序员。这本书深入浅出地讲解了Java的核心概念和技术,旨在帮助读者建立坚实的编程基础,并理解面向对象...
Thinking In Java-Java 编程思想(中英文版 第四版) Thinking In Java-Java 编程思想(中英文版 第四版)
《Thinking in Java》是Bruce Eckel的经典之作,被誉为学习Java编程的权威指南。该书以其深入浅出的方式,详尽地介绍了Java语言的核心概念和技术。第三版是此书的一个重要里程碑,它涵盖了Java语言的诸多关键特性,...
在众多关于Java的学习材料中,《Thinking in Java》第二版以其独特的视角和方法论成为了一本备受推崇的教科书。 本书由Bruce Eckel所著,作为一本权威性的Java编程教程,它不仅仅是向读者介绍Java语言的语法和基础...
《Thinking in Java》是一本备受推崇的Java编程教材,由Bruce Eckel撰写,被誉为Java学习者的必读之作。这本书深入浅出地介绍了Java语言的核心概念和技术,覆盖了从基础到高级的主题,对于有一定Java基础的读者来说...
《Thinking in Java》是Bruce Eckel的经典之作,第四版更是被广大Java开发者视为学习和进阶的重要参考书籍。这本书深入浅出地介绍了Java语言的核心概念和技术,包括面向对象编程、集合框架、多线程、网络编程、GUI...
《Thinking in Java》是Bruce Eckel的经典编程教材,它深入浅出地介绍了Java语言的核心概念和技术。这本书以其详尽的解释、丰富的示例和实践性强的习题深受程序员喜爱。"Thinking in Java 习题答案"是配套的解答集,...
《Thinking in Java》是 Bruce Eckel 编著的一本经典的Java编程教材,它深受程序员喜爱,被誉为学习Java的必备参考书。这本书全面深入地探讨了Java语言的核心概念和技术,不仅适合初学者,也对有经验的程序员提供了...
Thinking in Java 3rd Edition.rar Thinking in Java 3rd Edition.rar Thinking in Java 3rd Edition.rar Thinking in Java 3rd Edition.rar Thinking in Java 3rd Edition.rar Thinking in Java 3rd Edition.rar
《Thinking in Java》是Bruce Eckel的经典Java编程教材,它以其深入浅出的讲解和全面系统的内容深受程序员喜爱。这本书的Annotated Solution Guide是作者为读者提供的配套解答指南,帮助读者理解和解决书中练习题,...
《Thinking In Java(英文版 第四版)》是一本由Bruce Eckel所著的经典Java编程教程。Bruce Eckel是MindView公司的总裁,他凭借自己在Java编程领域的深刻理解和丰富的教学经验,为读者呈现了一本深受世界各地Java...
《Thinking in Java》是Bruce Eckel的经典Java编程书籍,它深入浅出地讲解了Java语言的核心概念和技术。这本书不仅适合初学者,也对有经验的程序员提供了深入理解Java的宝贵资源。现在,我们有机会免费下载这本书的...
Thinking in java java核心思想英文版(带目录),学java必备
他是《Thinking in Java》、《Thinking in C++》、《C++ Inside & Out》《Using C++》和《Thinking in Patterns》的作者,同时还是《Black Belt C++》文集的编辑。他的《Thinking in C++》一本书在1995年被评为...
《Thinking in Java》是Bruce Eckel的经典之作,它被誉为学习Java编程的最佳教材之一。这本书以其深入浅出的讲解方式和全面覆盖的Java知识点而受到广大程序员的推崇。本压缩包包含的是《Thinking in Java》的第三版...