本周主要学习了以下内容
一异常:分为运行期异常和编译期的异常。
处理:
A:try...catch...finally
B:throws 抛出
演示案例
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/*
* public String getMessage():异常的消息字符串
* public String toString():返回异常的简单信息描述
* printStackTrace() 获取异常类名和异常信息,以及异常出现在程序中的位置。
* 返回值void。把信息输出在控制台。
*/
public class ThrowableDemo {
public static void main(String[] args) {
Date d=new Date();
String s="2021-07-20";
// System.out.println(d.getTime());
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// System.out.println(sdf.format(d));
try {
sdf.parse(s);
} catch (Exception e) {
//System.out.println(e.getMessage());
//System.out.println(e.toString());
e.printStackTrace();
}
}
}
二:文件与Io流
案例演示
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/*
* .字节流四种方式复制MP4并测试效率
字节流四种方式复制文件
基本字节流一次读写一个字节
基本字节流一次读写一个字节数组
高效字节流一次读写一个字节
高效字节流一次读写一个字节数组
问题:Exception in thread "main" java.io.FileNotFoundException: c: (拒绝访问。)
当读取对象是文件夹时产生
*/
public class Copymap4Demo {
public static void main(String[] args) throws IOException {
long start =System.currentTimeMillis();
FileInputStream fis=new FileInputStream("C:\\Users\\wpp\\20210301_090103.mp4");
FileOutputStream fos=new FileOutputStream("c:\\java\\网课.mp4");
//方式一:用时:794462
// int by=0;
// while((by=fis.read())!=-1){
// fos.write(by);
// }
// fis.close();
// fos.close();
//方式二:用时:1012
// int length=0;
// byte []bys=new byte[1024];
// while((length=fis.read(bys))!= -1) {
// fos.write(bys,0,lenght);
// }
// fis.close();
// fos.close();
//方式三:用时:3397
BufferedInputStream bis=new BufferedInputStream(fis);
BufferedOutputStream bos=new BufferedOutputStream(fos);
// int by=0;
// while((by=bis.read())!=-1) {
// bos.write(by);
// }
// bis.close();
// bos.close();
// fis.close();
// fos.close();
//方式四:用时:272
int length=0;
byte[] bys=new byte[1024];
while((length=bis.read(bys))!=-1) {
bos.write(bys,0,length);
}
bis.close();
bos.close();
fis.close();
fos.close();
long end=System.currentTimeMillis();
System.out.println(end-start);
}
}
//复制多层文件夹
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class CopyFolders {
public static void main(String[] args) throws IOException {
File filesrc=new File("c:\\java");
File filedest=new File("c:\\Demo");
if(!filedest.exists()) {
filedest.mkdirs();
}
File[] fs=filesrc.listFiles();
for(File f:fs) {
copy(f, filedest);
}
}
public static void copy(File filesrc,File filedest) throws IOException {
File newfile=new File(filedest,filesrc.getName());
if(filesrc.isDirectory()) {
if(!newfile.exists()) {
newfile.mkdirs();
}
File[] fs=filesrc.listFiles();
if(fs.length!=0) {
for(File f:fs) {
copy(f, newfile);
}
}
}
else {
copyFlie(filesrc,newfile);
}
}
public static void copyFlie(File file,File newfile) throws IOException{
BufferedInputStream bis=new BufferedInputStream(new FileInputStream(file));
BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(newfile));
int length=0;
byte[] bys=new byte[1024];
while((length=bis.read(bys))!=-1) {
bos.write(bys);
}
bis.close();
bos.close();
}
}
三:多线程
案例演示
//测试类
public class BuySellPlusDmeo {
public static void main(String[] args) {
Pen p=new Pen();
Thread t1=new Thread(new BuyThread(p),"店铺");
Thread t2=new Thread(new MakeThread(p),"工厂");
t1.start();
t2.start();
}
}
//卖
public class BuyThread implements Runnable {
Pen p=new Pen();
BuyThread(Pen p){
this.p=p;
}
public void run() {
while(p.x>0) {
try {
p.get();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
//做
public class MakeThread implements Runnable{
Pen p=new Pen();
MakeThread(Pen p){
this.p=p;
}
@Override
public void run() {
while(p.x>0) {
try {
p.set();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
//接口实现类
public class MyRunnable implements Runnable{
@Override
public void run() {
for(int x=0;x<100;x++) {
System.out.println(Thread.currentThread().getThreadGroup().getName()+
"--"+Thread.currentThread().getName()+"--"+x);
}
}
}
//
public class Pen {
private String name;
private int price;
private boolean flag=false;
int x=100;
public Pen() {
super();
}
public Pen(String name, int price) {
super();
this.name = name;
this.price = price;
}
public synchronized void get () throws InterruptedException {
if(!flag) {
this.wait();
}
System.out.println(name+"----"+price);
flag=false;
this.notify();
}
public synchronized void set() throws InterruptedException {
if(flag) {
this.wait();
}
if(x%2==0) {
name="万宝龙";
price=9999;
}
else {
name="凌美";
price=399;
}
x--;
flag=true;
this.notify();
}
}
四:GUI
案例演示
import java.awt.Button;
import java.awt.CardLayout;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class ButtonDemo {
public static void main(String[] args) {
Frame f=new Frame("数据转移");
f.setBounds(450,250,500,500);
//f.setLayout(new CardLayout());
//f.setLayout(new FlowLayout());
//f.setLayout(new GridBagLayout());
f.setLayout(new GridLayout());
final TextField tf=new TextField(20);
Button b=new Button("数据转移");
final TextArea ta=new TextArea(25,40);
f.add(tf);
f.add(b);
f.add(ta);
f.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
b.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String str=tf.getText().trim();
tf.setText("");
//ta.setText(str);
ta.append(str+"\n\r");
tf.requestFocus();
}
});
f.setVisible(true);
}
}