当前位置: 移动技术网 > IT编程>开发语言>Java > java学习第一周

java学习第一周

2020年07月07日  | 移动技术网IT编程  | 我要评论

脱发第一周

这是我学习java的第一周,还是要有点仪式感的,那就在这记录一下,希望未来的我也能保持这个习惯,也能从事这个行业,并希望在多年后,当我翻到这些的时候,我是过得很开心的,有一份热爱的事业,有一个坚持的爱好,哈哈哈哈哈哈哈哈哈哈!
在这里插入图片描述
话不多说了,开始正文
在这学习的一周里面,我学了蛮多,也经历了蛮多,我意识到周围的小伙伴们很多大佬,本以为大家都是零基础,结果好像只有我是萌新,就在此时,我写这篇文章的时候,我的内心还是拔凉拔凉了,哈哈哈哈,开个玩笑,我还是对未来抱有很大的希望的。
对这周进行一个总结吧

  • String类
    对字符串的一些操作,如拼接,删除,添加,查找等等
package com.softeem.lesson22;

import java.io.UnsupportedEncodingException;

public class StringDemo {

	public static void main(String[] args) throws UnsupportedEncodingException {
		 
		String s1 = "abc";
		String s2 = new String();
		System.out.println(s2);
		
		
		System.out.println("");
		
		String s4 = new String("def");
		
		char[] c = {'h','e','l','l','o'};
		String s5 = new String(c);
		System.out.println(s5);
		
		String s6 = new String(c,0,2);
		System.out.println("-------------------------");
		System.out.println(s6);			
		//获取字符串中指定索引处位置
		char c1 = s5.charAt(3);
		System.out.println(c1); 
		
		//获取字符串中指定字符在字符集中的位置
		int i = s5.codePointAt(1);
		System.out.println(i);
		
		//按顺序比较相同位置的字符,返回字符之间的距离
		String s7 = "world";
		System.out.println(s7.compareTo(s5));

		//按顺序比较相同位置的字符,返回字符之间的距离,忽略字符大小写
		System.out.println(s7.compareToIgnoreCase(s5));
		System.out.println("s5"+s5);
		System.out.println("-----------");
		//将参数字符串和对象字符串对接
		System.out.println(s5.concat(s7));
		
		//判断当前字符串对象中是compareToIgnoreCase否包含指定的字符序列(连续)
		System.out.println(s5.contains("ho"));
		
		System.out.println(s5.contentEquals("hello"));
		
		String path = "http://www.softeem.com/src/xiaojiejie.png";
		//判断字符串是否以指定的字符串结尾
		System.out.println(path.endsWith(".png"));
		
		//忽略大小写比较是否一致
		System.out.println(s5.equalsIgnoreCase("HELLO"));
		
		//将字符串转换为字节数组
		byte[] byts = s5.getBytes("gbk");//可以更改编码
		for(byte b:byts) {
			System.out.print(b+"");
		}
		
		System.out.println();
		//获取指定字符第一次出现在字符集的索引位置
		System.out.println(s5.indexOf("h"));
		
		//获取指定字符串第一次出现在字符集的索引位置
		System.out.println(s5.indexOf("he"));
		
		//判断字符串的长度是否为0
		System.out.println(s5.isEmpty());
		
		System.out.println(s5.length());
		
		
		String[] arr = {"java","是","世界上","最好的语言"};
		//jdk1.8新增,用指定的分割符将字符串数组组合拼接起来
		String str = String.join("", arr);
		System.out.println(str);
		
		String phone = "12345678923";
		//判断给定的字符串是否匹配指定的正则表达式
		System.out.println(phone.matches("^1[2356789]\\d{9}$"));
		
		String comm = "少时诵诗书所";
		comm = comm.replace("少年","***" ).replace("所", "***");
		System.out.println(comm);
		//实现一个敏感词的过滤的方法,要求能对指定的一些敏感词进行脱敏处理
		//要求:1.,敏感词可以动态添加 2.发表评论内容时,自动脱敏处理 3.根据敏感词的字数使用对应个*替代
	}
	public String dengerString(String source) {
		
		
		
		return null;
	}

}

  • File类
    对文件的一些操作,增删改查等等,然后有个一个遍历目录然后按照一个树的格式输出
package com.softeem.lseeon23.filrs;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.Scanner;

public class FileDemo {

