Java I/O
2015-11-25 05:56
549 查看
Java uses the concept of stream to make I/O operation fast.
Stream
A stream is a sequence of data.In Java a stream is composed of bytes
1) System.out: standard output stream
2) System.in: standard input stream
3) System.err: standard error stream
Let’s see the code to get input from console.
OutputStream
Java application uses an output stream to write data to a destination, it may be a file,an array,peripheral device or socket.
write data–>a destination
InputStream
Java application uses an input stream to read data from a source, it may be a file,an array,peripheral device or socket.
a source –>read data
FileInputStream and FileOutputStream (File Handling)
are used to read and write data in file.
//把String转化为Byte->写入文档
Java FileInputStream class
Java FileInputStream class obtains input bytes from a file.It is used for reading streams of raw bytes such as image data. For reading streams of characters, consider using FileReader.
It should be used to read byte-oriented data for example to read image, audio, video etc.
Example of Reading the data of current java file and writing it into another file
We can read the data of any file using the FileInputStream class whether it is java file, image file, video file etc. In this example, we are reading the data of C.java file and writing it into another file M.java.
ByteArrayOutputStream
Java ByteArrayOutputStream class is used to write data into multiple files. In this stream, the data is written into a byte array that can be written to multiple stream.
Stream
A stream is a sequence of data.In Java a stream is composed of bytes
1) System.out: standard output stream
2) System.in: standard input stream
3) System.err: standard error stream
Let’s see the code to get input from console.
int i=System.in.read();//returns ASCII code of 1st character System.out.println((char)i);//will print the character
OutputStream
Java application uses an output stream to write data to a destination, it may be a file,an array,peripheral device or socket.
write data–>a destination
InputStream
Java application uses an input stream to read data from a source, it may be a file,an array,peripheral device or socket.
a source –>read data
FileInputStream and FileOutputStream (File Handling)
are used to read and write data in file.
import java.io.*; class Test{ public static void main(String args[]){ try{ FileOutputstream fout=new FileOutputStream("abc.txt"); //读取文件 String s="Sachin Tendulkar is my favourite player"; byte b[]=s.getBytes();//converting string into byte array fout.write(b); fout.close(); System.out.println("success..."); }catch(Exception e){system.out.println(e);} } }
//把String转化为Byte->写入文档
Java FileInputStream class
Java FileInputStream class obtains input bytes from a file.It is used for reading streams of raw bytes such as image data. For reading streams of characters, consider using FileReader.
It should be used to read byte-oriented data for example to read image, audio, video etc.
import java.io.*; class SimpleRead{ public static void main(String args[]){ try{ FileInputStream fin=new FileInputStream("abc.txt"); int i=0; while((i=fin.read())!=-1){ System.out.println((char)i); } fin.close(); }catch(Exception e){system.out.println(e);} } }
Example of Reading the data of current java file and writing it into another file
We can read the data of any file using the FileInputStream class whether it is java file, image file, video file etc. In this example, we are reading the data of C.java file and writing it into another file M.java.
import java.io.*; class C{ public static void main(String args[])throws Exception{ FileInputStream fin=new FileInputStream("C.java"); FileOutputStream fout=new FileOutputStream("M.java"); int i=0; while((i=fin.read())!=-1){ fout.write((byte)i); } fin.close(); } }
ByteArrayOutputStream
Java ByteArrayOutputStream class is used to write data into multiple files. In this stream, the data is written into a byte array that can be written to multiple stream.
import java.io.*; class S{ public static void main(String args[])throws Exception{ FileOutputStream fout1=new FileOutputStream("f1.txt"); FileOutputStream fout2=new FileOutputStream("f2.txt"); ByteArrayOutputStream bout=new ByteArrayOutputStream(); bout.write(139); bout.writeTo(fout1); bout.writeTo(fout2); bout.flush(); bout.close();//has no effect System.out.println("success..."); } }
相关文章推荐
- 2015年11月25 Java基础系列(二)Thread Runnable线程初级讲解
- java 随机访问 RandomAccessFile
- java四舍五入保留两位小数
- java 对象流
- Java设计模式——观察者模式
- Java设计模式——观察者模式
- Swagger+Spring mvc生成Restful接口文档
- java 的反射机制
- 浅谈Java中的深拷贝和浅拷贝(转载)
- maven构建命令,maven本地工厂的创建,maven骨架的生成,以及在eclipse里创建maven工程
- Java内部类详解
- Java中的String类
- spring定时任务初体验
- JAXB 解决XML与JavaBean的互换
- eclipse常识
- 基于注解的Spring AOP的配置和使用
- Struts2整合之CKeditor和CKfinder
- Spring3之Bean的自动装配
- Struts2配置精要之defaultStack与paramsPrepareParamsStack
- Spring3核心技术之DI依赖注入