java入门级十五个简易案例

(1) location返回其中域名
public static void main(String[] args) {
public static String getHostName(String location) {
String name1=getHostName(“http://www.baidu.com”);
//截取开始位置(第一个“.”之后第一个字符的位置)
System.out.println(“name1:”+name1);
//name1:baidu
int start=location.indexOf(".")+1;
String name2=getHostName(“http://www.tedu.cn”);
//截取末尾位置(第二个“.”的位置)
System.out.println(“name2:”+name2);
//name2:tedu
int end=location.indexOf(".",start);
String name3=getHostName(“doc.canglaoshi.com.cn”);
return location.substring(start,end);}
System.out.println(“name3:”+name3);
}
//name3:canglaoshi

(2)检测一个字符串是否为回文? 回文:正反念都是一句话
方法1:
String str=“上海自来水来自海上”;
for(int i=0;i<str.length();i++) {
char s1=str.charAt(i);
char s2=str.charAt(str.length()-(i+1));
if(s1!=s2) {
System.out.println(“此字符串不是回文”);
break;
}
System.out.println(“此字符串为回文”);
break;
}
方法2:
for(int i=0;i<str.length()/2;i++) {
if(str.charAt(i)!=str.charAt(str.length()-1-i)) {
System.out.println(“不”);
break;
}
System.out.println(“是回文”);
}

(3)参数名:username 参数值:张三 参数名:age 参数值:16 注:某个参数无值时,参数值输出为null
String line=“username=张三&&age=16&&gender=男
&&salsy=5000”;
String[] s=line.split("&&");
//按照"&&“拆分每一个参数
for(int i=0;i<s.length;i++) {
String[] s1=s[i].split(”=");
//每个参数在按照"="拆分
System.out.println(“参数名:”+s1[0]);
if(s1.length>1) {
//数组长度>1说明拆分出2项
System.out.println(“参数值:”+s1[1]);
}else {
//不大于说明无参
System.out.println(“参数值:null”);
}
}

(4)生成一个4位数验证码,验证码可以出现的符为英文字符和数字,包括英文大小写
StringBuilder builder=new StringBuilder();
for(int i=0;i<26;i++) {
builder.append((char)(‘a’+i));
builder.append((char)(‘A’+i));
if(i<10) {
builder.append(i);
}
}
String line=builder.toString();
StringBuilder b=new StringBuilder();
Random rand=new Random();
for(int i=0;i<4;i++) {
b.append(line.charAt(rand.nextInt(line.length())));
}
System.out.println(“生成的4位数验证码
为:”);
System.out.println(b);

(5)输出当前目录下所有文本文件(后缀为.txt)
File file=new File(".");
if(file.isDirectory()) {
FileFilter fiter=new FileFilter() {
public boolean accept(File f) {
return file.getName().endsWith(".txt");
}
};
File[] subs=file.listFiles(fiter);
for(int i=0;i<subs.length;i++) {
File sub=subs[i];
System.out.println(sub.getName());
}
}
(6))删除一个目录
public static void main(String[] args) {
File dir=new File("./a");
delete(dir);
}
//删除时要判定file表示的是否为一个目录,要是目录需要先清空目录,然后再删除该目录,如果file表示的是一个文件,则直接删除
private static void delete(File file) {
if(file.isDirectory()) {
//先清空该目录
File[] subs=file.listFiles();
//先获取所有子项
for(int i=0;i<subs.length;i++) {
File sub=subs[i];
delete(sub);
//一个方法内部调用自己的这个方法的现象称为递归调用 }
file.delete();
}
}

(7)输出给定目录下的所有子项的名字(包括子项的子项···)
public static void main(String[] args) {
File file=new File(".");
show(file);
}
private static void show(File file) {
if(file.isDirectory()) {
File[] a1=file.listFiles();
for(int i=0;i<a1.length;i++) {
System.out.println(a1[i].getName());
show(a1[i]);
}
}
}
(8)简易记事本工具
程序启动后,要求输入一个文件名,然后创建RAF对该文件操作,之后在控制台输入的每个字符串都写入该文件中(字符集统一用UTF-8),然后单独输入exit时程序退出
Scanner scan=new Scanner(System.in);
System.out.println(“创建文件名称:”);
String fileName=scan.nextLine();
RandomAccessFile raf=new RandomAccessFile(fileName,“rw”);
System.out.println(“输入内容:”);
while(true) {
String nr=scan.nextLine();
if(“exit”.equals(nr)) {
break;
}
byte[]b=nr.getBytes(“UTF-8”);
raf.write(b);
}
System.out.println(“已退出”);
raf.close();
(9)用户注册
1)注册信息分4块:用户名,密码,昵称,年龄 ,除了年龄为int值之外,其余3个都是字符串,将每个注册用户都写到user.dat文件中
Scanner scan=new Scanner(System.in);
System.out.println(“请输入用户名”);
String username=scan.nextLine();
System.out.println(“请输入密码”);
String password=scan.nextLine();
System.out.println(“请输入昵称”);
String nickname=scan.nextLine();
System.out.println(“请输入年龄”);
int age=scan.nextInt();
RandomAccessFile raf=new RandomAccessFile(“user.dat”,“rw”);
raf.seek(raf.length());
//指针移至末尾,防止下一条用户信息覆盖前面
byte[] b=username.getBytes(“UTF-8”);
b=Arrays.copyOf(b, 32);//扩容到32字节 raf.write(b);
b=password.getBytes(“UTF-8”);
b=Arrays.copyOf(b, 32);//扩容到32字节 raf.write(b);
b=nickname.getBytes(“UTF-8”);
b=Arrays.copyOf(b, 32);//扩容到32字节 raf.write(b);
raf.writeInt(age);
System.out.println(“成功注册”); raf.close();
2)将user.dat文件所用用户输出到控制台
RandomAccessFile raf=new RandomAccessFile(“user.dat”,“r”);
for(int i=0;i<raf.length()/100;i++) {
byte[] b=new byte[32]; raf.read(b);
String username=new String(b,“UTF-8”).trim(); raf.read(b);
String password=new String(b,“UTF-8”).trim(); raf.read(b);
String nickname=new String(b,“UTF-8”).trim();
int age=raf.readInt();
System.out.println(“用户名:”+username+“密码:”+password+“昵称:”+nickname+“年龄:”+age);
}
raf.close();
(10)用户登录操作
程序启动后要求输入用户名和密码,如果用户名密码都正确输入则提示:登录成功,如果密码输入错误湖或者该用户名在user.dat文件中不存在,则提示:用户名或密码错误
RandomAccessFile raf=new RandomAccessFile(“user.dat”,“r”);
Scanner scan=new Scanner(System.in);
System.out.println(“请输入用户名”);
String username1=scan.nextLine();
System.out.println(“请输入密码”);
String password1=scan.nextLine();
byte[] b=new byte[32];
int a=0;
for(int i=0;i<raf.length()/100;i++) {
raf.seek(i*100);
raf.read(b);
String username=new String(b,“UTF-8”).trim();
if(username.equals(username1)) {
raf.read(b);
String password=new String(b,“UTF-8”).trim();
if(password.equals(password1)) {
a++;
System.out.println(“登录成功”); }
break;
}
}
if(a==0) {
System.out.println(“用户名或密码错误”);
}
(11)用户注册
(一)程序启动后1)要求用户输入用户名、密码、昵称、年龄 2)然后创建一个User实例并利用序列化将对象写入文件,文件名格式:用户名。obj 3)判断创建的用户是否存在
Scanner scan=new Scanner(System.in);
String username;
while(true) {
System.out.println(“请输入用户名”);
username=scan.nextLine();
if(!new File(username+".obj").exists()) {
break;
}
System.out.println(“该用户名已存在”);
}
System.out.println(“请输入密码”);
String password=scan.nextLine();
System.out.println(“请输入昵称”);
String nickname=scan.nextLine();
System.out.println(“请输入年龄”);
int age=scan.nextInt();
User u=new User(username,password,nickname,age);
FileOutputStream fos=new FileOutputStream(username+".obj");
ObjectOutputStream oos=new ObjectOutputStream(fos);
oos.writeObject(u);
System.out.println(“用户注册成功”); oos.close();
(二)输出所有注册用户
FileFilter filter = (file)->file.getName().endsWith(".obj");
File file = new File(".");
File[] subs = file.listFiles(filter);
for(int i=0;i<subs.length;i++) {
File sub = subs[i];
FileInputStream fis = new FileInputStream(sub);
ObjectInputStream ois = new ObjectInputStream(fis);
Object o = ois.readObject();
if(o instanceof User) {
System.out.println(o);
}
ois.close();
}
(12)文本文件转换字符集
程序启动后,输入要转换字符集的文本文件的名字,然后再输入该文件的字符集名字然后再输入要转换的字符集名字。之后程序会将该文件内容以指定的字符集写入另一个文件。
Scanner scanner = new Scanner(System.in);
System.out.println(“请输入文件名:”);
String fileName = scanner.nextLine();
//demo.txt
System.out.println(“请输入该文件的字符集:”);
String charset = scanner.nextLine();
//utf-8
System.out.println(“请输入要转换的字符集:”);
String newCharset = scanner.nextLine();
//gbk
//截取原文件名
String name = fileName.substring(0,fileName.lastIndexOf("."));
//截取原文件的后缀名
String ext = fileName.substring(fileName.lastIndexOf(".")+1);
try (
FileInputStream fis = new FileInputStream(fileName);
InputStreamReader isr = new InputStreamReader(fis,charset);
BufferedReader br = new BufferedReader(isr);
FileOutputStream fos = new
FileOutputStream(name+"_"+newCharset+"."+ext);
//demo_gbk.txt
OutputStreamWriter osw = new
OutputStreamWriter(fos,newCharset);
BufferedWriter bw = new BufferedWriter(osw);
PrintWriter pw = new PrintWriter(bw); )
{
String line = “”;
while((line = br.readLine())!=null) {
pw.println(line);
}
System.out.println(“转码完毕!”); } catch (Exception e) {
e.printStackTrace();
}
}
}

