首页 > 代码库 > 输入输出流
输入输出流
输入:从文件读取到程序中 input read
输出:从程序输入到文件中 output write
节点流(原始流):管道直接连接数据源
处理流(包裹流):套在其他管道之上的称为处理流
字节流 字节为单位 字符流 字符为单位
InputStream Reader
OutputStream Writer
字节流:处理所有类型的数据 (图像,音频……)
字符流:只能处理字符型的数据 (处理纯文本数据时,优先选择字符流)
字节流:
InputStream ——从输入流(数据源)中读取数据
read()
继承InputStream
FileInputStream
StringBufferInputStream
ByteArrayInputStream
SequenceInputStream
PipedInputStream
FilterInputStream
System.in
FileInputStream
FileInputStream 读取文件中的信息,以文件作为数据来源——从文件读取的两种方式 第一种,中文字符会出现乱码 第二种,中中文不会出现乱码 public class FileInputStreamDemo { public static void main(String[ ] args) throws IOException { try{ FileInputStream readFile = new FileInputStream("read.txt");//这个地方的路径有相对路径和绝对路劲 int intTemp = readFile.read( ); while(intTemp!=-1){ System.out.print((char)intTemp); intTemp = readFile.read( ); } readFile.close( ); }catch(IOException ex){ } } try { FileInputStream readFile = new FileInputStream("read.txt"); byte[ ] buffer = new byte[2048]; int byteLength = readFile.read(buffer , 0 , 2048); String str = new String(buffer , 0 , byteLength); System.out.println(str); readFile.close( ); }catch(IOException ex){ } }
OutputStream——从用于程序中输出数据 向文件写入数据,第一个支持中文。 第二个不支持中文
write() flush()
先flush() 再close()
继承OutputStream
FileOutputStream
ByteArrayOutputStream
PipedOutputStream
FilerOutputStream
System.out
FileOutputStream
public static void main(String[ ] args) throws IOException { try{ FileOutputStream writeFile = new FileOutputStream("write.txt");
/*可以指定从键盘输入*/ String s = "happy new year!happy everyday!" +"\n"+"wish fulfillment!"+"\n"+"你好\nEnd of file stream\n"; byte[ ] bytes = s.getBytes( ); writeFile.write(bytes); //writeFile.write(bytes,0,s.length()); */ writeFile.close( ); }catch(IOException ex){ } try{ FileOutputStream writeFile = new FileOutputStream("write.txt"); String s = "happy new year!happy everyday!" +"\n"+"wish fulfillment!"+"\n"+"你好\nEnd of file stream\n"; for (int i = 0;i<s.length( );i++){ writeFile.write(s.charAt(i)); } writeFile.close( ); }catch(IOException ex){ } }
FilteredStream
FilterInputStream和FilterOutputStream,把输入流和输出流提供的功能扩大 使用Filtered Stream提供的功能,必须首先提供Filter Stream加工的对象 FilterInputStream BufferedInputStream extends FilterInputStream 为读取的数据申请一份可用的缓冲区,用于提高输入处理的效率。 DataInputStream extends FilterInputStream 从流中读取基本型别(int、char、long等)的数据. DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream(" c:\\Data.txt") ) ) ; Filtered outputStream BufferedOutputStream extends FilteroutputStream 调用flush( )来清除缓冲区内容。 DataOutputStream extends FilteroutputStream 将各种基本型别的数据写至流。 DataOutputStream out =new DataOutputStream(new BufferedOutputStream(new FileOutputStream(“c:\\Data.txt") )) ;
字符流:
Reader --继承自Reader的流都是用于向程序中输入数据,且数据的单位为字符(16 bit);
类 FileReader继承 InputStreamReader
类 StringReader继承 Reader
类 CharArrayReader 继承Reader
利用FileReader类读取纯文本文件”text.txt” 。 public static void main(String args[]) throws IOException{ char c[ ]=new char[500]; //创建可容纳500个字符的数组 FileReader r=new FileReader("c:\\7\\test.txt"); //创建对象r int num=r.read(c); //将数据读入字符数组c内,并返回读取的字符数 String str=new String(c,0,num); //将字符串数组转换成字符串 System.out.println("读取的字符个数为:"+num+",其内容如下:"); System.out.println(str); }
Writer --输出数据到相应的输出流(输出的目标),写入到文件中
flush()--刷空所有输出流,并输出所有被缓存的字节到相应的输出流。
类 FileWriter 继承 OutputStreamWriter
类 StringWriter 继承 Writer
类 CharArrayWriter 继承 Writer
利用FileWriter类将字符数组与字符串写到文件里。 public static void main(String args[]) throws IOException{ FileWriter fw=new FileWriter("c:\\7\\test2.txt"); char c[ ]={‘H‘,‘e‘,‘l‘,‘l‘,‘o‘,‘\r‘,‘\n‘}; String str="欢迎使用Java!"; fw.write(c); //将字符数组写到文件里 fw.write(str); //将字符串写到文件里 fw.close(); }
桥接类
InputStreamReader
将InputStream转换为Reader;
OutputStreamWriter
将OutputStream转换为Writer
BufferedReader
BufferedReader 继承 Reader
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in)) String input = stdIn.readLine( ); 从文件中读取一行字符串: BufferedReader fileIn = new BufferedReader(new FileReader(filename)); String line = fileIn.readLine(); while (line != null) { line = fileIn.readLine( ); } public class Demo { public static void main(String[ ] args) throws IOException{ try{ BufferedReader fileIn = new BufferedReader(new FileReader("c:\\7\\2.txt")); String line = fileIn.readLine( ); while (line != null) { System.out.println(line); line = fileIn.readLine( ); } }catch(IOException ex){ } } }
BufferedWriter
BufferedWriter 继承 Writer
BufferedWriter writer = new BufferedWriter(new FileWriter(“c:\\IODemo.out")) public static void main(String[ ] args) throws IOException{ try{ BufferedReader stdin =new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter a filename:"); String s=stdin.readLine( ); BufferedWriter out1 = new BufferedWriter(new FileWriter(s)); System.out.print("输入5行数据结束!"); for(int i =0;i<5;i++){ s=stdin.readLine( ); out1.write(s); out1.newLine( ); } out1.flush( ); out1.close( ); }catch(IOException ex){ } }
输入输出流