java输入输出流

导读:本篇文章讲解 java输入输出流,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

1.首先,什么是i/o流?
I/O流用来完成java的输入输出工作,包括磁盘文件、设备、其他程序以及内存数据等等的输入输出
2.I/Ol流的原理
2.1输入流
程序从一个数据源读取数据到本程序中,这些数据的方向是从程序的外部到程序内部,这样就构成了一个输入流,在程序读取数据的某个时刻如下图所示:
在这里插入图片描述
2.2输出流
把当前程序中保存的数据输出到程序外部的目的源中,方向从程序内部到外部,这样就构成了输出流,程序输出数据的某个时刻如下图所示:
在这里插入图片描述
3.流的分类:
从本质上讲,流只分为字节流和字符流(字符流是为了解决字节流在读写文本时的乱码问题)
按操作种类又可以分为文件流,数据流,对象流,等等
字节流为输入输出提供了最原始的环境,字节流可以直接操作二进制数据
InputStream是定义Java的流式字节输入模式的抽象类,该类的所有方法在出错情况下都将引发IOException异常
OutputStream是定义流式字节输出的抽象类,所有该类的方法都返回一个void值并在出错条件下引发IOException异常
4.inputStream和outputStream体系结构
在这里插入图片描述
所谓节点流,就是在创建时,可直接指定目的地和源头的流,处理流创建时需要传进其他流才能指定源头,如下:Z , vnm,.

try {
			FileInputStream in = new FileInputStream("a.txt");//节点流
			ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream("b.txt"));//处理流
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

在这里插入图片描述
5.示例:
5.1.字节流

		FileInputStream in = null;
		FileOutputStream out = null;
		try {
			in = new FileInputStream("./a.txt");
			out = new FileOutputStream("./b.txt");
			int n = -2;
			while((n = in.read()) != -1) {
				System.out.print((char)n);
				out.write(n);
				out.flush();
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if(in != null) {
					in.close();
					in = null;
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

流使用完一定记得关闭,避免引起资源泄露!,read()方法返回值其实仍是字节,因为他就是按字节读的,但这块返回值是int,int是32位, Java中就是用-1来表示这个末尾的, 因为-1不会在数据中出现,如果返回byte, 同样无法表示流末尾., byte的取值范围是从-128到127,这个范围内所有的数据, 都有可能在数据中出现,read()方法需要返回一个特殊的值来表示流末尾, 这个值不能和流中的数据重复,read()方法内部将读取到的所有字节高位补0转为int返回, 这样做所有的数据都会是正数,这时就可以用-1表示流末尾了
5.2带缓冲(自己写)的字节流

	  FileInputStream in = null;
		FileOutputStream out = null;
		try {
			in = new FileInputStream("./ppt.rar");
			out = new FileOutputStream("./ppt2.rar");
			
			int n = -2;
			byte[] pool = new byte[1024];
			while((in.read(pool, 0, 1024)) != -1) {
				out.write(pool, 0, pool.length);
				out.flush();
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if(in != null) {
					in.close();
					in = null;
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

6.字符流体系结构
在这里插入图片描述
在这里插入图片描述
字符流使用示例
6.1字符流

	 FileReader reader = null;
		FileWriter writer = null;
		try {
			reader = new FileReader("./a.txt");
			writer = new FileWriter("./c.txt");
			int n = -2;
			while((n = reader.read()) != -1) {
				System.out.print((char)n);
				writer.write(n);
				writer.flush();
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if(reader != null) {
					reader.close();
					reader = null;
				}
				if(writer != null) {
					writer.close();
					writer = null;
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

6.2缓冲流示例

BufferedReader reader = null;
		BufferedWriter writer = null;
		try {
			reader = new BufferedReader(new FileReader("./a.txt"));
			writer = new BufferedWriter(new FileWriter("./d.txt"));
			String s = null;
			while((s = reader.readLine()) != null) {
				System.out.println(s);
				writer.write(s + "\n");
				writer.flush();
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if(reader != null) {
					reader.close();
					reader = null;
				}
				if(writer != null) {
					writer.close();
					writer = null;
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

6.3数据流

		 DataInputStream in = null;
		DataOutputStream out = null;
		String[] name = {"rose", "jack", "peter"};
		int[] age = {22, 24, 25};
		try {
			out = new DataOutputStream(new FileOutputStream("./f.txt"));	
			for(int i = 0; i < 3; i++) {
				out.writeUTF(name[i]);
				out.writeInt(age[i]);
				out.flush();
			}
			in = new DataInputStream(new FileInputStream("./f.txt"));
			for(int i = 0; i < 3; i++) {
				System.out.println(in.readUTF());
				System.out.println(in.readInt() + 1);
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if(in != null) {
					in.close();
					in = null;
				}
				if(out != null) {
					out.close();
					out = null;
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

6.4对象流

	ObjectInputStream in = null;
		ObjectOutputStream out = null;
		Student s = new Student("叮当", 500);
		try {
			out = new ObjectOutputStream(new FileOutputStream("./g.txt"));
			out.writeObject(s);
			out.flush();
			in = new ObjectInputStream(new FileInputStream("./g.txt"));
			Student temp = (Student)in.readObject();
			//System.out.println(temp.toString());
			PrintStream ps = System.out;
			ps.println(temp.toString());
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if(in != null) {
					in.close();
					in = null;
				}
				if(out != null) {
					out.close();
					out = null;
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

6.5标准流(控制台输入和输出得流)当需要字节字符转换时(例如:网络传输都是字节传输,但我们自己在电脑上看时,则需要将其转换成字符)

		BufferedReader reader = null;
		PrintStream out = null;
		reader = new BufferedReader(new InputStreamReader(System.in));
		out = System.out;
		String info = null;
		try {
			while(true) {
				info = reader.readLine();
				out.println(info);
				out.flush();
				if("再见!".equals(info)) {
					break;
				}
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

7.文件操作

		File f1 = new File("c:/02dev-c_space/test.txt");
		if(f1.exists()) {
			System.out.println("文件已存在,不能重复创建!");
			System.out.println(f1.isDirectory());
			System.out.println(f1.getAbsolutePath());
			System.out.println(f1.getName());
			System.out.println(f1.getParent());
			System.out.println(f1.getAbsoluteFile());
		} else {
			try {
				f1.createNewFile();
				System.out.println("新建文件成功!");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

对于文件的操作,java中也提供了一个类,来对文件进行随机访问
RandomAccessFile对象的文件位置指针遵循下面的规律:
① 新建RandomAccessFile的对象文件位置指针位于文件的开头处
② 每次读写操作之后,文件位置的指针都相应后移到读写的字节数
③ 可以通过getFilePointer方法来获得文件位置指针的位置,通过seek方法来设置文件指针的位置
如果某个文件有30个字节,读取数据过程中,从20-30读取,用skip( )跳过方法,但在读取的过程
中,前面的字节都被删除掉了,如果用户有这样的需求,先读取10-20字节,之后再读1-10之间的
数,再20-30之间
随机访问文件类 java.io.RandomAccessFile,所有已实现的接口:
Closeable, DataInput, DataOutput
在这里插入图片描述

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由半码博客整理,本文链接:https://www.bmabk.com/index.php/post/96873.html

(0)
小半的头像小半

相关推荐

半码博客——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!