2020.02-Study_update.3

week 2.17-2.23

-Study_update
MonVector,LinkedList,泛型,HashSet,TreeSet,HashMap,TreeMap,HashTable,定义集合类并使用泛型
Tue异常处理exception,try-catch,可变参数,JUnit,转义字符
-WesIo文件操作,绝对路径,相对路径,FileInputStream
-Thus输入流的关闭,FileOutputStream,BufferedOutputStream(包装流)
FriBufferedInputStream,编码表,字符流,InputStreamReader,OutputStreamWriter,FileWriter,FileReader
SatFileWriterAndFileReader-Copy.BufferedWriter,BufferedReader,思考双链表
Sun构造myList,,看书

2.17 Monday

ArrayList 线程不安全,但是效率较高。
Vector 线程安全,但是效率较ArrayList低。
LInkedList 多了两个方法,addFirst和addLast,可以在首尾添加元素,可以方便的插入与删除数据。
Hashset TreeSet 添加的元素自动去掉重复,并且是无序的。
HashMap 保存的是键值对.key不能重复,值可以.KeySet方法返回key的集合.
TreeMap 大致与HashMap相同,哈希根据哈希值查找,Tree较有序查找.
HashTable 与HashMap大致相同,但是线程安全。
各种集合的差异

Collection(存储单个值的)
 List可重复,有序.
  ArrayList(使用数组来存储)
  LinkedList(使用引用来存储)(很方便的数据插入与删除)
  Vector(与ArrayList一样,Vector线程安全,性能较低)
 Set不可重复,无序.
  HashSet
  TreeSet 
  
Map(用来存储键值对)
 HashMap
 TreeMap
 HashTable

定义集合类并使用泛型

public class MyArrayList<T> {
 private T[] dataArray =(T[])new Object[100];
 private int index=0;
 
 public void add(T data) {
  dataArray[index++]=data;
 }
 public T get(int i) {
  if(i>=0&&i<=this.index-1) {
   return dataArray[i];
  }else {
   System.out.println("没有数据");
   return (T)null;
  }
 }
}

2.18 Tuesday

异常捕捉,每一个异常都有相应得异常类。所有的语句都继承自exception.使用try catch语句。不会影响语句以下的运行。

try{
可能出错的语句,如果有多条语句,当语句①出错,语句②也不会执行了。
语句①
语句②
}catch(错误类型 名字){
当try语句出现括号内的错误时,执行该语句块。
}catch(错误类型 名字){
当try语句出现括号内的错误时,执行该语句块。
}finally{
恒要执行。
}

可变参数

当参数个数不确定时,
public static int add(int …args){
定义了一个可变参数,当使用时,args当作数组使用。
}
或者传递一个数组参数

public class Kebiancanshu {
 public static void main(String[] args) {
  System.out.println(add(new int[] {1,2,3,4,5}));
 }
// public static int add(int...args) {
//  int res=0;
//  for(int i=0;i<args.length;i++) {
//   res+=args[i];
//  }
//  return res;
// }
 public static int add(int[] args) {
  int res=0;
  for(int i=0;i<args.length;i++) {
   res+=args[i];
 }
  return res;
 }
}

JUnit

导入JUnit jar,在需要单独运行的类前加@Test,Run as JUnit.

2.19 Wednesday

File类 可以构造对象指向文件夹或者文件。
reNameTo方法可以改名与移动文件。
createNewfFile(p)当p指向的路径不存在时,创建文件。
绝对路径 带盘符的,用getAbsolutePath()获取。
相对路径 以程序运行的地方做绝对坐标 getPath方法

练习
用递归遍历文件夹所有文件

package com.yangcong.io;
import java.io.File;
public class showAllPath {
 public void method(File sourcePath) {//传递一个File类型的参数,遍历这个文件夹下所有文件.
  File[] lists=sourcePath.listFiles();//File类型的数组,接收这个文件夹次级文件。
  for(File list:lists) {//遍历数组中所有文件
   if(list.isDirectory()) {//如果是文件夹,继续调用此方法
    method(list);
   }else {//如果是文件,输出名字。
    System.out.println(list.getName());
   }
  }
 }
}

2.20 Thursday

流使用后得关闭
取得单个字节

