java 多线程 数据流 内部类(播放音乐)

来源:互联网 发布:各国电视台直播软件 编辑:程序博客网 时间:2024/06/03 18:21

一.多线程

1.进程与线程

进程:负责资源管理(内存)
线程:负责的程序的执行


2.类实现

a、定义一个类,继承Thread
b、重写Run方法
c、启动线程(线程对象.start() )


3.接口实现

a、定义一个类,实现Runnable接口
b、造一个线程对象,传入接口对象
c、启动线程(线程对象.start())


4.线程的同步

synchronized
a、同步方法
b、同步资源

5.数据流

A:分类

a、方向:输入流,输出流  InputStream OutputStream
b、字节:字节流,字符流 Reader  Writer
c、功能:实体流,操作流

操作流:
a、BufferedInputStream,BufferedOutputStream 缓冲包装
b、DataInputStream,DataOutputStream 数据类型包装
c、ObjectInputStream,ObjectOutputStream 对象包装

B:设计模式

装修模式--Decorator

C:序列化

1.  implements Serializable 接口

2.序列化数据类型
a、基本型
b、String
c、对象(可序列化)
d、集合(放前面的)

 

1、文件夹操作 

a.创建目录包括上级目录文件夹

public static void main(String[] args) {
File dir=new File("d:/abc");
String[] files=dir.list();
}
private static void t1() {
//文件信息描述
File file=new File("d:/abc/cang");
if (file.exists()) {
System.out.println("有了");
file.delete();
}else {
System.out.println("还没有");
//创建目录 包括上级目录--上级路径不存在就不工作了
file.mkdirs();
}
}
}

b.创建文件

public static void main(String[] args) throws Exception  {
File file = new File("d:/abc", "你好.avi");
if (file.exists()) {
System.out.println("你好被干掉了");
file.delete();
} else {
file.createNewFile();
System.out.println("你好出来了");
}
}

2、文件操作

a.读取

private static String file = "d:/accounts.xml";
public static void main(String[] args) throws Exception {
// 文件输入流
InputStream is = new FileInputStream(file);

// XML读取流
SAXReader reader = new SAXReader();
Document doc = reader.read(is);

// 获得根元素
Element root = doc.getRootElement();

// 获得所有的儿子元素elements
List<Element> elements = root.elements();
for (Element e : elements) {
System.out.println("id:" + e.attributeValue("id"));
System.out.println("name:" + e.elementText("name"));
System.out.println("pass:" + e.elementText("pass"));
System.out.println("money:" + e.elementText("money"));
System.out.println("------------------------");
}
}

b.写入

private static String file = "d:/accounts.xml";
public static void main(String[] args) throws Exception {
// 1.创建XML文档对象
Document doc = DocumentHelper.createDocument();

// 2.加入根元素节点addElement
Element root = doc.addElement("accounts");

// 3.加入一个account
Element account = root.addElement("account");
// 属性节点addAttribute
account.addAttribute("id", "0001");

// 文本节点addText
account.addElement("name").addText("tom");
account.addElement("pass").addText("<b>123</b>");
account.addElement("money").addText("1000");

// 4.再加入一个account
account = root.addElement("account");
account.addAttribute("id", "0002");

account.addElement("name").addText("mary");
account.addElement("pass").addText("234");
account.addElement("money").addText("2000");

// 保存文档
save(doc);

System.out.println("game over");
}
private static void save(Document doc) throws Exception {
// 文件实体流--字符流
FileWriter fileWriter = new FileWriter(file);

// 输出格式化
OutputFormat of = OutputFormat.createPrettyPrint();

// 包装流--处理流
XMLWriter xmlWriter = new XMLWriter(fileWriter, of);

// 输出文档对象
xmlWriter.write(doc);

// 关闭资源
xmlWriter.close();
}

3、目录列表-递归 (举例打印某个盘的文件)

public static void main(String[] args) {
File file = new File("c:/");
listfiles(file);
}
private static void listfiles(File file) {
// 打印路径
System.out.println(file.getAbsolutePath());
try {
// 如果是文件夹 打印所以文件
if (file.isDirectory()) {
File[] files = file.listFiles();
for (File f : files) {
if (f.isFile()) {
System.out.println(f.getAbsolutePath());
} else if (f.isDirectory()) {
listfiles(f);
}
}
}
} catch (Exception e) {
// TODO: handle exception
}
}

4、数据流分类

a、方向:输入流,输出流  InputStream OutputStream
b、字节:字节流,字符流 Reader  Writer
c、功能:实体流,操作流

5.内部类(播放音乐)

public static void main(String[] args) {

  // 背景音乐--匿名累不累(内部类)--关键点 new 名称(){}---名称可以是类,抽象类,接口
  new Thread(new Runnable() {

   public void run() {
    String file = "/com/learn/music/Dj 出卖我.mp3";
    InputStream is = this.getClass().getResourceAsStream(file);
    try {
     new Player(is).play();
    } catch (JavaLayerException e) {
     e.printStackTrace();
    }

   }
  }).start();

 }

 


 


0 0
原创粉丝点击