	public static void main(String[] args) throws IOException {
		
		//获取与本机系统相关的目录分隔符windows:\  linux: /
		System.out.println(File.separator);
		//以char类型返回目录分隔符
		System.out.println(File.separatorChar);
		//获取与本机系统相关的路径分隔符windows:;   linux为":"
		System.out.println(File.pathSeparator);
		
		//根据提供的文件路径创建一个File对象
		File f = new File("D:\\text.txt");
		
		//根据提供的文件所在的父路径,以及文件名创建File对象
		File f2 = new File("D:\\软帝","环境安装.pdf");
		
		if(!f.exists()) {
			//创建一个新的文件
			boolean r = f.createNewFile();
			System.out.println(r);
			
		}
		
		
		//创建一个指定前缀和后缀命名的临时文件
		File.createTempFile("temp", ".txt", new File("D:\\eclipse文件\\lesson23"));
		
		//删除指定的文件
//		r = f.delete();
//		System.out.println("删除结果:"+r);
		
		//当java虚拟机结束的时候删除文件
//		f.deleteOnExit();
//        Scanner sc = new Scanner(System.in);
//        sc.nextLine();
        
        //判断当前File对象所指的文件或目录是否存在
        System.out.println(f.exists());
        
        //当需要被删除的目录招标方存在文件或者子目录是此时删除会失败
        System.out.println(f.delete());
        System.out.println("程序执行完成-------");
        
        //获取当前File对象所表示的文件对象
        File file = f2.getAbsoluteFile();
        //获取当前File对象所表示的文件的绝对路径
        String path = f2.getAbsolutePath();
        System.out.println(file);
        System.out.println(path);
        
        //获取当前文件所在的磁盘的空闲空间
        System.out.println(f2.getFreeSpace()/(1024*1024*1024));
        
        //获取当前file对象所表示的文件或者目录名称
        System.out.println(f2.getName());
        
        //获取当前文件所在的父路径
        System.out.println(f2.getParent());
        System.out.println(f2.getParentFile());
        
        //获取文件所在路径
        System.out.println(f2.getPath());
        
        //判断当前file所表示的文件是否为标准文件(非目录)
        System.out.println(f2.isFile());
        
        f = new File("D:\\\\软帝\",\"环境安装.pdf");
        //判断当前file所表示的文件是否是一个目录
        System.out.println(f.isDirectory());
        
        f = new File("e:/qqpcmgr_docpro");
        //判断文件是否为隐藏文件
        System.out.println(f.isHidden());
        
        //获取文件最后修改时间
        long t = f.lastModified();
        System.out.println(new Date(t));
        
        //获取标准文件的大小
        f = new File("d:/BXPT.mdb");
        System.out.println(f.length());
	}

}


package com.softeem.lesson24.example;

import java.io.File;

public class FileUtils {
	
	public void showFileTree(File dir) {
		//获取目录中所有的子File对象(数组)
		File[] files = dir.listFiles();
		if(files != null) {
			for(int i = 0 ;i<files.length;i++) {
				//获取一个File 对象
				File f = files[i];
				//获取文件绝对路径
				String fpath = f.getAbsolutePath();
				System.out.println(f.getAbsolutePath());
				System.out.println(print(fpath));
				//判断f对象是否表示一个目录
				if(f.isDirectory()) {
					showFileTree(f);
				}
			}
		}	
	}
	
	public void showFileTree2(File dir,int level) {
		//获取目录中所有的子File对象(数组)
		File[] files = dir.listFiles();
		level++;
		if(files != null) {
			for(int i = 0 ;i<files.length;i++) {
				//获取一个File 对象
				File f = files[i];
				for (int n = 0; n < level; n++) {
					System.out.print("--");
				}
				System.out.println(f.getName());
				//判断f对象是否表示一个目录
				if(f.isDirectory()) {
					showFileTree2(f,level);
				}
			}
		}	
	}
	public String print(String path) {
		//使用指定的分隔符分割路径
		String[] arr = path.split("\\"+File.separator);
		String delimiter = "";
		for(int i = 0;i<arr.length;i++) {
			delimiter +="--";
		}
		//将文件名拼接到最后
		delimiter += arr[arr.length-1];
		return delimiter;
	}
	
	public static void main(String[] args) {
		File dir = new File("D:\\软帝");
//		new FileUtils().showFileTree(dir);
		new FileUtils().showFileTree2(dir, 0);
		
	}
}

  • Math类
    主要是对数字进行基本运算的方法,用的较多的是对数字进行取舍方法

  • date类
    该类主要是获取当前时间,里面很多方法已弃用了

  • calendar类
    该类里面方法用的比较多,有很多对时间进行操作的方法

package com.softeem.lesson24.date;

import java.util.Calendar;
import java.util.GregorianCalendar;

public class CalendarDemo {

