JAVA-经典的基础案例片段

来源:互联网 发布:九黎影视源码 编辑:程序博客网 时间:2024/06/02 06:43
/**
     * Scanner类的使用实例
     */
    public void compete() {
        Scanner scan = new Scanner(System.in);
        //获取键盘输入的扫描
        int compet[] = new int[10];
        compet[0] = scan.nextInt();
        //int 值输入
        compet[1] = scan.nextShort();
        //char  值输入
        compet[2] = scan.nextByte();
        //baty 值输入
        String str = scan.nextLine();
        //字符串输入
        boolean nextBoolean = scan.nextBoolean();
        //布尔值输入
        double nextDouble = scan.nextDouble();
        //Double值输入

    }


/**
 *
 *Calender
 *日历类的用法
 */
    public static void main(String[] args) {
        Calendar cal=Calendar.getInstance();
        System.out.println("年:"+cal.get(Calendar.YEAR));
        System.out.println("月:"+(cal.get(Calendar.MONTH)+1));
        System.out.println("日:"+cal.get(Calendar.DAY_OF_MONTH));
        System.out.println("时:"+cal.get(Calendar.HOUR_OF_DAY));
        System.out.println("分:"+cal.get(Calendar.MINUTE));
        System.out.println("秒:"+cal.get(Calendar.SECOND));
        System.out.println("当前是今年的第"+cal.get(Calendar.DAY_OF_YEAR)+"天");
        System.out.println("*********************************");
        cal.add(Calendar.DAY_OF_MONTH,5);  // 给当前日期加5天
        System.out.println("5天以后,月:"+(cal.get(Calendar.MONTH)+1));
        System.out.println("5天以后,日:"+cal.get(Calendar.DAY_OF_MONTH));
    }




/**
 * jvm
 * runtime类常用方法
 */
    public static void main(String[] args)throws Exception {
        Runtime run=Runtime.getRuntime();
        System.out.println("JVM中的内存总量:"+run.totalMemory());
        System.out.println("JVM中目前空闲内存总量:"+run.freeMemory());
        run.exec("notepad.exe");
        run.exec("calc.exe");
        run.exec("F:/Program Files (x86)/Tencent/QQ/Bin/QQ.exe");
    }


/**

*字符串常用方法

*/

public static void main(String[] args) {
        String str="中华人民共和国";
        System.out.println(str.charAt(2));  // 返回指定索引处的 char 值,索引从0开始
        System.out.println(str.contains("共和"));   // 判断str是否包含"共和"字符串
        System.out.println(str.startsWith("中"));   // 测试此字符串是否以指定的前缀开始
        System.out.println(str.endsWith("共和国"));  // 测试此字符串是否以指定的后缀结束
        System.out.println(str.length());
        System.out.println("************");
        String str2="Hello World";
        System.out.println(str2.indexOf("ll"));
        System.out.println(str2.lastIndexOf("l"));
        System.out.println(str.substring(3));   // 从索引为3处开始截取字符串
        System.out.println(str.substring(1, 5));  // 截取指定索引范围的子字符串,beginIndex包括,endIndex不包括
        byte[] data=str.getBytes();   // 将字符串转化成byte[]
        String s=new String(data);   // 将byte[]转化成String
        System.out.println(s);
        String temp="abcde";
        System.out.println(temp.toUpperCase());
        char[] cArray=str.toCharArray();   // 将此字符串转换为一个新的字符数组
        for(int i=0;i<cArray.length;i++){
            System.out.print(cArray[i]+"、");
        }
        

    }



/**
 * Date类常用方式
 */
    public static void main(String[] args) {
        Date date=new Date();  // 类Date表示特定的瞬间
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String str=sdf.format(date);
        System.out.println(str);
    }


/**

*IO流

*简单实例

*/

public static void main(String[] args) {
        Writer win = null;
        try {
            win = new FileWriter("e:" + File.separator + "新建文件夹" + File.separator + "tt.txt");
        
            win.write("\r\n黄河入海流.");
            win.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                win.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }


InputStream input=null;
        try {
            input=new FileInputStream("e:"+File.separator+"笔记"+File.separator+"Oracle_config.txt");
            /**
            int temp=input.read();
            byte[] data=new byte[1];
            data[0]=(byte)temp;
            System.out.println(new String(data));
            **/
            byte[] b=new byte[1024];
            int len=input.read(b);   // 读取内容
            System.out.println(new String(b,0,len));
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            try {
                input.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }    }


/**
 * IO流简单应用
 * 文件的复制
 * 上传下载的原型,底层实现
 */
public class ByteArrayInputStreamDemo {

    public static void main(String[] args) {
        InputStream input=null;
        OutputStream out=null;
        ByteArrayOutputStream bos=new ByteArrayOutputStream();
        ByteArrayInputStream bis=null;
        try {
            //读取的原文件路径
            input=new FileInputStream("d:"+File.separator+"images"+File.separator+"daodejing.jpg");
            //复制的文件存放路径
            out=new FileOutputStream("e:"+File.separator+"JavaEE1601"+File.separator+"道德经.jpg");
            byte[] b=new byte[1024];
            int len=0;
            while((len=input.read(b))!=-1){
                bos.write(b, 0, len);    // 写入内存
            }
            bis=new ByteArrayInputStream(bos.toByteArray());   // 准备读取图片数据
            while((len=bis.read(b))!=-1){
                out.write(b, 0, len);//对目标文件进行循环读取
            }
            System.out.println("复制成功!");
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            try {
                bis.close();
                out.close();
                input.close();
                bos.close();//关闭资源
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}


1 0
原创粉丝点击