(13)生成10个随机整个整数(100以内),按照从大到小的顺序排序,要求自定义比较规则
Random rand=new Random();
List list=new ArrayList<>();
for(int i=0;i<10;i++) {
list.add(rand.nextInt(100));
}
System.out.println(list);
//[80, 63, 59, 62, 95, 30, 40, 72, 77, 8]
Collections.sort(list,(o1,o2)->o2-o1);
System.out.println(list);
//[95, 80, 77, 72, 63, 62, 59, 40, 30, 8]
生成20个不重复的随机数(100以内)

(14)然后按从大到小排序,排序后再将第5 -15个数按照从小到大排序后输出结果
从大到小排序数字时要自行定义比较器,提示: Compartor接口中的compar(T o1,T o2)方法返回值的三种取值范围 返回值>0时表示o1>o2 返回值<0时表示o1<o2 返回值=0时表示o1=o2
Random rand=new Random();
Collection ci=new HashSet<>();
while(ci.size()<20) {
ci.add(rand.nextInt(100));
}
List list=new ArrayList<>(ci);
//ci集合导入list集合
System.out.println(“初始集合元素:”+list);
Collections.sort(list,(o1,o2)->o2-o1);
//lambda表达式排序
System.out.println(“从大到小排序:”+list);
Collections.reverse(list.subList(4, 15));
//反转第5-15元素
System.out.println(“最终结果:”+list);

(15)递归:第1个人10,第2个比第1个人大2岁,依次递推,请用递归方式计算出第8个人多大?
public class 递归 {
public static void main(String[] args) {
System.out.println(computeAge(8)); }
public static int computeAge(int n){
if(n==1) return 10;
return computeAge(n-1) + 2;
}
public static void toBinary(int n,StringBuffer result) {
if(n/2 != 0)
toBinary(n/2,result);
result.append(n%2);
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值