- 浏览: 835280 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
贝塔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 14061.分页的解决方案,通用性,分页时参数丢失问题,如何解决? 2 ... -
java开源项目源代码
2008-08-14 23:02 20872java开源项目 源代码 http://www.codase. ... -
对象的序列化 Externalizable Serializable
2008-08-10 21:51 3124对象的序列化是非常有趣的,因为利用它可以实现“有限持久化”。请 ... -
thinking in java notes
2008-08-09 14:42 1083覆盖一个方法时,只能产生已在方法的基础类版本中定义的异常,或者 ... -
Java优化编程(第二版)
2008-08-03 22:25 2420Java优化编程(第二版) ... -
2.2 JVM中对象的生命周期
2008-08-03 22:22 12452.2 JVM中对象的生命周期 在JVM运行空间中,对象的整 ... -
自我参考:Java学习的30个目标
2008-08-03 21:47 1208自我参考:Java学习的30个目标时间:2007-02-10 ... -
解析Java类和对象的初始化过程
2008-08-03 21:46 1184解析Java类和对象的初始化过程本篇教程来源于 完全教程网 原 ... -
Thinking in java notes
2008-08-01 16:02 1316switch 接受char,enum,byte,int cha ... -
Thinking in java notes
2008-07-22 15:14 1156============chapter 1 Basic=== ... -
JavaBeans教程
2008-07-18 10:14 1143JavaBeans教程 作者:Unkown ... -
AWTEventMulticaster
2008-07-16 10:44 1965http://download.java.net/jdk/jd ... -
Developing Java Beans
2008-07-15 17:54 1050Chapter1 Java Bean architect ... -
Java Enumeration (枚举类型) (2) -- switch语句语法有一点点特别
2008-07-15 11:20 2473http://www.blogjava.net/JafeLee ...
相关推荐
英文版Thinking in java Thinking in Java Fourth Edition Bruce Eckel President, MindView, Inc.
Thinking in Java 4th Edition Annotated Solutions Guide!
Thinking in Java 4Edition 练习题答案
java ,thinking in java ,源码,thinking in java 第四版源码。。欢迎使用
Thinking in java 电子书,经典java书籍
thinking in java thinking in java thinking in java thinking in java thinking in java
Thinking in Java 习题答案, 源代码
thinking in java 文档
Thinking in Java 4th Edition 英文文字版,带书签 Annotated Solution Guide 有代码
Thinking In Java 练习题答案 第四版 Thinking In Java 练习题答案 第四版 Thinking In Java 练习题答案 第四版
Thinking in Java Second Edition.doc
Thinking In Java-Java 编程思想(中英文版 第四版) Thinking In Java-Java 编程思想(中英文版 第四版)
Thinking in java java核心思想英文版(带目录),学java必备
Thinking in java 中文版 学习Java必备教材
Thinking in java java源代码,书中所有裤和例子,必备
thinking in java第4版开发源码,从别处转载,方便大家下载
非扫描版,Thinking In Java 4th Edition(英文版)(含目录封面)
thinking in java word版
他是《Thinking in Java》、《Thinking in C++》、《C++ Inside & Out》《Using C++》和《Thinking in Patterns》的作者,同时还是《Black Belt C++》文集的编辑。他的《Thinking in C++》一本书在1995年被评为...
大量的教程涵盖了该语言的许多细节,每节都有示例练习,您可以尝试并扩展Java知识。