基础JAVA IO
2017-07-07 13:11
302 查看
流
在程序中所有的数据都是以流的方式进行传输或保存的,程序需要数据的时候要使用输入流读取数据,而当程序需要将一些数据保存起来的时候,就要使用输出流完成。
程序中的输入输出都是以流的形式保存的,流中保存的实际上全都是字节文件。
在java.io包中操作文件内容的主要有两大类:字节流、字符流、两类都分为输入和输出操作。在字节流中输出数据主要是要使用OutputStream完成,输入使用InputStream,在字符流中输出主要是使用Writer类完成,输入主要使用Reader类完成。
内容操作就四个类:OutputStream、InputStream、Writer、Reader
操作流程
在JAVA中IO操作也是有相应步骤的,以文件操作为例,主要的操作流程如下:
A、 使用File类打开一个文件
B、 通过字节流或字符流的子类,指定输出的位置
C、 进行读/写操作
D、 关闭输入/输出
使用File类操作的时候一定要有路径的问题,注意分隔符。
实际上四个操作类都是抽象类
IO操作属于资源操作,对于资源操作,操作的最后必须关闭,否则就有可能出现未知的 错误。
1、字节流OutputStream/InputStream
2、字符流(Reader/Writer)
在程序中一个字符等于2个字节,那么JAVA提供了Reader、Writer连个专门操作字符流的类。
字符输出流:Writer
字符输入流:Reader
Writer本身只是一字符流的输出类,此类的定义如下:
public abstract class Writer extends Object implements Appendable, Closeable,Flushable
此类本身也是一个抽象类,如果要想使用此类,则肯定要使用其子类,此时如果是向文件中写入内容,所以应该使用FileWriter的子类。
FileWriter类的构造方法定义如下:
public FileWriter (File file) throws IOException
Writer类的常用方法
字符流的操作比字节流操作好在一点,可以直接输出字符串,不用进行转换操作。
字符输入流:Reader
Reader是使用使用字符的方式从文件中取出数据,Reader类的定义如下:
public abstract class Readerextends Object implements Readable, Closeable
Reader本身也是抽象类,如果现在要从文件中读取内容,则可以直接使用FileReader子类。
FileReader的构造方法定义如下:
public FileReader (File file)throws FileNotFoudException
Reader类的常用方法
字节流与字符流的区别是字节流在操作的时候本身是不会用到缓冲区(内存)的,是与文件本身直接操作的,而字符流在操作的时候是使用到缓冲区的。
注意:在所有的硬盘上保存文件或是进行传输的时候都是以字节的方式进行的。包括图片也是按字节完成,而字符只是在内存中才会形成,所以使用字节的操作最多。
3、内存操作流
4、管道流
在程序中所有的数据都是以流的方式进行传输或保存的,程序需要数据的时候要使用输入流读取数据,而当程序需要将一些数据保存起来的时候,就要使用输出流完成。
程序中的输入输出都是以流的形式保存的,流中保存的实际上全都是字节文件。
在java.io包中操作文件内容的主要有两大类:字节流、字符流、两类都分为输入和输出操作。在字节流中输出数据主要是要使用OutputStream完成,输入使用InputStream,在字符流中输出主要是使用Writer类完成,输入主要使用Reader类完成。
内容操作就四个类:OutputStream、InputStream、Writer、Reader
操作流程
在JAVA中IO操作也是有相应步骤的,以文件操作为例,主要的操作流程如下:
A、 使用File类打开一个文件
B、 通过字节流或字符流的子类,指定输出的位置
C、 进行读/写操作
D、 关闭输入/输出
使用File类操作的时候一定要有路径的问题,注意分隔符。
实际上四个操作类都是抽象类
IO操作属于资源操作,对于资源操作,操作的最后必须关闭,否则就有可能出现未知的 错误。
1、字节流OutputStream/InputStream
public class Main01 { public static void main(String[] args) { File file = new File("src//JavaIO/test.txt"); try { //OutputStream out = new FileOutputStream(file); OutputStream out = new FileOutputStream(file,true);//追加方式打开文件 String str = "\nhello word kkk!";//字符串中添加换行 byte b[] = str.getBytes(); out.write(b); out.close(); InputStream input = new FileInputStream(file); //byte b2[] = new byte[1024];第一种方式定义接收文件内容的二进制变量,固定值造成浪费。 byte b2[] = new byte[(int)file.length()];//第二种根据file长度定义二进制变量大小。 //第一种读取文件内容的方式:input。read(b2); //int len = input.read(b2);//不计算len,输出的内容就是定义的1024,太长了 //第二种读取方式 /*for(int i=0;i<b2.length;i++){ b2[i]=(byte)input.read(); }*/ //第三种读取方式 int temp = 0; int len = 0 ; while((temp=input.read())!=-1){ b2[len++]=(byte)temp; } input.close(); //System.out.print("文件内容:"+new String(b2,0,len)); System.out.print("文件内容:"+new String(b2)); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
2、字符流(Reader/Writer)
在程序中一个字符等于2个字节,那么JAVA提供了Reader、Writer连个专门操作字符流的类。
字符输出流:Writer
字符输入流:Reader
Writer本身只是一字符流的输出类,此类的定义如下:
public abstract class Writer extends Object implements Appendable, Closeable,Flushable
此类本身也是一个抽象类,如果要想使用此类,则肯定要使用其子类,此时如果是向文件中写入内容,所以应该使用FileWriter的子类。
FileWriter类的构造方法定义如下:
public FileWriter (File file) throws IOException
Writer类的常用方法
No. | 方法或常量 | 类型 | 描述 |
1 | public abstract void close () throws IOException | 普通 | 关闭输出流 |
2 | public void write (String str) throws IOException | 普通 | 将字符串输出 |
3 | public void write (char [] cbuf) throws IOException | 普通 | 将字符数组输出 |
4 | public abstract void flush () throws IOException | 普通 | 强制清空缓存 |
String str ="hello world!!!"; byteb[] =str.getBytes(); out.write(b); | String str ="Hello World!!!"; out.write(str); |
Reader是使用使用字符的方式从文件中取出数据,Reader类的定义如下:
public abstract class Readerextends Object implements Readable, Closeable
Reader本身也是抽象类,如果现在要从文件中读取内容,则可以直接使用FileReader子类。
FileReader的构造方法定义如下:
public FileReader (File file)throws FileNotFoudException
Reader类的常用方法
No. | 方法或常量 | 类型 | 描述 |
1 | public abstract void close () throws IOException | 普通 | 关闭输出流 |
2 | public int read () throws IOException | 普通 | 读取单个字符 |
3 | public int read (char [] cbuf) throws IOException | 普通 | 读到字符数组中,返回长度 |
public class Main02 { public static void main(String[] args) { File file = new File("src//JavaIO//test.txt"); try { //Reader reader = new FileReader(file); //使用字符字节流转换 Reader reader = new InputStreamReader(new FileInputStream(file)); char[] ch = new char[(int)file.length()]; //读取也是三种方式 reader.read(ch); /*int len = 0; int temp = 0; while((temp=reader.read())!=-1){ ch[len++]=(char)temp; }*/ reader.close(); System.out.println("Main02文件读取:"+new String(ch)); String str = "what can I do for you?"; //Writer writer = new FileWriter(file,true);//写入文件 Writer writer = new OutputStreamWriter(new FileOutputStream(file,true)); writer.write(str); writer.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
字节流与字符流的区别是字节流在操作的时候本身是不会用到缓冲区(内存)的,是与文件本身直接操作的,而字符流在操作的时候是使用到缓冲区的。
注意:在所有的硬盘上保存文件或是进行传输的时候都是以字节的方式进行的。包括图片也是按字节完成,而字符只是在内存中才会形成,所以使用字节的操作最多。
3、内存操作流
public class Main03 { public static void main(String[] args){ String str = "HELLO WORD"; ByteArrayInputStream bais = new ByteArrayInputStream(str.getBytes()); ByteArrayOutputStream baos = new ByteArrayOutputStream(); int temp = 0; while((temp=bais.read())!=-1){ baos.write(Character.toLowerCase((char)temp)); } System.out.println(baos.toString()); try { bais.close(); baos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
4、管道流
class Send implements Runnable{//线程类 private PipedOutputStream pos = null;//管道输出流 public Send(){ this.pos = new PipedOutputStream();//实例化输出流 } @Override public void run() { // TODO Auto-generated method stub String str = "hello word!!!!"; try { this.pos.write(str.getBytes()); this.pos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public PipedOutputStream getPos(){ return this.pos; } } class Receive implements Runnable{ private PipedInputStream pis = null; public Receive(){ this.pis = new PipedInputStream(); } @Override public void run() { // TODO Auto-generated method stub byte b[] = new byte[1024];//接收内容 int len = 0; try { len = this.pis.read(b); this.pis.close(); System.out.println(new String(b,0,len)); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public PipedInputStream getPis(){ return this.pis; } } public class Main04 { public static void main(String args[]){ Send s = new Send(); Receive r = new Receive(); try { s.getPos().connect(r.getPis()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } new Thread(s).start(); new Thread(r).start(); } }
相关文章推荐
- Java IO学习基础之读写文本文件
- Java IO学习基础之读写文本文件
- [零基础学JAVA]Java SE应用部分-31.Java IO操作(05)IO操作实例讲解之实现简单MIS 推荐
- java基础整理9--IO
- java IO基础学习
- Java IO学习基础之读写文本文件
- JAVA基础------IO操作(一)
- java基础教程之IO使用一
- java IO详解 Java基础 很重要
- [零基础学JAVA]Java SE应用部分-30.Java IO操作(04) 推荐
- Java IO学习基础之读写文本文件
- Java IO学习基础之读写文本文件
- JAVA基础IO介绍
- Java基础---(io和xml)
- [零基础学JAVA]Java SE应用部分-27.Java IO操作(01) 推荐
- Java IO学习基础之读写文本文件
- JAVA基础:使用缓冲IO提高Java程序性能
- Java IO学习基础之读写文本文件
- java IO基础概念
- 黑马程序员_java基础视频第21天_JavaIO续集3