四种抽象类
字节输入流:InputStream 字节输出流:OutputStream
字符输入流:Reader 字符输出流:Writer
文件创建基本操作
File file = new File("d:/b.txt");
.createNewFile() 返回布尔类型,判断文件是否创建成功
.exists() 判断文件是否存在
.isDirectory() 判断文件是否是目录
.isFile() 判断是否是文件类型
.length() 计算文件大小
.getName() 获得文件名
.delete() 删除文件,返回布尔类型是否删除成功
File dir = new File("d:/电影/华语/大陆");
dir.mkdirs() 创建多级目录,返回布尔类型
如果只创建单级目录,则使用 mkdir()
InputStream通用开发模式
实例化InputStream对象
利用read方法循环读取字节数据,并进行处理
调用close方法关闭InputStream对象
OutputStream通用开发模式
实例化OutputStream对象
利用write方法循环写入的字节数据
调用close方法关闭OutputStream对象
应用字节输出流实现文件复制案例(实现文本或者jpg等文件的复制)
public class FileCopySample { public static void main(String[] args) { File source = new File("d:/demo.jpg"); File target = new File("d:/demo1.jpg"); InputStream fis = null; OutputStream fos = null; try { //实例化InputStream对象 fis = new FileInputStream(source); //实例化Outputstream对象 fos = new FileOutputStream(target); byte[] bs = new byte[1024]; int len; //利用read方法循环读取的字节数据,并进行处理 while((len = fis.read(bs)) != -1){ System.out.println(len); //将读取到到字节数组写入到输出流,0代表起始点,len代表写入的长度 fos.write(bs,0,len); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { //通过finally块确保fis/fos对象执行close方法 if(fos != null){ try { fos.close(); } catch (IOException e) { e.printStackTrace(); } } if(fis != null){ try { fis.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
应用字符输入输出流实现文本读取与写入
Reader是所有字符输入流的抽象父类
Writer是所有字符输出流的抽象父类
FileReader与FileWriter分别对应了文本文件的读取与写入
转换流的应用
输入/输出流体系中还提供了两个转换流,这两个转换流用于实现将字节流转换为字符流
InputStreamReader将字节输入流转换成字符输入流
OutputStreamWriter将字符输出流转换成字节输出流
缓冲池的作用
默认文件的读取写入都是逐个字节/字符完成的,但是这种方式并不高效,因此便有了缓冲区
BufferedInputStream与BufferedOutputStream用于缓冲区字节输入、输出流
BufferedReader和BufferedWriter用于缓冲字符输入、输出流
public class TextFileSample { /*FileReader读取文本文件案例*/ public void readTextFile(){ Reader reader = null; try{ File file = new File("d:/test.txt"); //实例化Reader对象 reader = new FileReader(file); int ch = 0; //逐个字符读取 while((ch = reader.read()) != -1){ System.out.print((char)ch);//UTF-8编码集 } }catch (IOException e){ e.printStackTrace(); }finally { if(reader != null){ try { //关闭reader对象 reader.close(); } catch (IOException e) { e.printStackTrace(); } } } } /*Writer写入文本文件过程*/ public void writeTextFile(){ Writer writer = null; try { File file = new File("d:/test.txt"); //创建文件 if (!file.exists()) { file.createNewFile(); } //实例化writer对象 writer = new FileWriter(file); writer.write("这是一个新文件New"); writer.append(":Append内容");//此处使用.write()也是表示在文本文件内容末尾追加内容,不过一般使用append方法,且append方法与write方法最大的区别就是write方法括号内不允许为null,append可为null }catch (IOException e){ e.printStackTrace(); }finally { //关闭writer对象 if(writer != null){ try { writer.close(); } catch (IOException e) { e.printStackTrace(); } } } } public void isrSample(){ FileInputStream fis = null; InputStreamReader isr = null; try{ File file = new File("d:/test.txt"); fis = new FileInputStream(file); isr = new InputStreamReader(fis,"UTF-8"); StringBuffer buffer = new StringBuffer(); while(isr.ready()){ buffer.append((char)isr.read()); } System.out.println(buffer.toString()); }catch (IOException e){ e.printStackTrace(); }finally { if(isr != null){ try { isr.close(); } catch (IOException e) { e.printStackTrace(); } } if(fis != null){ try { fis.close(); } catch (IOException e) { e.printStackTrace(); } } } } //利用OutputStreamWriter写入文本文件 public void oswSample() { FileOutputStream fos = null; OutputStreamWriter osw = null; try { File file = new File("D:/test.txt"); //创建文件 if (!file.exists()) { file.createNewFile(); } fos = new FileOutputStream(file); osw = new OutputStreamWriter(fos, "UTF-8"); osw.write("这是一个新文件!"); } catch (IOException e) { e.printStackTrace(); } finally { try { if (osw != null) { osw.close(); } if (fos != null) { fos.close(); } } catch (IOException e) { e.printStackTrace(); } } } public void readBuffer(){ Reader reader =null; BufferedReader br = null; try{ File file = new File("d:/FileSample.java"); reader = new FileReader(file); br = new BufferedReader(reader); String line = null; while((line = br.readLine()) != null){ System.out.println(line); } }catch (IOException e){ e.printStackTrace(); }finally { try { if (br != null) { br.close(); } if(reader!= null){ reader.close(); } }catch (IOException e){ e.printStackTrace(); } } } public static void main(String[] args) { TextFileSample sample = new TextFileSample(); //sample.writeTextFile(); //sample.readTextFile(); //sample.isrSample(); //sample.oswSample(); sample.readBuffer(); } }
通过建立网络连接读写文件
import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; public class URLConnectionSample { public static void main(String[] args) { InputStream is = null; OutputStream os = null; try { URL url = new URL("https://manongbiji.oss-cn-beijing.aliyuncs.com/images/weixin.jpg"); URLConnection connection = url.openConnection(); is = connection.getInputStream(); os = new FileOutputStream("d:/weixin.jpg"); byte[] bs = new byte[1024]; int len = 0; while((len = is.read(bs)) != -1){ //System.out.println(len); os.write(bs,0,len); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if(os != null){ os.close(); } if (is != null) { is.close(); } }catch (IOException e){ e.printStackTrace(); } } } }