Java基础-打印流和合并流、分割与管道流、RandomAccessFile及其它流

打印流:

打印流提供了打印方法,可以将各种数据类型的数据都原样打印。

字节打印流:
PrintStream
构造函数可以接收的参数类型:
1,file对象。File
2,字符串路径。String
3,字节输出流。OutputStream
字符打印流:
PrintWriter
构造函数可以接收的参数类型:
1,file对象。File
2,字符串路径。String
3,字节输出流。OutputStream
4,字符输出流,Writer。
 

打印流有自己的PrintWriter.println();等方法,把标准输入流写到文件中。

代码:

import java.io.*;

public class code
{
	public static void main(String[] args) throws IOException
	{
		BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
		PrintWriter out = new PrintWriter(new FileWriter("b.txt"),true);

		String line = null;

		while ((line = bufr.readLine())!= null)
		{
			if (line.equals("over"))
			{
				break;	
			}
			out.println(line.toUpperCase());
			//out.flush();
		}
		out.close();
		bufr.close();
	}
}

合并流:

SequenceInputStream是能对多个流进行合并成一个读取流,它在构造时需要传入Enumeration,而这个只用Vector中有,所以这个多个读取流要加入Vector集合中。注意:它只是对读取流进行合并。

它使用步骤:
1.创建Vector<InputStream>
2.将要合并的InputStream加入Vector
3.通过Vector获取Enumeration

4.创建SequenceInputStream,将Enumeration作为参数传入。

代码:

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

public class code
{
	public static void main(String[] args) throws IOException{
		Vector<FileInputStream> v = new Vector<FileInputStream>();
		
		v.add(new FileInputStream("c:\\1.txt"));
		v.add(new FileInputStream("c:\\2.txt"));
		v.add(new FileInputStream("c:\\3.txt"));

                Enumeration<FileInputStream> en = v.elements();
		
		SequenceInputStream sis = new SequenceInputStream(en);
		
		FileOutputStream fos = new FileOutputStream("c:\\4.txt");
		
		byte[] buf = new byte[1024];
		int len = 0;
		while((len=sis.read())!=-1) {
			fos.write(buf,0,len);
		}
		fos.close();
		sis.close();
		
	}
}

切割文件  代码:

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

public class code
{
	public static void main(String[] args) throws IOException{
		splitFlie();
	}
	
	public static void merge() throws IOException {
		ArrayList<FileInputStream> al = new ArrayList<FileInputStream>();
		for(int i=1; i<=3 ;i++) {
			al.add(new FileInputStream("c:\\splitfile\\"+i+",part"));
		}
		
		Iterator<FileInputStream> it = al.iterator();
		Enumeration<FileInputStream> en = new Enumeration<FileInputStream>() {
			public FileInputStream nextElement() {
				
				return it.next();
			}
			//枚举
			public boolean hasMoreElements() {
				return it.hasNext();
			}
		};
		SequenceInputStream sis = new SequenceInputStream(en);
		
		FileOutputStream fos = new FileOutputStream("c:\\splitfile\\0.bmp");
		
		byte[] buf = new byte[1024];
		int len = 0;
		while((len=sis.read())!=-1) {
			fos.write(buf,0,len);
		}
		fos.close();
		sis.close();
	}
	
	public static void splitFlie() throws IOException {
		FileInputStream fis = new FileInputStream("c:\\1.bmp");
		FileOutputStream fos = null;
		
		byte[] buf = new byte[1024*1024];
		int len = 0,count = 1;
		while((len=fis.read())!=-1) {
			fos = new FileOutputStream("c:\\splitflie\\"+(count++)+".part");
			fos.write(buf,0,len);
			fos.close();
		}
		fis.close();
	}
}
管道流:
PipedInputStream和PipeOutputStream
RandowAccessFile
随机访问文件,自身具备读写的方法。
通过skipBytes(int x),seek(int x)来达到随机访问。

该类不算是IO体系中的子类。而是直接继承自Object。但是他是IO包中成员。因为他具备读和写功能。
内部封装了一个数组,而是通过指针对数组的元素进行操作。可以通过getFilePointer获取指针位置。
同时可以通过seek改变指针的位置。其实完成读写的原理就是内部封装了字节输入流和输出流。
通过构造函数可以看出,该类只能操作文件。

管道流和多线程一起使用。

代码:

