栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java进阶

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java进阶

Iterationtest集合
package com.newdream.class5;

import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;

public class Iterationtest {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Collection schools=new HashSet();
		//往集合里添加数据
		schools.add("湖南大学");
		schools.add("中南大学");
		schools.add("湘潭大学");
		schools.add("交通大学");
		//将schools放入迭代器中
		Iterator iter=schools.iterator();
		while(iter.hasNext()){//判定是否结束
			String school=(String) iter.next();//取下一笔数据
			System.out.println(school);
			if(school.equals("交通大学")){
				iter.remove();//删除
			}
		}
		//将集合转成数组
		String[] sch=(String[]) schools.toArray(new String[0]);
		System.out.println(Arrays.toString(sch));
	}

}

Listtest

package com.newdream.class5;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Listtest {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//1. 新建一个list
		List aa=new ArrayList();
		//2. 插入数据
		aa.add("张三");
		aa.add("李四");
		aa.add("王五");
		aa.add("赵六");
		aa.add("田七");		
		//3. 求长度
		int size=aa.size();
		System.out.println("aa的长度为:"+aa.size());
		//4. 打印列表
		System.out .println(aa.toString());
		//5.移除
		aa.remove("田七");
		
		//6. 检查是否包含
		boolean flag=aa.contains("王五");
		if(flag){
			System.out.println("有这个人");
		}else{
			System.out.println("没有这个人");
		}
		//7. 复制到一个新的数组中
		String[] name=new String[5];
		aa.toArray(name);
		System.out.println(Arrays.toString(name));
		//8.清除
		aa.clear();
		System.out.println("清除后的数据为"+aa.toString());
	


	}

}
package com.newdream.Door;

public interface Alram {
	void alarm();

}
package com.newdream.Door;

public abstract class Door {
	public int height;
	public int wider;
	abstract void open();
	public void close(){
		System.out.println("是门就可以关,不然会进小偷!");
		}
	
}
package com.newdream.Door;

public class moveDoor extends Door{
	void open() {
		System.out.println("门高:"+height+",我是移门,往二边移动打开!");
		}

}
package com.newdream.Door;

public class onlyAlram implements Alram {
	public void alarm() {
		// TODO Auto-generated method stub
		System.out.println("我是报警器,我只能报警!");
		}

}
package com.newdream.Door;

public class pushAlarmDoor extends pushDoor implements Alram {
	void open() {
		// TODO Auto-generated method stub
		super.open();
		System.out.println("我也是具有报警功能,按了门铃,我才会开门");

}
	public void alarm() {
		// TODO Auto-generated method stub
		System.out.println("我是门铃,开门前请先按门铃!");
		}
}
package com.newdream.Door;

public class pushDoor extends Door{
	void open() {
		// TODO Auto-generated method stub
		System.out.println("门高:"+height+",我是推拿门,往里推打开!");
		}

}
package com.newdream.Door;

public class TestDoor {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//推拿门
		pushDoor pushD=new pushDoor();
		pushD.height=200;
		pushD.open();
		pushD.close();
		//移门
		System.out.println("-----------------");
		moveDoor moveD=new moveDoor();
		moveD.height=180;
		moveD.open();
		moveD.close();
		//推拿报警门
		System.out.println("-----------------");
		pushAlarmDoor paD=new pushAlarmDoor();
		paD.height=220;
		paD.alarm(); //按门铃
		paD.open(); 
		paD.close();
		//报警器
		System.out.println("-----------------");
		onlyAlram oA=new onlyAlram();
		oA.alarm(); 

	}

}
package com.newdream.class3;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;

public class CharIOreader {

	public static void main(String[] args) {
		BufferedReader br=null;
		try {
			FileReader fr=new FileReader("F:/T79.5/ljs.txt");
			br=new BufferedReader(fr);
			while(br.read()!=-1){
				String date=br.readLine();
				System.out.println(date);
			}
		} catch (Exception e) {
			System.out.println(e.toString());
		}finally{
			try{
				br.close();
			}catch(Exception e){
				System.out.println(e.toString());
			}
		}
	}

}
package com.newdream.class3;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.StringReader;

public class CharIOWriter {
	private static String data="sfdsgfgsdgdsgsdgss";
	public static void main(String[] args) {
		String line;
		BufferedWriter bw=null;
		BufferedReader br=null;
		try{
			FileWriter fr=new FileWriter("F:/T79.5/ljs.txt");
			bw=new BufferedWriter(fr);
			br=new BufferedReader(new StringReader(data));
			while((line=br.readLine())!=null){
				bw.write(line);
				bw.flush();
			}
			System.out.println("写入成功!");
		}catch(Exception e){
			System.out.println(e.toString());
		}finally{
			try{
				br.close();
				br.ready();
			}catch(Exception e){
				System.out.println(e.toString());
			}
		}

	}
}
package com.newdream.class3;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class fileCopy {
	public void fileCopt(String fileA,String fileB){
		File Filea=new File(fileA);
		File fileb=new File(fileB);
		FileInputStream fis=null;
		FileOutputStream fos=null;
		try{
			if(!fileb.exists()){
				fis=new FileInputStream(Filea);
				fos=new FileOutputStream(fileb);
				byte[] data=new byte[1024];
				int len=0;
				while((len=fis.read(data))!=-1){
					fos.write(data, 0, len);//从当前位置写数据字节,len长度
					fos.flush();
				}
			}else{
				System.out.println("目标文件已存在,不能复制!");
			}
			System.out.println("文件复制成功!");
		}catch(Exception e){
			System.out.println("操作文件失败:"+e.toString());
		}finally{
			try{
				fos.close();
				fis.close();
			}catch(Exception e){
				System.out.println("关闭文件失败:"+e.toString());
			}
		}
	}

}
package com.newdream.class3;

