----IO------
1. 分类
--------------------
--------------------
..
2. 举例
读文件1:------------------------------------------------------------》try {
FileInputStream inputStream = new FileInputStream("test.txt");
int by=-1;
while((by=inputStream.read())!=-1){
System.out.print((char)by);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
读文件2(缓存):--------------------------------------------------------》FileInputStream inputStream = new FileInputStream("test.txt");
FileOutputStream outputStream = new FileOutputStream("copy.txt");
int by=-1;
while((by=inputStream.read())!=-1){
outputStream.write(by);
}
inputStream.close();
outputStream.close();
读文件3(缓存):----------------------------------------------------》FileInputStream inputStream = new FileInputStream("test.txt");
byte[]buffer=new byte[1024];
FileOutputStream outputStream = new FileOutputStream("copy1.txt");
int by=-1;
while((by=inputStream.read(buffer))!=-1){
outputStream.write(buffer,0,by);
}
读文件4(缓存):-------------------------------------------------》BufferedOutputStream fox = new BufferedOutputStream(new FileOutputStream("fox.txt"));
fox.write("1234567".getBytes());
fox.close();
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("fox.txt"));
int by=-1;
while((by=bis.read())!=-1){
System.out.print((char)by);
}
写文件:----------------------------------------------------------------》String content = "hello,IO";
byte[] buffer = content.getBytes();
FileOutputStream outputStream = null;
try {
outputStream = new FileOutputStream("hello.txt",true);
for (int i = 0; i < 10; i++) {
outputStream.write(("hello,IO" + i).getBytes());
outputStream.write("\r\n".getBytes());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}