import java.io.*;
class Read implements Runnable
{
	//定义输入管道
	private PipedInputStream in;
	Read(PipedInputStream in)
	{
		this.in = in;
	}
	public void run()
	{
		try
		{
			byte[] buf = new byte[1024];//定义一个数组。
			System.out.println("读取前没有数据.......堵塞");
			int len = in.read(buf);//调用read方法。
			System.out.println("读取数据.......堵塞结束");
			String s = new String(buf,0,len);
			System.out.println(s);
			in.close();
		}
		catch (IOException e)
		{
			throw new RuntimeException("管道流读取失败");
		}
	}
}
class Write implements Runnable
{
	//定义输出管道。
	private PipedOutputStream out;
	Write(PipedOutputStream out)
	{
		this.out = out;
	}
	public void run()
	{
		try
		{
			System.out.println("开始写入数据.......等待");
			Thread.sleep(6000);
			out.write("piped lai la".getBytes());
			out.close();
		}
		catch (Exception e)
		{
			throw new RuntimeException("管道输出流失败");
		}
	}
}
public class code
{
	public static void main(String[] args) throws IOException
	{
		PipedInputStream in = new PipedInputStream();
		PipedOutputStream out = new PipedOutputStream();
		in.connect(out);//读取流和输入流连接

		Read r = new Read(in);
		Write w = new Write(out);
		//创建线程。
		new Thread(r).start();
		new Thread(w).start();
	}
}
RandomAccessFile:
随即访问文件,自身具备读写的方法。

通过skipBytes(int x),seek(int x)来达到随机访问。

存在指向该隐含数组的光标或索引,称为文件指针;输入操作从文件指针开始读取字节,并随着对字节的读取而前移此文件指针。


RandomAccessFile这个类从名字就可以看出:不是IO体系总的子类,它的父类是Object
特点:
1.既能读,又能写。
2.该对象内部维护了一个大型byte数组,可以通过指针对数组元素进行操作
3.可以通过getFilePointer获取指针;通过seek方法设置指针位置
4.其实该对象就是对字节输入流和输出流进行了封装
5.该对象的源或者目的只能是文件,通过构造方法可以看出

代码:

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

public class code
{
	public static void main(String[] args) throws IOException{
		writeFile();
		readFile();
	}
	
	public static void readFile() throws IOException {
		RandomAccessFile raf = new RandomAccessFile("demo.txt", "r");
		
		//调整对象中指针
		raf.seek(8*1);
		
		//跳过指定的字节数
		//raf.skipBytes(8);
		
		byte[] buf = new byte[4];
		raf.read(buf);
		String name = new String(buf);
		
		int age = raf.readInt();
		System.out.println("name:"+name);
		System.out.println("age:"+age);
		
		raf.close();
	}
	
	public static void writeFile() throws IOException {
		RandomAccessFile raf = new RandomAccessFile("demo.txt", "rw");
		
		raf.write("张三".getBytes());
		raf.writeInt(97);
		raf.write("李四".getBytes());
		raf.writeInt(98);
		
		raf.close();
	}
}

DataInputStream和DataOutputStream:
数据输入流允许应用程序以与机器无关方式从底层输入流中读取基本 Java数据类型。

数据输出流允许应用程序以适当方式将基本 Java数据类型写入输出流中。
然后,应用程序可以使用数据输入流将数据读入。可以用于操作基本数据类型的数据流对象。

代码:

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

public class code {
	 public static void main(String[] args) throws IOException {
         writeDemo();
         readDemo();
     }
 
     public static void readDemo() throws IOException {
         DataInputStream dis=new DataInputStream(new FileInputStream("demo.txt"));
         String str=dis.readUTF();
         dis.close();
         System.out.println(str);
     }
 
     public static void writeDemo() throws IOException {
         DataOutputStream dos=new DataOutputStream(new FileOutputStream("demo.txt"));
         dos.writeUTF("你好!");
         dos.close();
     }
}

操作字节数组的流对象:
ByteArrayInputStream:在构造的时候,需要接受数据源,而且数据源是一个字节数组。
ByteArrayOutputStream:在构造的时候,不用定义数据长度,因为该对象中已经内部封装了可变长度的字节数组。就是数据目的地。
因为这两个流对象都操作的数组,并没有使用系统资源。所以,不用进行close关闭。

代码:

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

public class code
{
	public static void main(String[] args) {
	    ByteArrayInputStream bais=new ByteArrayInputStream("Hello!".getBytes());
	    ByteArrayOutputStream baos=new ByteArrayOutputStream();
	    int ch=0;
       	    while((ch=bais.read())!=-1){
          	baos.write(ch);
	    }
	    System.out.println(baos.toString());//Hello!
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值