4.5 Java进阶(五)I/O流

一、流的概念和作用

1、在JAVA中,流是指传输的数据。

2、流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象。即数据在两设备间的传输称为流,流的本质是数据传输,根据数据传输特性将流抽象为各种类,方便更直观的进行数据操作。

二、流的分类

1、输入流:外部设备-->内存,只能进行读操作。

2、输出流:内存-->外部设备,只能进行写操作。

3、字符流:通过内存把数据写入到外部设备。

(1)通过内存向外部设备的文件写入数据的时候,如果该文件不存在,系统帮忙创建。

(2)代码实例1:

@SuppressWarnings({"resource" })
	@Test
	public void test01() throws IOException
	{
		/*
		 * 字符流
		 * 通过内存把数据写入到外部设备
		 * c:\\  =   c:/
		 * 通过内存向外部设备的文件写入数据的时候,如果该文件不存在,系统帮忙创建
		 */
		Writer writer=new FileWriter("D:\\student\\text_test.txt");
		writer.write("好好学习,天天上网!");
		writer.close();
	}

(3)代码实例2:

@SuppressWarnings({"resource" })
	@Test
	public void test02() throws IOException
	{
		/*
		 * 字符流
		 * 把外部设备的文件里的数据读取到内存中
		 * char类型的数组,其中的元素没有赋值之前,判断是否为空用利用==0判断
		 */
		Reader reader=new FileReader("D:\\student\\text_test.txt");
		char[] c=new char[100];
		int num=reader.read(c);
		System.out.println(num);
		for(char h:c)
		{
			if(h!=0)
			{
				System.out.println(h);
			}
		}
		
		reader.close();
	}

4、字节流:把外部设备的文件里的数据读取到内存中。

(1)char类型的数组,其中的元素没有赋值之前,判断是否为空用利用==0判断。

(2)代码实例1:

@SuppressWarnings({ "unused", "resource" })
	@Test
	public void test03() throws IOException
	{
		/*
		 * 字节流
		 * 输入到内存
		 * 字节类型的数组判断字节是否为空使用==0
		 */
		InputStream is=new FileInputStream("D:\\student\\text_test.txt");
		byte[] b=new byte[100];
		int num=is.read(b);
		for(byte y:b)
		{
			if(y!=0)
			{
				System.out.println(y);
			}
		}
		
		is.close();
	}

(3)代码实例2:

@SuppressWarnings({ "unused", "resource" })
	@Test
	public void test04() throws IOException
	{
		/*
		 * 字节流
		 * 输出到外部设备
		 */
		InputStream is=new FileInputStream("D:\\student\\text_test.txt");
		byte[] b=new byte[100];
		int num=is.read(b);
		
		OutputStream os=new FileOutputStream("D:\\student\\demo.txt");
		os.write(b);
		
		is.close();
		os.close();
	}

(4)代码实例3:

@SuppressWarnings("unused")
	@Test
	public void test05()
	{
		File file=new File("D:\\student\\demo\\demo");
//		System.out.println(file.getName());//获得文件名
//		System.out.println(file.getParent());//获得该文件所在路径
//		System.out.println(file.getParentFile());//获得该文件所在路径
//		System.out.println(file.getPath());//绝对路径
//		System.out.println(file.isDirectory());
		file.mkdirs();//创建目录包括其子目录(创建多级目录)
		file.mkdir();//创建一级目录
	}

(5)代码实例4:

@Test
	public void test06() throws IOException
	{
		File file=new File("D:\\student\\text_test.txt");
		file.createNewFile();//创建新文件
		System.out.println(file.exists());//判断指定路径下的问价你是否存在
		file.delete();//删除指定路径下的文件
	}

5、对象流

(1)代码实例1:

@Test
	public void test07() throws Exception
	{
		/*
		 * 对象流
		 */
		//把对象写入文件
		List<Student> list = new ArrayList<Student>();
		Student stu1 = new Student(1001,"admin");
		Student stu2 = new Student(1002,"root");
		Student stu3 = new Student(1003,"scoot");
		list.add(stu1);
		list.add(stu2);
		list.add(stu3);
		//对象流操作,直接写入或者读入对象
		FileOutputStream fos = new FileOutputStream("D:\\student\\demo.txt");
		ObjectOutputStream oos = new ObjectOutputStream(fos);
		//可以直接写出对象
		oos.writeObject(list);
		oos.flush();
		oos.close();
	}

(2)代码实例2:

@Test
	public void test08() throws Exception
	{
		FileInputStream fis = new FileInputStream("D:\\student\\demo.txt");
		ObjectInputStream ois = new ObjectInputStream(fis);
		@SuppressWarnings("unchecked")
		List<Student> stuList = (ArrayList<Student>)ois.readObject();
		ois.close();
		for (int i = 0; i < stuList.size(); i++) 
		{
			Student stu = stuList.get(i);
			System.out.println(stu.getId()+"   "+stu.getName());
		}
	}

6、字符缓冲流

@SuppressWarnings("unused")
	@Test
	public void test09() throws Exception
	{
		/*
		 * 字符缓冲流
		 * 读取数据
		 */
		List<String> ls=new ArrayList<String>();
		Reader is = new FileReader("D:\\student\\demo.txt");
		BufferedReader br=new BufferedReader(is);
		String str=null;
		while((str=br.readLine())!=null)
		{
			System.out.println(str);
			ls.add(str);
		}
		br.close();
		
		Writer wr=new FileWriter("D:\\student\\text_test.txt");
//		BufferedWriter bw=new BufferedWriter(wr);
//		for(String s:ls)
//		{
//			bw.write(s+"\\");
//		}
		for(String s:ls)
		{
			wr.write(s);
		}
		
		//bw.close();
		wr.close();
	}

7、字节缓冲流

@Test
	public void test10() throws Exception
	{
		/*
		 * 字节缓冲流
		 */
		OutputStream out=new FileOutputStream("D:\\student\\text_test.txt");
		 //根据字节输出流构建字节缓冲流  
		BufferedOutputStream buf=new BufferedOutputStream(out);
		String data="好好学习,天天向上";
		buf.write(data.getBytes());//写入缓冲区
		buf.flush();
		
		buf.close();
		out.close();
	}

8、其它流类库

猜你喜欢

转载自blog.csdn.net/qq_40254536/article/details/81132614
4.5