java I/O
来源:互联网 发布:ubuntu 远程 中文乱码 编辑:程序博客网 时间:2024/06/05 12:50
1 Node Stream
These streams are all connected to the file or memory directly.
1.2 use FileInputStream/ FileOutputStream to read and write from file
import java.io.*;public class TestFileInputStream { public static void main(String[] args) { int b = 0; FileInputStream in = null; try { in = new FileInputStream("d:\\share\\java\\io\\TestFileInputStream.java"); } catch (FileNotFoundException e) { System.out.println("can't find the file"); System.exit(-1); } try { long num = 0; while((b=in.read())!=-1){ //in.read() can read a byte a time or read a byte array a time System.out.print((char)b); // change each byte to a corresponding char. charactores may not be displayed /** * byte[] c = new byte[20]; * while((b=in.read(c))!=-1){ * System.out.print(new String(c)); * num++; * } * num++; **/ } in.close(); System.out.println(); System.out.println("read "+num+" bytes"); } catch (IOException e1) { System.out.println("error when reading files"); System.exit(-1); } }}
import java.io.*;public class TestFileOutputStream { public static void main(String[] args) { int b = 0; FileInputStream in = null; FileOutputStream out = null; try { in = new FileInputStream("d:/share/java/HelloWorld.java"); out = new FileOutputStream("d:/share/java/io/HW.java"); while((b=in.read())!=-1){ out.write(b); // write can write one byte a time or a byte array a time } in.close(); out.close(); } catch (FileNotFoundException e2) { System.out.println("can't find the file"); System.exit(-1); } catch (IOException e1) { System.out.println("copy file failed"); System.exit(-1); } System.out.println("file copy succeed"); }}
2. Processing Stream
processing Streams can be added to the node stream, offering advanced functions
2.1 BufferedReader& BufferedWriter
The consturctor of BufferedInputStream has an InputStream parameter:
FileInputStream fis = new FileInputStream("d:\\share\\java\\HelloWorld.java");BufferedInputStream bis = new BufferedInputStream(fis);
Its often used with FileInputStream to offer the buffered function to the input stream, but it is still byte stream, which means it can only read byte by byte, and can't read chinese charactors which is combined by 2 bytes.
So Actually the other processing stream: BufferedReader/ BufferedWriter is more popular, they are charactor stream and can read info based on charactor. BufferedReader is used with FileReader(Which is a charactor node stream)
BufferedWriter bw = new BufferedWriter( new FileWriter("d:\\share\\java\\dat2.txt"));BufferedReader br = new BufferedReader( new FileReader("d:\\share\\java\\dat2.txt"));String s = null;for(int i=1;i<=100;i++){ s = String.valueOf(Math.random()); bw.write(s); bw.newLine();// read one line, BufferedReader's popular function}
2.2 Converting Stream( InputStreamReader& OutputStreamWriter)
As the consturctor says, both of them as the note stream as parameter, they can be added onnote byte stream, to change them to be charactor stream. Shown as the program blow:
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("d:\\bak\\char.txt"));osw.write("mircosoftibmsunapplehp");//the reason we do this way is that we can use the osw.write() to write string to the file, not byte by byte//Reading API, we can find this write a string mehtod is inherited from java.io.wirter classSystem.out.println(osw.getEncoding());
The only reason we use it is we can write a string a time to the file.But I think the combination of OutputStreamWriter and the FileOutputStream as the same function as the single Character Node Stream: FileWriter , we can analysis FileOutputStream, OutputStreamWriter and FileWriter 's write() methods.
First, FileOutputStream
FileOutputStream doesn't have advanced write methods like write a string a time.
So,Second, it can be used with OutputStreamWriter
OutputStreamWriter is interited from java.io.Write, which has wirte(String str)
Third, The above 2 are not as good as FileWriter
FileWriter is inherited from OutputStreamWriter and has all the write() methods. Besids, it'sNode Character Stream,which can write to
file directly and based on characters.- Java I/O系统----------- 标准I/O
- Socket Java I/O Socket I/O
- JAVA I/O
- 复习java: I/O
- Java I/O 体系
- java I/O流
- Java 标准I/O
- Java I/O系统
- Java I/O
- JAVA I/O--(2)
- JAVA I/O--(3)
- JAVA I/O--(4)
- java i/o包
- JAVA I/O (一)
- java I/O流
- JAVA I/O操作
- Java I/O原理
- JAVA的I/O
- Codeforces Round #146 (Div. 2) B. Easy Number Challenge
- Codeforces Round #146 (Div. 2) C. LCM Challenge
- java 文本文件加密 文本文件解密
- 推荐一个活动:VMware vForum 2012 开发者盛会
- 【转】Windows SOCKET 缓存/缓冲区 相关了解 Windows NT和Windows 2000的套接字架构
- java I/O
- 构建高性能J2EE应用的五种核心策略
- EEPlat学习笔记------业务对象总线【BusinessObjectBus】(四)
- 使用桌面窗口管理器创建Aero 玻璃效果
- 正则表达式
- .net里Equals 数字变量与数字常量的比较问题
- C#截取屏幕中選定區域的圖像
- 基本数据类型转换
- 这几天被问到的数据库的问题