`
fehly
  • 浏览: 245399 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Java I/O编程

    博客分类:
  • java
阅读更多

java i/o原理

基本概念:

  • I/O(Input/Output)
  • 数据源(Data Source)
  • 数据宿(Data Sink)

Java中把不同的数据源与程序间的数据传输都抽象表述为"流"(Stream),java.io包中定义了多种I/O流类型实现数据I/O功能。

I/O流分类:

  • 输入流(Input Stream)和输出流(Output Stream)
  • 节点流(Node Stream)和处理流(Processing Stream)
  • 字符流(Character Stream)和字节流(Byte Stream)

输入流和输出流

按照数据流动的方向,java流可分为输入流(Input Stream)和输出流(Output Stream)

  • 输入流只能从中读取数据,而不能向其写出数据;
  • 输出流则只能向其写出数据,而不能从中读取数据
  • 特例:java.io.RandomAccessFile类

节点流和处理流

根据数据流所关联的是数据源还是其他数据流,可分为节点流(Node Stream)和处理流(Processing Stream)

  • 节点流直接连接到数据源
  • 处理流是对一个已存在的流的连接和封装,通过封装的流的功能调用实现增强的数据读/写功能,处理流并不直接连接到数据源.

字符流和字节流

按传输数据的"颗粒大小"划分,可分为字符流(Character Stream)和字节流(Byte Stream)

  • 字节流以字节为单位进行数据传输,每次传送一个或多个字节;
  • 字符流以字符为单位进行数据传输,每次传送一个或多个字符.

Java命名惯例:

凡是以InputStream或OutputStream结尾的类型均为字节流,凡是以Reader或Writer结尾的均为字符流。

InputStream

抽象类java.io.InputStream是所有字节输入流类型的父类,该类中定义了以字节为单位读取数据的基本方法,并在其子类中进行了分化和实现.

三个基本的read方法:

  • int read()
  • int read(byte[] buffer)
  • int read(byte[] buffer,int offset,int length)

其他方法:

  • void close()
  • int available()
  • skip(long n)
  • boolean markSupported()

InputStream类层次

 

 

OutputStream

java.io.OutputStream与java.io.InputStream对应,是所有字节输出流类型的抽象父类。

三个基本的write方法:

  • void write(int c)
  • void write(byte[] buffer)
  • void write(byte[] buffer,int offset,int length)

其他方法:

  • void close()
  • void flush()

OutputStream类层次

Reader

抽象类java.io.Reader是所有字符输入流类型的父类,其中声明了用于读取字符流的有关方法.

三个基本的read方法:

  • int read()
  • int read(char[] cbuf)
  • int read(char[] cbuf,int offset,int length)

其他方法:

  • void close()
  • boolean ready()
  • skip(long n)
  • boolean markSupported()
  • void mark(int readAheadLimit)
  • void reset()

Reader类层次

 Writer

java.io.Writer与java.io.Reader类对应,是所有字符输出流类型的共同父类.

五个基本的write方法:

  • void write(int c)
  • void write(char[] cbuf)
  • void write(char[] cbuf,int offset,int leng)
  • void write(String string)
  • void write(String string,int offset,int length)

其它方法:

  • void close()
  • void flush()

Writer类层次

常用I/O流类型

FileInputStream/FileOutputStream

FileInputStream用于读取本地文件中字节数据,FileOutputStram用于将字节数据写到文件.

使用字节流实现文件的复制

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class CopyFile{
	public static void main(String[] args) {
		try {
			FileInputStream fis = new FileInputStream ("a.jpg");
			FileOutputStream fos = new FileOutputStream ("temp.jpg");
			int read = fis.read();		
			while ( read != -1 ) {
				fos.write(read);	
				read = fis.read();
			}			
			fis.close();
			fos.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

FileReader/FileWriter

FileReader用于以字符为单位读取文本文件,FileWriter类用于将字符数据写到文本文件中

使用字符流实现文件的复制

import java.io.*;
public class Test {
	public static void main(String[] args) {
		try {
			FileReader input = new FileReader("a.txt");
			FileWriter output = new FileWriter("temp.txt");
			int read = input.read();		
			while ( read != -1 ) {
				output.write(read);	
				read = input.read();
			}			
			input.close();
			output.close();
		} catch (IOException e) {
			System.out.println(e);
		}
	}
}

BufferedReader/BufferedWriter

BufferedReader用于缓冲读取字符,BufferedWriter则是供字符的缓存写出功能.

使用字符处理流实现文件的复制

import java.io.*;
public class Test {
	public static void main(String[] args) {
		try {
			FileReader input = new FileReader("Test.java");
			BufferedReader br = new BufferedReader(input);
			FileWriter output = new FileWriter("temp.txt");
			BufferedWriter bw = new BufferedWriter(output);
			String s = br.readLine();			
			while ( s!=null ) {
				bw.write(s);
				bw.newLine();		
				s = br.readLine();
			}			
			br.close();
			bw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

 InputStreamReader

  • InputStreamReader可封装字节输入流并从中读取字节数据,然后将之转换为字符.
  • 转换时所使用的字符编码可以在构造方法中显示指定,也可以使用平台的默认字符编码,其构造方法格式为:
  • public InputStreamReader(InputStream in)
  • public InputStreamReader(InputStream in,String enc)

OutputStream Writer

  • 与InputStreamReader对应,OutputStreamWriter可按照特定的字符编码规则把字符转化为字节并写出到它所封装的字节输出流.

PrintStream

  • PrintStream在OutputStream基础之上提供了增强的功能,即可以方便地输出各种类型数据(而不仅限于byte型)的格式化表示形式.
  • PrintStream的方法从不抛出IOException

PrintWriter

  • PrintWriter提供了PrintStream的所有打印方法,其方法也从不抛出IOException、
  • 与PrintStream的区别:作为处理流使用时,PrintStream只能封装OutputStream类型的字节流,而PrintWriter既可以封装OutputStream,还能够封装writer类型字符输出流并增强其功能

DataInputStream/DataOutputStream

  • 二者分别实现DataInput/DataOutput接口
  • DataInputStream能够以一种与机器无关的方式,直接从底层字节输入流读取java基本类型和String类型的数据.
  • DataOutputStream则能够直接将java基本类型和String类型数据写出到其他的字节输出流。
import java.io.*;
public class Test{
	public static void main(String args[]){
		try{
			FileOutputStream fos;
			DataOutputStream dos;
			FileInputStream fis;
			DataInputStream dis;
			fos = new FileOutputStream("myfile.data");
			dos = new DataOutputStream(fos);
			dos.writeUTF("休.格兰特");
			dos.writeInt(40);
			dos.close();
			fis = new FileInputStream("myfile.data");
			dis = new DataInputStream(fis);
			System.out.println("name:" + dis.readUTF());
			System.out.println("age:" + dis.readInt());
			fis.close();
		}catch(IOException e){
			System.out.println(e);	
		}
	}
}

 CharArrayReader/CharArrayWriter

  • charArrayReader实现了一个可用作字符输入流的字符缓冲区.
  • CharArrayWriter实现了一个可当做Writer使用的字符输出缓存区.
import java.io.*;
public class MyReader{
	public void getInfo(Reader reader){
		try{
			int b = reader.read();
			while(b != -1){
				System.out.print((char)b);
				b = reader.read();	
			}
			System.out.println();
		}catch(IOException e){
			e.printStackTrace();	
		}		
	}	
}
import java.io.*;
public class TestCharArrayReader{
	public static void main(String[] args){
		char[] b = {'I',' ','a','m',' ','S','a','i','l','i','n','g','!'};
		CharArrayReader car = new CharArrayReader(b);
		MyReader mr = new MyReader();
		mr.getInfo(car);	
	}	
}
import java.io.*;
import java.util.Date;

public class TestCharArrayWriter{
	public static void main(String[] args){
		try{
			BufferedReader br = new BufferedReader(new FileReader("a.txt"));
			String s ;
			while((s=br.readLine()) != null){
				System.out.println(s);	
			}
		}catch(IOException e){
			CharArrayWriter cw = new CharArrayWriter(); 
			PrintWriter pw = new PrintWriter(cw,true); 
	 		e.printStackTrace(pw);
	 		//char[] ca = cw.toCharArray(); 
	 		String info = cw.toString(); 
	 		Date time = new Date();
	 		//将time和info信息写入数据库---	
	 		System.out.println("出错时间: " + time);
	 		System.out.println("错误信息:\n" + info);
		}			
	}	
}

标准I/O重定向 

标准输入重定向

import java.io.*;

public class TestSetInput{
	public static void main(String[] args){
		try{
			//FileInputStream fis = new FileInputStream("source.txt");
			//System.setIn(fis);			
			int avg = 0;
			int total = 0;
			int count = 0;
			int num = 0;
			int i;
			BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
			String s = br.readLine();
			while(s != null && !s.equals("over")){
				i = Integer.parseInt(s);
				num++;
				total += i;
				avg = total/num;
				System.out.println("num=" + num + "\ttotal=" + total + "\tavg=" + avg);	
				s = br.readLine();
			}
		}catch(Exception e){
			e.printStackTrace();	
		}
	}
}

标准输出/标准错误输出重定向

import java.io.*;
import java.util.Date;

public class TestSetOutput{
	public static void main(String[] args){
		PrintStream ps = null;
		PrintStream ps_error = null;
		try{
			ps = new PrintStream(new FileOutputStream("output.txt",true));
			System.setOut(ps);
			ps_error = new PrintStream(new FileOutputStream("errorLog.txt",true));
			System.setErr(ps_error);

			int avg = 0;
			int total = 0;
			int count = 0;
			int num = 0;
			int i;
			BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
			String s = br.readLine();
			while(s != null && !s.equals("over")){
				i = Integer.parseInt(s);
				num++;
				total += i;
				avg = total/num;
				System.out.println("num=" + num + "\ttotal=" + total + "\tavg=" + avg);	
				s = br.readLine();
			}
		}catch(Exception e){
			System.err.println("出错时间: " + new Date());
			System.err.print("错误信息:");
			e.printStackTrace(System.err);	
		}finally{
			try{
				ps.close();
				ps_error.close();
			}catch(Exception e1){
				System.err.println("出错时间: " + new Date());
				System.err.print("错误信息:");
				e1.printStackTrace(System.err);	
			}	
		}
	}
}

属性导出/导入

import java.io.*;
import java.util.Properties;

public class LoadProperties{
	public static void main(String[] args){
		try{
			Properties ps = new Properties();
			FileReader fr = new FileReader("props.txt");
			ps.load(fr);
			fr.close();				
			ps.list(System.out);
		}catch(Exception e){
			e.printStackTrace();	
		}
	}
}
import java.io.*;
import java.util.Properties;

public class SaveProperties{
	public static void main(String[] args){
		try{
			Properties ps = new Properties();
			ps.setProperty("name","Scott");
			ps.setProperty("password","Tiger");
			FileWriter fw = new FileWriter("props.txt");
			ps.store(fw,"loginfo");	
			fw.close();		
		}catch(Exception e){
			e.printStackTrace();	
		}
	}
}

实现文件随即存/取操作

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;

public class TestRandomAccessFile{
	private File file; 
	public static void main(String[] args){
		TestRandomAccessFile traf = new TestRandomAccessFile();
		traf.init();
		traf.record("Billy",22);
		traf.listAllRecords();
	}
	
	public void record(String record_breaker, int times){
		try{
			RandomAccessFile raf = new RandomAccessFile(file,"rw");
			boolean flag = false;
			while(raf.getFilePointer() < raf.length()){
				String name = raf.readUTF();
				if(record_breaker.equals(name)){
					raf.writeInt(times);	
					flag = true;
					break;
				}else{
					raf.skipBytes(4);	
				}
			}
			if(!flag){
				raf.writeUTF(record_breaker);
				raf.writeInt(times);	
			}
			raf.close();				
		}catch(Exception e){
			e.printStackTrace();	
		}
	}
	
	public void init(){
		if(file == null){
			file = new File("record.txt");
			try{
				file.createNewFile();
			}catch(IOException e){
				e.printStackTrace();	
			}	
		}	
	}
	
	public void listAllRecords(){
		try{
			RandomAccessFile raf = new RandomAccessFile(file,"r");
			while(raf.getFilePointer() < raf.length()){
				String name = raf.readUTF();
				int times = raf.readInt();
				System.out.println("name:" + name + "\trecord:" + times);
			}
			raf.close();				
		}catch(Exception e){
			e.printStackTrace();	
		}		
	}
}

使用临时文件

import java.io.*;
import java.awt.event.*;
import javax.swing.*;

public class TestTempFile implements ActionListener{
	private File tempPath;
	public static void main(String args[]){
		TestTempFile ttf = new TestTempFile();
		ttf.init();
		ttf.createUI();
	}
	
	public void createUI(){
		JFrame frame = new JFrame();
		JButton jb = new JButton("创建临时文件");
		jb.addActionListener(this);
		frame.add(jb,"North");	
		frame.setSize(200,100);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
	}
	
	public void init(){
		tempPath = new File("temp");
		if(!tempPath.exists() || !tempPath.isDirectory()){
			tempPath.mkdir();  //如果不存在,则创建该文件夹
		}
	}
	public void actionPerformed(ActionEvent e){
		try	{
			//在tempPath路径下创建临时文件"mytempfileXXXX.tmp"
			//XXXX 是系统自动产生的随机数, tempPath对应的路径应事先存在
			File tempFile=File.createTempFile("mytempfile",".txt",null);
			System.out.println(tempFile.getAbsolutePath());
						
			FileWriter fout=new FileWriter(tempFile);
			PrintWriter out=new PrintWriter(fout);
			out.println("some info!" );
			out.close(); //注意:如无此关闭语句,文件将不能删除
			
			//tempFile.delete(); 
			tempFile.deleteOnExit();
		}
		catch(IOException e1){
			System.out.println(e1);
		}
	}
}

 对象序列化

基本概念

  • 对象的持久性(Object Persistance)长久保持一个对象的状态并在需要时获取该对象的信息以重新构造一个状态完全相同的对象
  • 对象序列化(Object Serialization)通过写出对象的状态数据来记录一个对象
  • 对象序列化的主要任务,写出对象信息,并遍历该对象对象的引用,从而建立一个完整的序列化流。

实现对象序列化

要序列化一个对象,其所属的类必须实现一些两种接口之一:

java.io.Serializable

java.io.Externalizable

java.io.ObjectOutputStream/ObjectInputStream类分别提供了对象的序列化和反序列化功能。

对象序列化APi层次

对象序列化过程中的数据保护

标记性接口Serializable

在对象序列化过程中,其所属类的static属性和方法代码不会被序列化处理.

对于个别不希望被序列化的非static属性,可以在属性声明时使用transient关键字进行标明.

分享到:
评论

相关推荐

    Java I/O编程 java

    数据流的概念及输入输出方法 字节流和字符流 文件、文件流 随机访问文件 过滤流 管道流 对象流 数据流小结

    java i/o 实例 编程 学习 教程 复习

    java i/o 实例 编程 学习 教程 复习,,,,,,,,,,,,,,,,,,,,,

    Java I/O学习笔记: 磁盘操作 字节操作 字符操作 对象操作 网络操作 NIO & AIO Java I/O

    Java I/O学习笔记: 磁盘操作 字节操作 字符操作 对象操作 网络操作 NIO & AIO Java I/O Java是一种面向对象的编程语言,由Sun Microsystems于1995年推出。它是一种跨平台的语言,意味着可以在不同的操作系统上运行...

    Java中的I/O学习总结

    Java中的I/O学习总结Java中的I/O学习总结

    java基础之I/O流

    适合初学者学习使用的流案例,绝对使用。

    Java网络编程--Unix域协议:I/O和描述符的传递

    Unix域协议:I/O和描述符的传递 18.1 概述 210 18.2 PRU_SEND和PRU_RCVD请求 210 18.3 描述符的传递 214 18.4 unp_internalize函数 218 18.5 unp_externalize函数 220 18.6 unp_discard函数 221 18.7 unp_dispose...

    Java的I/O编程

    从键盘输入一系列字母,将其存储到文件中,对其进行升序排序后,存到另一个文件中,并显示在屏幕上。

    基于I/O、多线程、GUI的标准考试系统v1.0(JAVA实现)

    本系统是基于JAVA开发平台,运用Socket网络编程,I/O文件流,线程,Swing图形界面等技术,实现的一套标准化考试系统。通过该系统可以完成多个科目的标准化考试工作。 该系统共分为三个模块:考生客户端,服务器端和...

    java使用简单的I/O编写的投票系统

    java使用简单的I/O编写的投票系统

    java基础知识I/O流使用详解

    编程语言的I/O类库中常常使用流这个抽象的概念,它代表任何有能力产生数据的数据源对象或时有能力接收数据的接收端对象,本文为大家介绍Java中I/O系统基础知识

    [Java] Java 网络编程 第4版 (英文版)

    Learn how Java’s core I/O API handles network input and output Discover how the InetAddress class helps Java programs interact with DNS Locate, identify, and download network resources with Java’s ...

    Java NIO 高性能编程

    Java NIO 深入探讨了 1.4 版的 I/O 新特性,并告诉您如何使用这些特性来极大地提升您所写的Java 代码的执行效率。这本小册子就程序员所面临的有代表性的 I/O 问题作了详尽阐述,并讲解了如何才能充分利用新的 I/O ...

    java文件与I/O流

    高效的课件,详细的内容介绍,针对性的代码展示,提高性的练习题目。适合新手或者基础薄弱人员学习,一个PPT解决你编程中常见的错误,最重要的是一人下载多人使用,不受限制,没有隐藏密码

    2018年9月 Java9编程参考官方大全 第10版.rar

    畅销书作家Herbert Schildt在本书中详尽地讲解了Java语言,包括Java的语法、关键字和基本编程原则,以及Java API库的主要内容,如I/O、集合框架、流库或并发实用程序。另外,《Java 9编程参考官方大全(第10版)》还...

    Java输入与输出(I、O).

    Java输入输出合集。 File类 RandomAccessFile类 Scanner类 字节流:IputStream类 等等等等

    Java+NIO+(中文版).pdf

    Java NIO深入探讨了1.4版的I/O新特性,并告诉您如何使用这些特性来极大地提升您所写的Java代码的执行效率。这本小册子就程序员所面临的有代表性的I/O问题作了详尽阐述,并讲解了如何才能充分利用新的I/O特性所提供的...

    JAVA网络编程第4版

    《Java网络编程(第4版)》全面介绍了如何使用Java开发网络程序。你将学习如何使用Java的网络类库既快速又轻松地完成常见的网络编程任务,如编写...第四版经过全面修订,已经涵盖REST、SPDY、异步I/O和很多其他高级技术。

    java程序设计

    《Java程序设计》系统地介绍了Java程序语言的知识,全书分为13章,包括Java集成开发环境,语法基础,对象和类的基本概念,数组,JavaI/O体系,Java多线程机制,图形用户界面、容器等。在介绍基础知识的同时,《Java...

    Java网络编程期末考试复习题库+答案

    Java网络编程期末考试复习题库+答案 java网络编程考试题目,内含有Java网络编程期末复习题(I/O、多线程、网络编程、数据库操作),有答案

Global site tag (gtag.js) - Google Analytics