您的位置:首页 > 编程语言 > Java开发

Java输入输出流

2014-08-22 16:31 239 查看
转自http://blog.csdn.net/hguisu/article/details/7418161下面是提取一些精华部分:Java的IO模型设计非常优秀,它使用Decorator模式,按功能划分Stream,您可以动态装配这些Stream,以便获得您需要的功能。例如,您需要一个具有缓冲的文件输入流,则应当组合使用FileInputStream和BufferedInputStream。 在Java类库中,IO部分的内容是很庞大的,因为它涉及的领域很广泛:        标准输入输出,文件的操作,网络上的数据流,字符串流,对象流,zip文件流等等,java中将输入输出抽象称为流,就好像水管,将两个容器连接起来。将数据冲外存中读取到内存中的称为输入流,将数据从内存写入外存中的称为输出流。java有流式部分(字节流,字符流),非流式部分(文件),其他(文件读取部分的与安全相关的类,如:SerializablePermission类)java的IO流类库包括四个基本类,inputstream,outputstream,reader,writer及各种派生类。InputStream和OutputStream类处理的是字节流,数据流中的最小单位是字节(8个bit); 而Reader与Writer处理的是字符流,在处理字符流时涉及了字符编码的转换问题。Reader类能够将输入流中采用其他编码类型的字符转换为Unicode字符,然后在内存中为其分配内存,Writer类能够将内存中的Unicode字符转换为其他编码类型的字符,再写到输出流中。IOException异常类的子类:public class  EOFException :   非正常到达文件尾或输入流尾时,抛出这种类型的异常。public class FileNotFoundException:   当文件找不到时,抛出的异常。public class InterruptedIOException:   当I/O操作被中断时,抛出这种类型的异常字节流inputstream,outputstream范例:文件输入流:
import java.io.IOException;
import java.io.FileInputStream;
;
public class TestFile {
public static void main(String args[]) throws IOException {
try{
FileInputStream rf=new   FileInputStream("InputFromFile.java");
int n=512;   byte  buffer[]=new  byte
;
while((rf.read(buffer,0,n)!=-1)&&(n>0)){
System.out.println(new String(buffer) );
}
System.out.println();
rf.close();
} catch(IOException  IOe){
System.out.println(IOe.toString());
}

}

}
文件输出流:
<pre name="code" class="java">import java.io.IOException;
import java.io.FileOutputStream;
public class TestFile {
public static void main(String args[]) throws IOException {
try {
System.out.println("please Input from      Keyboard");
int count, n = 512;
byte buffer[] = new byte
;
count = System.in.read(buffer);
FileOutputStream wf = new FileOutputStream("d:/myjava/write.txt");
wf.write(buffer, 0, count);
wf.close(); // 当流写操作结束时,调用close方法关闭流。
System.out.println("Save to the write.txt");
} catch (IOException IOe) {
System.out.println("File Write Error!");
}
}

}
文件输入流和输出流的应用:
import java.io.File;
import java.io.IOException;
import java.io.FileOutputStream;
import java.io.FileInputStream;

public class TestFile {
public static void main(String args[]) throws IOException {
try {
File inFile = new File("copy.java");
File outFile = new File("copy2.java");
FileInputStream finS = new FileInputStream(inFile);
FileOutputStream foutS = new FileOutputStream(outFile);
int c;
while ((c = finS.read()) != -1) {
foutS.write(c);
}  <pre name="code" class="java">import java.io.*;

public class ReadWriteToFile {
public static void main(String args[]) throws IOException {
InputStreamReader sin = new InputStreamReader(System.in);
BufferedReader bin = new BufferedReader(sin);
FileWriter out = new FileWriter("myfile.txt");
BufferedWriter bout = new BufferedWriter(out);
String s;
while ((s = bin.readLine()).length() > 0) {
bout.write(s, 0, s.length());
}

}
}
finS.close(); foutS.close(); } catch (IOException e) { System.err.println("FileStreamsTest: " + e); } } }
缓冲输入输出流(从键盘输入流到内存示例):<pre name="code" class="java">import java.io.*;

public class ReadWriteToFile {
public static void main(String args[]) throws IOException {
InputStreamReader sin = new InputStreamReader(System.in);
BufferedReader bin = new BufferedReader(sin);
FileWriter out = new FileWriter("myfile.txt");
BufferedWriter bout = new BufferedWriter(out);
String s;
while ((s = bin.readLine()).length() > 0) {
bout.write(s, 0, s.length());
}

}
}

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java基础 io流 android