IO体系
抽象基类 | 节点流(文件流) | 缓存流 |
InputStream | FileInputStream | BufferedInputStream |
OutputStream | FileOutputStream | BufferedOutputStream |
Reader | FileReader | BufferedReader |
Writer | FileWriter | BufferedWriter |
在IO里有一个很重要的类:File类(涉及创建 删除 重命名等 对文件内容操作要用到流)
FileInputStream
public static void main(String[] args) throws IOException{
//创建file对象
File file=new File("hello.txt");
FileInputStream fis=new FileInputStream(file);
//对file文件读取
//int b=fis.read();//读取文件中的一个字节 读取到末尾是返回-1
byte[] b=new byte[5];
int len;//每次读入到byte中的字节长度
while((len=fis.read(b))!=-1){
System.out.println(new String(b,0,len));
}
}
FileOutStream
public static void main(String[] args) throws IOException{
File file=new File("hello.txt");
FileOutputStream fos=null;
try{
fos=new FileOutputStream(file);
fos.write(new String("I Love").getBytes());
}catch(Exception e){
e.printStackTrace();
}finally{
if(fos!=null){
fos.close();
}
}
}
读写一体:这个方法复制文本效率很低 但是可以复制非文本字节流 如视频文件 音频 图片等
public static void main(String[] args) throws IOException{
File file1=new File("hello1.txt");
File file2=new File("hello2.txt");
FileInputStream fis=null;
FileOutputStream fos=null;
try{
fis=new FileInputStream(file1);
fos=new FileOutputStream(file2);
byte[]b=new byte[1024];
int len;
while((len=fis.read(b))!=-1){
fos.write(b,0,len);
}
}catch(Exception e){
e.printStackTrace();
}finally{
fis.close();
fos.close();
}
FileReader 和FileWriter 下面的不能实现非文本的复制
public static void main(String[] args) throws IOException{
FileReader fr=null;
FileWriter fw=null;
try{
File src=new File("hello1.txt");
File des=new File("hello2.txt");
fr=new FileReader(src);
fw=new FileWriter(des);
char[] c=new char[1024];//注意是char不是byte
int len;
while((len=fr.read(c))!=-1){
fw.write(c, 0, len);
}
}catch(Exception e){
throw new RuntimeException("失败");
}finally{
fw.close();
fr.close();
}
}
缓冲流
BufferedInputStream 和BufferedOutPutStream 实现非文本复制
BufferReader 比FileReader 多了一个readerline() 读取一行
public static void main(String[] args) throws IOException{
BufferedInputStream bis=null;
BufferedOutputStream bos=null;
try{
File src=new File("1.jpg");
File des=new File("2.jpg");
//创建节点流
FileInputStream fis=new FileInputStream(src);
FileOutputStream fos=new FileOutputStream(des);
//将节点流作为参数传给缓存流
bis=new BufferedInputStream(fis);
bos=new BufferedOutputStream(fos);
byte[] c=new byte[1024];
int len;
while((len=bis.read(c))!=-1){
bos.write(c, 0, len);
bos.flush();//刷新
//这两步可以用PrintWriter中的方法代替掉
}
}catch(Exception e){
throw new RuntimeException("失败");
}finally{
bos.close();
bis.close();
}
}
标准输入输出流 System.in System.out 从键盘输入输出
如:InputStream is=System.in;
转化流(字节流和字符流转化)
解码:字节数组-->字符串
编码: 字符串-->字节数组
public static void main(String[] args) throws IOException{
InputStream reader=null;
OutputStream wirter=null;
try{
//解码
File src=new File("1.txt");
FileInputStream fis=new FileInputStream(src);
InputStreamReader isr=new InputStreamReader(fis,"GBK");//编码
BufferedReader br=new BufferedReader(isr);
//编码
File des=new File("2.txt");
FileOutputStream fos=new FileOutputStream(src);
OutputStreamWriter osr=new OutputStreamWriter(fos,"GBK");//编码
BufferedWriter bw=new BufferedWriter(osr);
String str;
while((str=br.readLine())!=null){
bw.write(str);
bw.newLine();
bw.flush();
}
}catch(Exception e){
throw new RuntimeException("失败");
}finally{
reader.close();
wirter.close();
}
}
File类的一些API
String getName();//获取文件名
File getAbsoluteFile() //turns the absolute form of this abstract pathname.
boolean mkdir()Creates the directory named by this abstract pathname.
boolean mkdirs()//Creates the directory named by this abstract pathname, including any necessary but nonexistent parent directories.
String[] list() //Returns an array of strings naming the files and directories in the directory denoted by this abstract pathname.
File[] listFiles()//Returns an array of abstract pathnames denoting the files in the directory denoted by this abstract pathname.
boolean renameTo(File dest)//Renames the file denoted by this abstract pathname.
boolean delete()//Deletes the file or directory denoted by this abstract pathname...............................