I/O流之字节流

   在程序中所有的数据都是以流的形式进行传输或保存的,程序需要数据时要使用输入流读取数据,而当程序需要将一些数据保存起来时,就要使用输出流完成对于操作文件内容,要进行文件内容的操作就需要通过Java提供的两组数据流的操作类完成

   字节操作流(byte):OutputStream,InputStream;

   字符操作流(char):Writer,Reader

   对于资源内容的操作,都会按照如下的步骤执行

   (1)如果要操作的是文件,那么首先要通过File类对象找到一个要操作的文件路径

   (2)通过字节流或者字符流的子类为字节流或字符流的对象实例化

   (3)执行读写操作

   (4)最后一定要关闭操作的资源。

1.字节输出流

public class test2 {
   public static void main(String[] args) throws IOException {
	File file=new File("D:"+File.separator+"hellodemo"+File.separator+"test.txt");//定义文件路径
	if(!file.getParentFile().exists()) {//判断父路径是否存在
		file.getParentFile().mkdirs();//创建父路径
		}
	OutputStream output=new FileOutputStream(file);//通过子类实例化父类
	String data="Hello world";
	output.write(data.getBytes());//将数据变为字节数组输出
	output.close();
}
}

 关于OutputStream定义的三个输出方法

1.public abstract void write(int b) throws IOException
2.public void write(byte[] b) throws IOException
3.public void write(byte[] b,int off,int len)throws IOException //从off开始,len结束

 FileOutputStream的构造方法

1.public FileOutStream(File file) throws FileNotFound Exception//传入file实例构成文件输出流
2.public FileOutStream(File file,boolean append)throws FileNotFoundException//用于追加数据

 追加数据

OutputStream output=new FileOutputStream(file,true);//通过子类实例化父类,设置追加为true
     String data="\r\ncaizhen 666";//   /r/n表示换行 output.write(data.getBytes());    //写入 output.close();     

 输出部分数据

output.write(data.getBytes(),0,5);

 2.字节输入流

1.public abstract int read()throws IOException//读取单个字符,如果读到结尾返回-1
2.public int read(byte[] b)throws IOException//读取多个字符,如果需要读取的数据小于byte的数据,返回的是数据的个数,如果不是且已经读完则返回-1
3.public int read(byte[] b,int off,int len)throws IOException

2.1 一次性全部读取

public class test2 {
   public static void main(String[] args) throws IOException {
	File file=new File("D:"+File.separator+"hellodemo"+File.separator+"test.txt");//定义文件路径
	if(file.exists()) {//判断文件是否存在
	   InputStream input=new FileInputStream(file);
	   byte data[]=new byte[1024];//假设要读的长度是1024
	   int len=input.read(data);//读取数据,返回读取个数
	   input.close();
	   System.out.println("读取的数据是:"+new String(data,0,len));
	}
}
}

 2.2单个字节读取

public class test2 {
   public static void main(String[] args) throws IOException {
	File file=new File("D:"+File.separator+"hellodemo"+File.separator+"test.txt");//定义文件路径
	if(file.exists()) {//判断文件是否存在
	   InputStream input=new FileInputStream(file);
	   byte data[]=new byte[1024];//假设要读的长度是1024
	   int temp=0;
	   int foot=0;
	   while((temp=input.read())!=-1) {//表示未读取完毕
		   data[foot++]=(byte)temp;
	   }
	   input.close();
	   System.out.println("读取的数据是:"+new String(data,0,foot));
	}
}
}

 程序解读:

1.定义文件路径

2.判断文件是否存在,若存在则执行

3.利用FileInputStream的构造方法传入file实例获得InputStream对象

4.新建一个长度为1024的字节数组

5.利用InputStream对象的read()方法,读取一个字节,并且把变量赋值给我temp

6.当temp=-1时跳出循环,表示读取完毕

7.利用字符串的构造方法生成字符串并打印

 3.部分读取

       InputStream input=new FileInputStream(file);
	   byte data[]=new byte[1024];//假设要读的长度是1024
	   int len=input.read(data,0,3);
	   input.close();
	   System.out.println("读取的数据是:"+new String(data,0,len));

  

猜你喜欢

转载自www.cnblogs.com/cainame/p/10420779.html