//流
//了解:
//传输协议
//QQ
[IP]
UDP(用户数据包协议)/[TCP]
SMTP/FTP/HTTP[S]
//流:不同进程间数据传输的机制
//本质:(硬件)二进制/(Java)字节数组
//分类:
方向:输出/输入
内容:字符/字节
功能:节点流(直接面向数据源)/处理流
//文件:File(文件/目录)
File file =new File(String path);
//相对路径:根目录为当前工程根目录
//绝对路径:从分区盘符开始到目标的完整路径
//路径的表示方式:c:\dir\subdir\… c:/dir/subdir/…
//共性操作
boolean exists = file.exists();
String relativePath = file.getPath();
String absolutePath = file.getAbsolutePath();
String name = file.getName();
Boolean success = file.delete();//立即删除
file.deleteOnExit();//虚拟机关闭时,文件或者目录才会删除
//文件操作
boolean isFile = file.isFile();
long size = file.length();
long lastModifiedTime = file.lastModified();
boolean success = file.createNewFile();
//目录操作
boolean isDir = file.isDireatory();
Boolean success = file.mkdir();//创建一个目录
Boolean success = file.mkdirs();//创建多个目录
file.deleteOnExit();
FileFilter filter = new FileFilter{
@Override
public boolean accept(File file){
return file.getName().endsWith(".zip");
}
};
File[] files = file.listFiles([FileFilter fileter]);
案例:
//字符流
//字符节点流读写文件
//写文件
String path = ...; File dir = new File(String path);
FileWriter fw = =new FileWriter(String/File path);
fw.write(String content);
fw.close();
案例:
//读文件
FileReader fr = new FileReader(String/File path);
char[] chars = new chars[int length];
int len = fr.read(chars);
fr.close();
案例:
//写
File file = new File("myfile/aa.txt");
FileWriter writer = null;
try{
write = new FileWriter(file,true);
writer.write("天安门上太阳照...");
}catch(IOException e){
e.printStackTrace();
}finally{
}catch(IOException e){
e.printStackTrace();
}finally{
//释放资源
try{
writer.close();
}catch(IOException e){
e.printStackTrace();
}
}
//读
FileReader fr= null;
try{
fr = new FileReader(file);
char[] chars = new char[4];
int len = -1;
while((len=fr.read(chars))!=-1){
System.out.println(new String(chars,0,len));
}
}catch(FileNotFoundException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}finally{
//释放资源
try{
fr.close();
}catch(IOException e){
e.printStackTrace();
}
}
//线程
//线程:CPU调度的最小单位
//一个进程中至少包含一条线程:主线程
//主线程是系统创建的
//java中主线程的入口为public static void main(String[] args){}
//线程的状态
//1、新生 new born
↓
//2、就绪 ready ←
↓ ↑
//3、运行 running → 阻塞 blocked
↓
//4、死亡 dead
//关于线程的阻塞
Thread.sleep(int milliseconds);
t.wait(); //一直等,直到(锁在同一把锁)有人通知
t.wait(long milliseconds); //等milliseconds,有人通知立即执行,无人通知等超过millises后自动恢复执行
t.notify(); //通知另一个人
t.notifyAll();//通知其他人
//java中创建线程的方式
//1、创建java.lang.Thread的子类:重写Thread的run方法
class MyThread1 extends Thread{
@Override
public void run(){
setName("sub1");
try{
for (int i=0;i<100;i++){
synchronized (lock){
if(count%2!=0){
lock.wait();
}
System.out.println(this.getName() +" : "+ i);
count++;
lock.notify();
Thread.sleep(1000);
}
}
}catch (InterruptedException e){
e.printStackTrace();
}
}
}
MyThread1 mt = new MyThread1();
mt.start();
//2、匿名内部类创建Thread的对象: 重写Thread的run方法
Thread t = new Thread() {
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println("Sub :" + i);
try {
Thread.sleep(1000);
} catch(InterruptedException e){
e.printStackTrace();
}
}
}
};
t.start();
//3、以匿名内部类,创建Runnable接口对象,作为java.lang.Thread的构造参数传入
Runnable run1 = new Runnable() {
@Override
public void run(){
try{
for (int i=0;i<100;i++){
synchronized (lock){
if(count%2!=0){
lock.wait();
}
System.out.println(Thread.currentThread().getName() +" : "+ i);
count++;
lock.notify();
Thread.sleep(1000);
}
}
}catch (InterruptedException e){
e.printStackTrace();
}
}
};
Thread1 t = new Thread(run1);
t.start();
//线程池
//固定线程池Executors.newFixedThreadPool(int size); 池中创建固定数量的线程,在有闲置线程的情况下,可随时申请使用,无闲置线程,则等待;
ExecutorService fixedPool = Executors.newFixedThreadPool(int size);
//缓存线程池 无须维护,初始化不创建任何线程,根据需要去无限制创建线程,当线程闲置到一定的时间阈值时,线程会被回收
ExecutorService cachedPool = Executors.newCachedThreadPool();
//单线程池 只维护一条线程的线程池
ExecutorService singlePool = Executors.newSingleThreadPool();
//通过向线程池submit方法中传入Runnable,Callable对象来完成指定任务,需要返回值传入Callable,不需要传入Runnable
xxxPool.submit(Runnable task);
Future<T> future = xxxPool.submit(Callable<T> call);
//计划线程池 根据需要在指定时间执行或以固定的周期或延时重复执行的线程池
ScheduledExecutorService scheduledPool = Executors.newScheduledThreadExecutor();
//延迟多久执行一次(一次性执行) 让指定任务延时固定时长后执行,让参数一指定的任务延时参数二数量的参数三单位后执行
scheduledPool.schedule(Runnable run,int delay,TimeUnit unit);
scheduledFuture<T> future=scheduledPool.schedule(Callable<T> run,int delay,TimeUnit unit);
//每隔多久执行一次 参数一指定的任务在参数二量参数四单位首次执行,并在此之后每隔参数三量参数四单位后再执行
scheduledPool.scheduleAtFixedRate(Runnable run,int initialDelay,int period,TimeUnit unit);
//执行完延迟多久执行下一次 参数一为任务,参数二量参数四单位后首次执行,在任务完成之后,每隔参数三量参数四单位后再执行
scheduledPool.scheduleWithFixedRate(Runnable run,int initialDelay,int delay,TimUnit unit);
//释放线程池
xxxPool.shutdown();