io流
inputStream 字节流,读取数据
outputStream 字节流 写入数据
reader 字符流,读取数据
writer 字符流,写入数据
其他多种多样变化的流均是继承了他们,并进行豪华装饰派生出来的加强版
字节流的读取和写入
//FileInputStream和 FileOutputStream字节流的读取和写入
FileInputStream fis=null;
FileOutputStream fos=null;
try {
fis =new FileInputStream(“C:\Users\18527\OneDrive\图片\屏幕快照\2021-03-19.png”);
fos=new FileOutputStream(“D:\ZZY.jpg”);
byte[] b=new byte[1024];
int length=0;
try {
while ((length=fis.read(b))!=-1) {
fos.write(b, 0, length);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
fis.close();
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
缓冲流
// BufferedInputStream和 BufferedOutputStream缓冲流字符流的读取和写入
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(“d:\ZZY.jpg”);
fos = new FileOutputStream(“D:\zzbY.jpg”);
BufferedInputStream bis = new BufferedInputStream(fis);
BufferedOutputStream bos = new BufferedOutputStream(fos);
byte[] b = new byte[1024];
int length = 0;
try {
while ((length = bis.read(b)) != -1) {
bos.write(b, 0, length);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
fis.close();
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
字符流的读取和写入
BufferedReader br=null;
BufferedWriter bw=null;
try {
br=new BufferedReader(new FileReader("d:\\ZZY.jpg"));
try {
bw=new BufferedWriter(new FileWriter("d:\\qwe.txt"));
String temp="";
while ((temp=(br.readLine()))!=null) {
bw.write(temp);
bw.newLine();
}
bw.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//字节流转换字符流
BufferedReader br=null;
BufferedWriter bw=null;
br=new BufferedReader(new InputStreamReader(System.in));
try {
bw=new BufferedWriter(new FileWriter(“d:\zzy.txt”));
String temp="";
System.out.println(“请开始你的表演”);
while ((temp=br.readLine())!=null) {
if (temp.equals(“zzy”)) {
break;
}
bw.write(temp);
bw.newLine();
}
bw.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}