流:在java数据中所有数据都是使用流读写的。流是一种有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象。即数据在两个设备间的传输成为流,流的本质是数据传输。
流分为:输出流,输入流。
2.字节流,字符流字节流:数据流中最小的数据单元是字节。 字符流:数据中最小的数据单元是字符,一个字符占用两个字节。
3.字节流
public class ByteStream {
public static void main(String[] args) {
//输入
FileInputStream fis=null;
//输出
FileOutputStream fos=null;
try {
fis=new FileInputStream("D:\abc.txt");
fos=new FileOutputStream("D:\ab.txt");
byte[] b=new byte[1024];
int c=0;
while ((c=fis.read(b))!=-1){
fos.write(b,0,c);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
fis.close();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
4.字符流
public class CharacterStream {
public static void main(String[] args) {
//输入
FileReader fir=null;
//输出
FileWriter fil=null;
BufferedReader br=null;
BufferedWriter bw=null;
try {
fir=new FileReader(new File("D:\abc.txt"));
fil=new FileWriter(new File("D:\ac.txt"));
br=new BufferedReader(fir);
bw=new BufferedWriter(fil);
String temp=null;
while ((temp=br.readLine())!=null){
bw.write(temp);
//换行
bw.newline();
}
//刷新缓冲区
bw.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
fir.close();
fil.close();
br.close();
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}