public class Demo01_FileInputOutputStream {
 public static void main(String[] args) {
  
 }
 public void testFileInputStream() {
  FileInputStream input=null;//声明一个流
  try {
   input=new FileInputStream("Test\\Demo01.txt");//指向一个文件
   int a=input.read();//获取第一个字符
   
   System.out.println((char)a);
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }finally {
   try {
    if(input!=null)//检测输入流是否为空,不为空则关闭。
    input.close();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
 }
}

取得多个字节

public class Demo01_FileInputOutputStream {
 public static void main(String[] args) {
 }
 @Test
 public void FileInputStreamDemo(){  
  FileInputStream input=null;
  try {
   input=new FileInputStream("Test\\Demo01.txt");
   byte[] data=new byte[4];//声明一个字节数组大小为4
   int length;//用来存储读取了多少个字符,
   while((length=input.read(data))>-1) {//检测是否读取到下一个数据,read(data)返回得是读取到的个数,如果没有读取到数据,返回-1循环终止
     for(int i=0;i<length;i++) {
      System.out.print((char)data[i]);
     }
   }
  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }finally {
   if(input!=null) {
    try {
     input.close();//关闭输入流
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   }
   
  }
 }
}

/表示路径级。
FileOutputStream构造对象时,参数append决定是追加还是重写,

public class Demo02_FileOutputStream {
 @Test
 public void testFileOutputStream() {
  FileOutputStream output=null;
  try {
   output=new FileOutputStream("Test/Demo02.txt",true);//如果文件不存在,会自动创建,后一个参数表示是否是追加模式
//   单个字符写入入   
   byte a='H';
   output.write(a);
//   写入字符串
   String str=new String("你好,Hello world");
   output.write(str.getBytes());
//   写入范围字符
   output.write(str.getBytes(),1,1);//写入从第1个Index至往后一位的数据
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }finally {
   try {
    if(output!=null)
    output.close();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
 }
}

练习,复制功能的类

public class CopyDemo {
 
 @Test
 public void copy(String inputName,String outputName) {//被复制文件,复制到的文件
  FileInputStream input=null;
  FileOutputStream output=null;
  try {
   input=new FileInputStream(inputName);
   output=new FileOutputStream(outputName);
   int data=-1;
   while((data=input.read())>-1){
    output.write(data);
   }
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }finally {
   try {
    if(input!=null)
    input.close();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   try {
    if(output!=null)
    output.close();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  } 
 }
}

BufferedOutputStream =FileOutputStream+缓冲区(数组),关闭只需关闭Buffered,因为包装流管理了输出流,会将FileOutputStream的输出放入数组,等到一定的字节数量时,才统一进行写入,减少与硬盘的交互,关闭流时也会进行写入。flush方法立即写入。
缓冲区何时写入硬盘
①缓冲区已满了
②调用flush
③调用close

2.21 Friday

BufferedInputStream 输入缓冲区
每次执行read命令 都是从硬盘取出缓冲区大小的数据量。可以减少跟硬盘的交互次数。
使用BufferedIO复制文件.

public class BufferedCopy {
 @Test
 public void copy() {
  FileInputStream fileInput;
  FileOutputStream fileOutput;
  BufferedInputStream input=null;
  BufferedOutputStream output=null;
  try {
   fileInput = new FileInputStream("Test/Movie01.mp4");//被复制的文件路径
   input=new BufferedInputStream(fileInput);
   fileOutput=new FileOutputStream("Test/Movie02.mp4");//复制到的文件路径及名字
   output=new BufferedOutputStream(fileOutput);
   byte[] data=new byte[1024];
   while(input.read(data)>-1) {//如果返回的大于—1证明有内容,写入到新文件中。
    output.write(data);
   }
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }finally {
   try {
    if(output!=null)//关闭输出流
    output.close();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   try {
    if(input!=null)//关闭输入流
    input.close();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
 }
}

字符流
字符流可以转换编码,只能对文本文件进行操作。

 //字符流
 //OutputStreamWriter InputStreamReader
 @Test
 public void testOutputWriter() {
  OutputStreamWriter output=null;
  try {
   FileOutputStream fileOutput=new FileOutputStream("Test/demo03.txt");
   output=new OutputStreamWriter(fileOutput);
   output.write('中');
   output.write('英');
   output.flush();
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }finally {
   try {
    if(output!=null)
    output.close();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
 }
}

InputStreamReader
可以修改编码

public class InputStreamReader_test {
 @Test
 public void inputStreamReaderTest() {
  InputStreamReader input=null;
  try {
   FileInputStream fileInput=new FileInputStream("Test/demo01.txt");
   input=new InputStreamReader(fileInput);
   char[] data=new char[1024];
   int length=-1;
//   while((length=input.read(data))>-1) {
//    System.out.println(data);
//   }
   length=input.read(data);
   System.out.println(new String(data,0,length));
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }finally {
   try {
    if(input!=null)
    input.close();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
 }
}

OutputStreamWriter
可以修改编码

public class OutputStreamWrite {
 //字符流
 //OutputStreamWriter InputStreamReader
 @Test
 public void testOutputWriter() {
  OutputStreamWriter output=null;
  try {
   FileOutputStream fileOutput=new FileOutputStream("Test/demo03.txt");
   output=new OutputStreamWriter(fileOutput);
   output.write('中');
   output.write('英');
   output.flush();
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }finally {
   try {
    if(output!=null)
    output.close();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
 }
}

FileWriter
不可以修改编码

 public void fileWriterdemo() {
  FileWriter output=null;
  try {
   output=new FileWriter("Test/demo01.txt");
   output.write("Hello world!");
   output.flush();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }finally {
   try {
    if(output!=null)
    output.close();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
 }

FileReader
不可以修改编码

 public void fileReader() {
  FileReader input=null;
  try {
   input=new FileReader("Test/demo01.txt");
   char[] data=new char[1024];
   int length;
   length=input.read(data);
   System.out.println(new String(data,0,length));
  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }finally {
   try {
    if(input!=null)
    input.close();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
 }
} 

FileWriterAndFileReader-Copy.
单个字符

 public void copyBySingleChar(String sourceName,String targetName) {
  FileReader input=null;
  FileWriter output=null;
  try {
   input=new FileReader(sourceName);
   output=new FileWriter(targetName); 
   int data=-1;
   while((data=input.read())>-1) {
    output.write(data);
   }
  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }finally {
   try {
    if(input!=null)
    input.close();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   try {
    if(output!=null)
    output.close();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
 }

字符数组

 public void copyByArrayChar(String sourceName,String targetName) {
  FileReader input=null;
  FileWriter output=null;
  try {
   input=new FileReader(sourceName);
   output=new FileWriter(targetName);
   char[] data=new char[1024];
   int length=-1;
   while((length=input.read(data))>-1) {
    output.write(new String(data,0,length));
   }
  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }finally {
   try {
    if(input!=null)
    input.close();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   try {
    if(output!=null)
    output.close();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
 }
}

双链表的后端插入极丑版思维图
在这里插入图片描述

 public void lastadd(T value) {
  Node newNode=new Node(value);
  if(head==null) {//如果链表为空
   head=newNode;//头指向新Node
   tail=newNode;//尾指向新Node
  }else {
   tail.next=newNode;//尾指向新Node
   newNode.pre=tail;//新Node指向尾
  }
  tail=newNode;
 }

2.22 Sunday

public class Test {
 public static void main(String[] args) {
  //substring 截取字符串
//  String greeting="Hello";
//  String s=greeting.substring(0,3);
//  System.out.println(s);
  //分隔符
//  String str=String.join("/", "a","b");
//  System.out.println(str);
  //equals判断是否相等,方法equalsIgnoreCase忽略大小写判断是否相等
//  String s1="abc";
//  String s2="abc";
//  System.out.println(s1.contentEquals(s2));
//  String s3="abc";
//  String s4="Abc";
//  System.out.println(s1.equalsIgnoreCase(s4))
  //Scanner输入
//  Scanner s=new Scanner(System.in);
//  System.out.println("How old are you ?");
//  String age=s.nextLine();
//  System.out.println("I am "+age+" years old.");
  //数组的拷贝
  int[] nums1=new int[5];
  int[] nums2=nums1;
  System.out.println(nums1.equals(nums2));
  int[] nums3=Arrays.copyOf(nums1, nums1.length);
  System.out.println(nums3.equals(nums1)); 
 }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值