程序-读
package IOStudy;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
/**
* 处理数据角度
* 字节流:音频,视频,excel
* 字符流:文本
*
* 能使用字符流就能使用字节流,但能使用字节流不一定能使用字符流.
*
* 以程序为中心,进来叫输入,出去叫输出
*
* 节点流,处理流
* 始终处于第一线的是节点流,为了提升性能我们包装叫做处理流,没有节点流处理流发挥不了任何的作用.
*
* 节点流
* ByteArray../File..
*
* 处理流
* DataInputStream/BufferedInputStream/Object..
*
* 四个抽象类
* InputStream 字节输入流的父类,数据单位为字节 int read()/void close()
* OutputStream 字节输出流的父类,数据单位为字节 void write(int)/void flush()/void close()
* Reader 字符输入流的父类,数据单位为字符 int read()/void close()
* Writer 字符输出流的父类,数据单位为字符 void write(String)/void flush()/void close()
*
* Java虚拟机不直接对接文件,只能向操作系统进行申请
* Java->OS->文件
* Closeable方法通知操作系统可以关闭这个资源
*
* OutputStream
* Closeable释放资源,Flushable(手动刷新) 避免数据驻留在内存中
*
*
* FileOutputStream处理一切
* FileWriter处理字符
* @author pmc
*/
/**
* 第一个程序:理解操作步骤
* 分段读取
* 1.创建源
* 2.选择流
* 3.操作
* 4.释放资源
* @author pmc
*
*/
public class IOAbsrtractTest5 {
public static void main(String[] args){
//1.创建源
File src=new File("txt.txt");
//2.选择流
InputStream is=null;
try {
is=new FileInputStream(src);
//3.操作
// int temp=is.read();
// System.out.println(temp);
byte[] flush=new byte[1024];//缓冲容器
int len=0;//接收长度
while((len=is.read(flush))!=-1){//is.read(byte[]) 返回一个读取长度 源码: return read(b, 0, b.length);
// System.out.print(len);
String str=new String(flush,0,len);
System.out.print(str);
}
// System.out.print(len);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
//4.释放资源
try {
if(is!=null){//避免空指针异常
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
程序-写
package IOStudy;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
* 处理数据角度
* 字节流:音频,视频,excel
* 字符流:文本
*
* 能使用字符流就能使用字节流,但能使用字节流不一定能使用字符流.
*
* 以程序为中心,进来叫输入,出去叫输出
*
* 节点流,处理流
* 始终处于第一线的是节点流,为了提升性能我们包装叫做处理流,没有节点流处理流发挥不了任何的作用.
*
* 节点流
* ByteArray../File..
*
* 处理流
* DataInputStream/BufferedInputStream/Object..
*
* 四个抽象类
* InputStream 字节输入流的父类,数据单位为字节 int read()/void close()
* OutputStream 字节输出流的父类,数据单位为字节 void write(int)/void flush()/void close()
* Reader 字符输入流的父类,数据单位为字符 int read()/void close()
* Writer 字符输出流的父类,数据单位为字符 void write(String)/void flush()/void close()
*
* Java虚拟机不直接对接文件,只能向操作系统进行申请
* Java->OS->文件
* Closeable方法通知操作系统可以关闭这个资源
*
* OutputStream
* Closeable释放资源,Flushable(手动刷新) 避免数据驻留在内存中
*
*
* FileOutputStream处理一切
* FileWriter处理字符
* @author pmc
*/
/**
* 文件字节输出流
* 1.创建源
* 2.选择流
* 3.操作
* 4.释放资源
* @author pmc
*
*/
public class IOAbsrtractTest6 {
public static void main(String[] args){
//1.创建流
File src=new File("dest.txt");
//2.选择流
OutputStream os=null;
try{
os=new FileOutputStream(src,true);//自动创建该文件,true追加,false覆盖
//3.写内容
String msg="io is so easy ";
byte[] by=msg.getBytes();
os.write(by,0,by.length);
os.flush();
//3.1读
InputStream in=new FileInputStream(src);
byte[] temp=new byte[1024];
int s=0;
s=in.read(temp);
String str=new String(temp,0,s);
System.out.println(str);
in.close();
}catch(FileNotFoundException e){
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
//4.释放资源
if(os!=null){
try{
os.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
}
}
程序-写 练习
package IOStudy;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class IOAbsrtractTest8 {
public static void main(String[] args){
File src=new File("temp.txt");
OutputStream out=null;
try {
out=new FileOutputStream(src,true);
String str="学习使我快乐\n";
out.write(str.getBytes());
out.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}