java操作流的对象都在io包下。
IO流的用途:用来处理设备之间的数据传输。
IO流的分类:
按流向分:
输入流(读取数据)
输出流(写数据)
按数据类型分:
字符流
字符输入流 Reader
字符输出流 Writer
字节流
字节输入流 InputStream
字节输出流 OutputStream
FileOutputStream(字节输出流)的使用:
package Stream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileOutputStreamDemo {
public static void main(String[] args) {
String s = "hello world";
FileOutputStream fos = null;
try {
// 如果文件不存在会自动创建
fos = new FileOutputStream("E:\\fos.txt");
// 追加写入
// FileOutputStream fos = new FileOutputStream("E:\\fos.txt",true);
fos.write(s.getBytes());
// 换行符 windows \r\n linux \n Mac \r
fos.write("\r\n".getBytes());
fos.write(s.getBytes());
// 为了让代码移植性更强,
//我们用java给的方法智能获取系统换行符
fos.write(System.getProperty("line.separator").getBytes());
fos.write(s.getBytes());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
// 为了保证close一定可以执行所以放在finally中
if(fos!=null){
try {
// 释放资源
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
FileInputStream(字节输入流)的使用:
package Stream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class FileInputStreamDemo {
public static void main(String[] args) {
FileInputStream fis = null;
try {
fis = new FileInputStream("E:\\fos.txt");
// 一次读取一个字节数组
byte[] bys = new byte[1024];
int len = 0;
while((len = fis.read(bys))!=-1){
System.out.print(new String(bys,0,len));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(fis!=null){ try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}}
}
}
BufferedOutputStream 高效字节输出流
package Stream;
import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class BufferedOutputStreamDemo {
public static void main(String[] args) {
BufferedOutputStream bos = null;
try {
bos = new BufferedOutputStream(new FileOutputStream("E:\\bos.txt"));
bos.write("hello world hello java".getBytes());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(bos!=null){ try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}}
}
}
package Stream;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class BufferedInputStreamDemo {
public static void main(String[] args) {
BufferedInputStream bis = null;
try {
bis = new BufferedInputStream(new FileInputStream("E:\\bos.txt"));
byte[] bys = new byte[1024];
int len = 0;
while((len = bis.read(bys))!=-1){
System.out.print(new String(bys,0,len));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(bis!=null){
try {
bis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
OutputStreamWriter 字符输出流
package Stream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
public class OutputStreamWriterDemo {
public static void main(String[] args) throws IOException {
OutputStreamWriter osw = new OutputStreamWriter(new
FileOutputStream("E:\\osw.txt"));
osw.write("我喜欢java");
osw.close();
}
}
package Stream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
public class InputStreamReaderDemo {
public static void main(String[] args) throws IOException {
InputStreamReader isr = new InputStreamReader(new FileInputStream("E:\\osw.txt"));
// 读数据 方式一 一次读取一个字符
// int ch = 0;
// while((ch = isr.read())!=-1){
// System.out.print((char)ch);
// }
// 读数据 方式二 一次读取一个字符数组
char[] chs = new char[1024];
int len = 0;
while((len=isr.read(chs))!=-1){
System.out.print(new String(chs,0,len));
}
// 关闭资源
isr.close();
}
}
高效字符流:
BufferedWriter 字符缓冲输出流
BufferedReader 字符缓冲输入流
用法与字节缓冲流类似。
字符缓冲流的特殊功能:插入新行(newLine)和读取一行(readLine)