Java输入输出流
一、Java输入输出流
文件输入 (Input)—— 读
文件输出 (Output)—— 写


二、File类的应用
文件可以认为是相关记录或放在一起的数据的集合
构造方法:
File(String pathname):根据一个路径得到File对象
File(String parent, String child):根据一个目录和一个子文件/目录得到File对象
File(File parent, String child):根据一个父File对象和一个子文件/目录得到File对象
创建功能:
public boolean createNewFile():创建文件
public boolean mkdir():创建目录
public boolean mkdirs():创建多级目录
重命名和删除功能:
public boolean renameTo(File dest):把文件重命名为指定的文件路径
public boolean delete():删除文件或者文件夹
Java中的删除不走回收站,要删除一个文件夹,请注意该文件夹内不能包含文件或者文件夹
判断功能:
public boolean isDirectory():判断是否是目录
public boolean isFile():判断是否是文件
public boolean exists():判断是否存在
public boolean canRead():判断是否可读
public boolean canWrite():判断是否可写
public boolean isHidden():判断是否隐藏
获取功能:
public String getAbsolutePath():获取绝对路径
public String getPath():获取路径
public String getName():获取名称
public long length():获取长度。字节数
public long lastModified():获取最后一次的修改时间,毫秒值
public String[ ] list()
public String[ ] list(FilenameFilter filter):获取指定目录下的所有文件或者文件夹的名称数组
public File[ ] listFiles()
public File[ ] listFiles(FileFilter filter):获取指定目录下的所有文件或者文件夹的File数组
绝对路径:从盘符开始的路径
相对路径:从当前路径开始的路径
三、字节流


四、字符流
字符输入流Reader
字符输出流Writer


五、对象的系列化与反序列化
必须实现Serialzable接口
对象输入流ObjectInputStream
对象输出流ObjectOutputStream
案例1:遍历文件夹找到某个文件
public static void main(String[] args) throws IOException { File f=new File("D:\\"); searchFile(f); } public static void searchFile(File f) throws IOException { File[] files=f.listFiles(); if(files!=null){ for(File file : files){ if(file.isDirectory()){ searchFile(file); }else if(file.getName().equals("寻找这个文件.txt")){ char cbuf[]=new char[1024]; FileReader fr=new FileReader(file); System.out.println(new String(cbuf,0,fr.read(cbuf))); fr.close(); } } } }
案例2:复制图片
public static void main(String[] args) { try{ FileInputStream fis=new FileInputStream("D:\\JavaProject\\4.jpg"); FileOutputStream fos=new FileOutputStream("D:\\JavaProject\\4copy.jpg"); int n=0; byte[] b=new byte[1024]; long startTime=System.currentTimeMillis(); while ( (n=fis.read(b))!=-1 ){ fos.write(b,0,n); } long endTime=System.currentTimeMillis(); fis.close(); fos.close(); System.out.println("用时"+endTime-startTime+"ms"); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
案例3:对象的序列化与反序列化
public class Product implements Serializable { private int id; private String name; private String categories; private double price; public Product(int id, String name, String categories, double price) { this.id = id; this.name = name; this.categories = categories; this.price = price; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getCategories() { return categories; } public void setCategories(String categories) { this.categories = categories; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } @Override public String toString() { return "\n产品ID:"+id + "\n产品名称:"+name + "\n产品属性:"+categories + "\n产品价格:"+price+"元"; } public void write(){ try { FileOutputStream fos = new FileOutputStream("D:\\JavaProject\\goods.txt"); ObjectOutputStream oos=new ObjectOutputStream(fos); oos.writeObject(this); oos.flush(); oos.close(); fos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); }catch (IOException e){ e.printStackTrace(); } } public void read(){ try{ FileInputStream fis=new FileInputStream("D:\\JavaProject\\goods.txt"); ObjectInputStream ois=new ObjectInputStream(fis); Product product=(Product) ois.readObject(); System.out.println(product); fis.close(); ois.close(); }catch (FileNotFoundException e){ e.printStackTrace(); }catch (IOException e){ e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } }
public class ProduTest { public static void main(String[] args) { Product iphone=new Product(123,"iphone","telephone",4888); Product ipad=new Product(234,"ipad","computer",5088); Product macbook=new Product(345,"macbook","computer",10688); Product iwatch=new Product(256,"iwatch","watch",4799); iphone.write(); iphone.read(); ipad.write(); ipad.read(); macbook.write(); macbook.read(); iwatch.write(); iwatch.read(); } }

浙公网安备 33010602011771号