import java.io.File;
import java.io.IOException;

import test.test;

public class filetest {
	public static void main(String[] args){
		String filepath="F:/T79.5/ljs.txt";
		String root_path="F:/T79.5";
		
		filetest aa=new filetest();
		aa.ceateFile(filepath);
		aa.test_isDirectory(root_path);
		aa.get_filename(filepath);
		aa.get_filelength(filepath);
		aa.get_dirList(root_path);
		aa.createDir(root_path+"//test");//创建目录
		aa.del_file(filepath);//删除文件
		aa.del_file(filepath+"//test");//删除目录
		aa.del_file("F:/T79.5/ljs.txt");//不存在的文件名
	}
	//1.创建文件
	public void ceateFile(String filepath){
			try{
			File file=new File(filepath);
			if(file.exists()){//判定文件是否存在,如果存在的话,先删除再创建
				file.delete();//删除文件
				System.out.println("文件存在,已删除!");
			}
			boolean flag=file.createNewFile();
			if(flag){
				System.out.println("文件创建成功!");
			}else{
				System.out.println("文件创建失败!");
		}
			}catch(IOException e){
			e.printStackTrace();
			System.out.println("创建文件异常"+e.toString());
		}finally{
			
		}
	}
	//2.判定文件夹目录是否有效
	public void test_isDirectory(String root_path){
		File file=new File(root_path);
		if(file.isDirectory()){
			System.out.println("文件夹目录有效!");
			
		}else{
		    System.out.println("文件夹目录无效!");
		}
	}
	//3.获取文件名称和路径
	public void get_filename(String filepath){
		File file=new File(filepath);
		String name=file.getName();
		String path=file.getPath();
		System.out.println("文件路径为:"+path+",文件夹路径为:"+name);
	}

	//4.获取文件大小
	public void get_filelength(String filepath){
		File file=new File(filepath);
		long file_size=file.length();
		System.out.println("文件大小为:"+file_size);
	}
	//5.获取文件夹目录信息
	public void get_dirList(String root_path){
		File file=new File(root_path);
		String[] files=file.list();
		System.out.println(root_path+"文件目录信息:");
		for(String name:files){
			System.out.println(name);
		}
	}
	//6.创建文件目录
	public void createDir(String root_path){
		File file=new File (root_path);
		boolean flag;
		flag=file.mkdirs();
		if(flag)
			System.out.println(root_path+"文件目录创建成功!");
		else
			System.out.println(root_path+"文件目录创建失败!");
	}
	
	//7.删除目录
	public void del_file(String filepath){
		File file=new File(filepath);
		boolean flag=file.delete();
		if(flag)
			System.out.println("文件删除成功!");
		else
			System.out.println("文件删除失败!");
	}
}
package com.newdream.class3;

public class test {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		fileCopy cp=new fileCopy();
		cp.fileCopt("F:/T79.5/ljs.txt","F:/T80/ljs.txt");//复制文本文件
		cp.fileCopt("F:/T79.5/桌面.png","F:/T80/桌面.png");//复制照片
		cp.fileCopt("F:/T79.5/分歧者2.mp4","F:/T80/分歧者2.mp4");//复制电影
	
		
		
	}
	
}

IO流和多线程

线程

概念 :多线程程序包含两条或两条以上并发运行的部分。程序中每个这样的部分都叫一 个线程(thread) 实现线程的两种方式: 1,继承Thread 类 2,实现Runnable 接口

五大常用包

java 中常用五大包有哪些?

常用的五个

java.lang.* 常用包,基础类

提供利用 Java 编程语言进行程序设计的基础类。最重要的类是 Object(它是类层次结构的根)和 Class(它的实 例表示正在运行的应用程序中的类)。

java.util.* 工具类

包含集合框架、遗留的 collection 类、事件模型、日期和时间设施、国际化和各种实用工具类(字符串标记生成 器、随机数生成器和位数组、日期Date类、堆栈Stack类、向量Vector类等)。集合类、时间处理模式、日期时间工具等 各类常用工具包

java.io.* 读写

Java的核心库java.io提供了全面的IO接口。包括:文件读写、标准设备输出等。Java中IO是以流为基础进行输入输 出的,所有数据被串行化写入输出流,或者从输入流读入。

java.net.* 网络协议

并非所有系统都支持 IPv6 协议,而当 Java 网络连接堆栈尝试检测它并在可用时透明地使用它时,还可以利用系 统属性禁用它。在 IPv6 不可用或被显式禁用的情况下,Inet6Address 对大多数网络连接操作都不再是有效参数。虽然 可以保证在查找主机名时 java.net.InetAddress.getByName 之类的方法不返回 Inet6Address,但仍然可能通过传递字 面值来创建此类对象。在此情况下,大多数方法在使用 Inet6Address 调用时都将抛出异常。

java.sql.* 数据库

提供使用 JavaTM 编程语言访问并处理存储在数据源(通常是一个关系数据库)中的数据的 API。此 API 包括一个 框架,凭借此框架可以动态地安装不同驱动程序来访问不同数据源 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/343110.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号