	public static void main(String[] args) {
		
		Calendar c = Calendar.getInstance();
		//获取当前日历的所表示的年份
		int year = c.get(Calendar.YEAR);
		System.out.println(year);
		//月份
		int mouth = c.get(Calendar.MONTH);
		System.out.println(mouth);
		//日期,本月第几天
		int day = c.get(Calendar.DAY_OF_MONTH);
		System.out.println(day);
		//小时
		int hour = c.get(Calendar.HOUR_OF_DAY);//24小时
		System.out.println(hour);
		//分钟
		int min = c.get(Calendar.MINUTE);
		System.out.println(min);
		//秒钟
		int second = c.get(Calendar.SECOND);
		System.out.println(second);
		
		Calendar c2 = Calendar.getInstance();
		c2.set(Calendar.YEAR, 2021);
		c2.set(Calendar.MONTH,7);
		c2.set(Calendar.DAY_OF_MONTH,1);
		System.out.println(c2);
		System.out.println("------------");
		System.out.println(c2.get(Calendar.DAY_OF_WEEK));
		System.out.println("--------");
		//获取最大月份,改月是从0月开始,不是一月
		int max = c2.getActualMaximum(Calendar.MONTH);
		System.out.println(max);
		//获取当前月份最大天数
		int days = c2.getActualMaximum(Calendar.DAY_OF_MONTH);
		System.out.println(days);
		
		//判断是否为闰年
		GregorianCalendar gc = new GregorianCalendar();
		boolean leapYear = gc.isLeapYear(2021);
		System.out.println(leapYear);
		
	}

}
package com.softeem.lesson24.date;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
//import java.sql.Date
public class DateDemo {

	public static void main(String[] args) throws ParseException {
//		java.sql.Date d2 = new java.sql.Date(System.currentTimeMillis());
		
		// 创建一个java.util.date对象
		Date d1 = new Date();
		System.out.println(d1);
		
		//获取的时间的毫秒数(从1970.1.1.00.00.00开始到现在)
		long t = System.currentTimeMillis();
		Date d2 = new Date(t+10000);
		System.out.println(d2);
		
		//比较两个日期之间的大小,根据日期的先后顺序返回的是-1,0,1
		System.out.println(d2.compareTo(d1));
		
		//如果d1在d2之后,返回true
		System.out.println(d1.after(d2));
		//如果d1在d2之前。返回ture
		System.out.println(d1.before(d2));
		
		//返回从1970.1.1.00.00.00到当前日期之间的总毫秒数
		System.out.println(d1.getTime());
		
		/**日期格式化*/
		DateFormat fmt = DateFormat.getDateInstance();
		String date = fmt.format(d1);
		System.out.println(date);
		
		fmt = DateFormat.getDateTimeInstance();
		date = fmt.format(d2);
		System.out.println(date);
		
		//2020年07月03日 14时39分30秒
		fmt = new SimpleDateFormat("yyyy年MM月dd日  HH时mm分ss秒");
		//使用特定的格式化工具对日期格式化
		date = fmt.format(d2);
		System.out.println(date);
		
		date = "2020/07/03 14:41:42";
		//如果实现将上述的字符转换为java.util.Date对像
		SimpleDateFormat fmt1 = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
		Date newDate = fmt1.parse(date);
		 System.out.println(newDate);

	}

}

package com.softeem.lesson25;

import java.util.Calendar;

public class MyCalendar {
	
	private int year;
	private int mouth;
	
	public MyCalendar(int year,int mouth) {
		this.year = year;
		this.mouth = mouth;
	}
	public void showCalendar() {
		
		//获取日历实例
		Calendar c = Calendar.getInstance();
		c.set(Calendar.YEAR,year);
		c.set(Calendar.MONTH, mouth-1);
		//将日期设置为当月的第一天
		c.set(Calendar.DAY_OF_MONTH,1);
		//获取当天是本周的第几天
		int weekDay = c.get(Calendar.DAY_OF_WEEK)-1;
		if(weekDay == 0) {
			weekDay = 7;
		}
		//获取当前月份总天数
		int days = c.getActualMaximum(Calendar.DAY_OF_MONTH);
		System.out.println("------------------"+year+"年"+mouth+"月"+"-----------------------");
		System.out.println("一\t二\t三\t四\t五\t六\t日\t");
		System.out.println("--------------------------------------------------");
		//声明一个计数器,是否为7
		int count = 0;
		//打印输出空格
		for(int i =1;i<weekDay;i++) {
			System.out.print("\t");
			count++;
		}
		for (int i = 1; i <= days; i++) {
			if(count == 7) {
				System.out.println();
				count=0;
			}
			System.out.print(i+"\t");
			count++;
			
			
		}
		
	}
	public static void main(String[] args) {
		new MyCalendar(2020,7).showCalendar();
	}

}

这一周也做了很多小题目,总的来说把类里面的基本方法学会了,完成题,不是一件很难的事情,但是这是在看前面的案例的基础上,如果不看前面的例子,要完后还是有点困难的。
最后总结一下,这周过的非常充实,学习了很多东西,收获了很多。
这就是我的秃头之路的开始,谨以此纪念。

鲜衣路马少年时
一朝看尽长安花

本文地址:https://blog.csdn.net/lqydsb/article/details/107138881

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网