java

四大接口

collection:存储不唯一的无序的数据
list:存储有序的不唯一的数据
set:存储无序的唯一的数据
map:以键值对的形式存储数据(以键取值,键不能重复,值可以重复)

ArrayList
实现了一个长度可变的数组,在内存空间中开辟一串连续的空间,与数组相比ArrayList长度可以随意修改
这种存储结构,循环遍历时的速度比较快

//创建集合对象
  List list=new ArrayList();
  //添加
  list.add("复合肥");
  list.add("打开文件");
  list.add("一人外有人");
  //遍历
  for (int i = 0; i < list.size(); i++) {
   System.out.println(list.get(i).toString());
  }
  for (Object o : list) {
   System.out.println(o.toString());
  }

LinkedList
使用链表结构存储数据,在新增,删除,修改时速度非常快

  //链表集合
  LinkedList lianlist=new LinkedList();
  //添加
  lianlist.add("哈哈");
  lianlist.add("呵呵");
  lianlist.add("哦哦");
  lianlist.addFirst("123");
  lianlist.addLast("456");
  //删除
  System.out.println(lianlist.removeFirst());
  System.out.println(lianlist.removeLast());

File类

一、相对路径和绝对路径
1.相对路径:从项目出发一级一级查找文件
2.绝对路径:从磁盘路径出发查找文件
二.四种基类(抽象类)流的特点
1.InputStream:字节输入流

//创建文件输入流对象
  FileInputStream fis=null;
  try {
   fis=new FileInputStream("D:\\yq.txt");
   //available:获取文件可以读取的字节数量
   System.out.println(fis.available());
   //借助循环实现读数据的过程
   int zm=-1;
   while ((zm=fis.read())!=-1) {
    System.out.print((char)zm);
   }
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }finally{
   //关闭输入流
   try {
    fis.close();
   } catch (IOException e) {
    e.printStackTrace();
   }
  }

2.OutputStream:字节输出流

//创建文件输出流对象
  FileOutputStream fos=null;
  try {
   fos=new FileOutputStream("D:\\456.txt",true);
   //创建数据
   String str="电视卡了道路喀什基地啊家开的撒旦";
   //将字符串转变成字符数组
   byte[] b=str.getBytes();
   //调用方法
   fos.write(b,0,8);
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }finally{
   try {
    //关闭fos
    fos.close();
   } catch (IOException e) {
    e.printStackTrace();
   }
  }

3.Writer:字符输出流

//创建对象
  FileWriter fw=null;
  BufferedWriter bw=null;
  try {
   fw = new FileWriter("D:\\iu.txt");
   bw=new BufferedWriter(fw);
   String str="都可能垃圾说的就是";
   //调用方法输出数据
   bw.write(str);
   String str1="打开傻大姐撒可见度开始了大家";
   //换行
   bw.newLine();
   bw.write(str1);
   //刷新缓冲区
   bw.flush();
  } catch (Exception e) {
   e.printStackTrace();
  }finally{
   //关闭对象
   try {
    bw.close();
    fw.close();
   } catch (IOException e) {
    e.printStackTrace();
   }
  }

4.Reader:字符输入流

//创建fr对象:给br提供路径
  FileReader fr=null;
  //创建br对象:读取数据
  BufferedReader br=null;
  try {
   fr=new FileReader("yq.txt");
   br=new BufferedReader(fr);
   //读取一行数据
   String str=br.readLine();
   //借助循环读取完整的数据
   while (str!=null) {
    System.out.println(str);
    str=br.readLine();
   }
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }finally{
   //关闭对象
   try {
    br.close();
    fr.close();
   } catch (IOException e) {
    e.printStackTrace();
   }
  }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值