• Java学习个人总结


    声明:个人原创,转载请在文章开头明显位置注明出处:https://www.cnblogs.com/sunshine5683/p/10063960.html

    学习从来都是一个阶段的学习,然后进行整理与总结,最后才能形成自己的东西,今天将最近所学习的内容整理总结出来,以便方便大家查阅与借鉴!下面每一个包都有时间说明,每一个包都是独立的demo,可以直接运行与测试!

    =====================================================================
    package test.xhq.day19;
    
    import java.io.File;
    
    public class Demo1_File {
        public static void main(String[] args) {
            String parent ="F:\Mystudy\java学习总结video\day19\video\001_今日内容.avi";
            String chiled = "001_今日内容.avi";
            File file = new File(parent, chiled);
            System.out.println(file.exists());
        
        }
        
        public static void demo1(){
            File file =new File("F:\Mystudy\java学习总结video\day19\video\001_今日内容.avi");
            System.out.println(file.exists());
            
            File file2 = new File("xxx.txt");
            System.out.println(file2.exists());
        }
        
    }
    ========================================================================
    package test.xhq.day19;
    import java.io.File;
    import java.io.IOException;
    
    public class Demo2_FileMethod {
    
        public static void main(String[] args) throws IOException {
            File file = new File("ccc.txt"); //声明一个file对象并赋予该对象文件类型及文件名
            System.out.println(file.createNewFile());//创建某类型的文件
            
    //        File file2  = new File("yyy1"); //声明一个file对象并赋予该对象文件夹名字或者路径
    //        System.out.println(file2.mkdir()); //创建文件夹
            
    //        File file2  = new File("yyy\ddd"); //这样的路径默认会创建在项目根目录下,如果想创建在某个盘,则写上该盘符和绝对路径即可
    //        System.out.println(file2.mkdirs()); //创建多级路径文件夹
        }
    }
    =============================================================================
    package test.xhq.day19;
    import java.io.File;
    
    public class Demo3_FileMethod {
        public static void main(String[] args) {
    //        File file = new File("ooo.txt");
    //        File file2 = new File("D:\xxx.txt");
    //        System.out.println(file.renameTo(file2));//renameto的功能是如果两个file路径相同则改名,路径不同则改名并剪切
            
    //        File file = new File("ooo.txt");
    //        System.out.println(file.delete()); //删除文件, Java中删除的文件不会保存在回收站
            
    //        File file = new File("yyy1");
    //        System.out.println(file.delete());//删除文件夹,注意该文件夹必须是空的或者没有下级文件夹
            
            File file = new File("yyy\ddd");
            System.out.println(file.delete());
        }
    }
    ===============================================================================
    package test.xhq.day19;
    import java.io.File;
    
    public class Demo4_FileMethod {
        public static void main(String[] args) {
    //        File dir1 = new File("yyy");
    //        System.out.println(dir1.isDirectory());//判断是否是目录
    //        
    //        File dir2 = new File("yyy.xls");//判断是否是文件 
    //        System.out.println(dir2.isFile());
            
            File file = new File("zzz");
            file.setReadable(false);
            System.out.println(file.canRead());//windows中设置不可读之后还是可读的,linux系统设置不可读就真的不可读了
            file.setWritable(false);
            System.out.println(file.canWrite());//windows系统中设置不可写。就真的不可写了
            
        }
    }
    ===============================================================================
    package test.xhq.day19;
    import java.io.File;
    import java.sql.Date;
    import java.text.SimpleDateFormat;
    import javax.xml.crypto.Data;
    
    public class Demo5_FileMethod {
        public static void main(String[] args) {
    //        File file = new File("ccc.txt");
    //        File file2 = new File("D:\user.xml");
    //        System.out.println(file.getAbsolutePath());
    //        System.out.println(file2.getAbsolutePath());//获取绝对路径,即文件实际在哪个目录下的全路径
    //        
    //        System.out.println(file.getPath());//获取构造方法中传入的路径,即与构造方法中传入的路径相同
    //        System.out.println(file2.getPath());
    //        
    //        System.out.println(file.getName());
    //        System.out.println(file2.getName());//获取文件夹的名字
    //        
    //        System.out.println(file.length());
    //        System.out.println(file2.length());//获取文件中字节的个数
    //        
    //        System.out.println(file.lastModified());//获取最后修改该文件的毫秒值
    //        
    //        Date d = new Date(file.lastModified());
    //        System.out.println(d);//时间显示修改
    //        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:MM:SS");
    //        System.out.println(sdf.format(d));//时间显示优化
            
            File dir = new File("F:/Mystudy/Java学习总结video/day19/video");
            String [] arr = dir.list(); //获取指定路径下的所有文件或者文件夹(仅仅为了获取文件名)
            for (String string : arr){
                System.out.println(string);
            }
            File[] subFiles= dir.listFiles();//获取指定路径下所有文件或者文件夹的File数组(为了获取文件对象)
            for ( File file : subFiles){
                System.out.println(file);
            }
        }
    }
    ==========================================================================
    package test.xhq.day19;
    import java.io.File;
    
    public class Demo6_FileMethod {
    
        public static void main(String[] args) {
            File dir = new File("E:\");//
    //        String[] arr = dir.list();//获取e盘下所有文件或文件夹(方式一)
    //        for(String string :arr){
    //            if (string .endsWith(".jpg")){ // 获取后缀名为".jpg"的文件
    //                System.out.println(string);
    //            }
    //        }
            File[] subFiles = dir.listFiles();//获取e盘下所有文件夹或文件(方式二,实际开发中使用此方式)
            for (File subFile:subFiles){
                if (subFile.isFile() && subFile.getName().endsWith(".rpb")){
                    System.out.println(subFile);
                }
            }
        }
    }
    =================================================================
    Day22
    
    package test.xhq.day22;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.SequenceInputStream;
    import java.util.Enumeration;
    import java.util.Vector;
    
    public class Demo1_SequenceInputStream {
    
        /**
         * @param args
         * SequenceInputStream可以将两个流整合在一起
         * @throws IOException 
         */
        public static void main(String[] args) throws IOException {
            
    //        FileInputStream fis = new FileInputStream("ccc.txt");//创建字节输入流,关联ccc.txt
    //        FileOutputStream fos= new FileOutputStream("c.txt");//创建字节输出流c.txt
    //        int b;
    //        while((b=fis.read())!=-1){                            //不断在ccc.txt上读字节
    //            fos.write(b);                                    //将读到的字节写到c.txt上
    //        }
    //        fis.close();                                        //关闭字节流
    //
    //        FileInputStream fis2 = new FileInputStream("bbb.txt");
    //        int b2;
    //        while((b2=fis2.read())!=-1){
    //            fos.write(b2);
    //        }
    //        fis2.close();
    //        fos.close();
            
            //整合两个输入流
    //        FileInputStream fis= new FileInputStream("ccc.txt");
    //        FileInputStream fis2 =new FileInputStream("bbb.txt");
    //        SequenceInputStream sis= new SequenceInputStream(fis,fis2);
    //        //SequenceInputStream sis2= new SequenceInputStream(sis,fis3);//如果有三个流对象时候,可以这样整合,因为sis也是流对象,单有多个流对象时候,还需要考虑
    //        FileOutputStream fos = new FileOutputStream("c.txt");
    //        int b;
    //        while ((b=sis.read())!=-1){
    //            fos.write(b);
    //        }
    //        sis.close();        //sis在关闭的时候会将构造方法中传入的流对象也都关闭
    //        fos.close();
            
            //整合多个输入流
            FileInputStream fis = new FileInputStream("bbb.txt");
            FileInputStream fis2= new FileInputStream("ccc.txt");
            FileInputStream fis3= new FileInputStream("c.txt");
            Vector<FileInputStream> v= new Vector<FileInputStream>();//创建集合对象,将流对象存储进来
            v.add(fis);
            v.add(fis2);
            v.add(fis3);
            
            Enumeration<FileInputStream> en= v.elements();
            SequenceInputStream sis= new SequenceInputStream(en); //将枚举中的输入流整合成一个
            FileOutputStream fos=new FileOutputStream("d.txt");
            int b;
            while((b=sis.read())!=-1){
                fos.write(b);
            }
            sis.close();
            fos.close();    
        }
    
    }
    ======================================================
    package test.xhq.day22;
    
    import java.io.ByteArrayOutputStream;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    
    /*
     * 内存输出流
     * 
     * 解决方案:
     * 字符流读取
     * ByteArrayOutputStream
     * 
     * */
    public class Demo2_ByteArrayOUtputStream {
        public static void main(String[] args) throws IOException {
    //        FileInputStream fis= new FileInputStream("c.txt");
    //        byte[] arr= new byte[3];
    //        int len;
    //        while((len=fis.read(arr))!=-1){
    //            System.out.println(new String(arr,0,len));
    //        }    
            
            FileInputStream fis = new FileInputStream("c.txt");
            ByteArrayOutputStream baos= new ByteArrayOutputStream();//在内存中创建科可以增长的内存数组
            
            int b;
            while((b=fis.read())!=-1){
                baos.write(b);            //将读取到的数据逐个写入到内存中
            }
            
            //byte[] arr=baos.toByteArray();        //将缓冲区 的数据全部获取出来。并赋值给array数组
            //System.out.println(new String(arr));
            
            System.out.println(baos.toString()); //将缓冲区的内容转换成字符串,在输出语句中可以省略调用toStreang方法
            
            fis.close();
        }
    }
    
    ================================================
    package test.xhq.day22;
    
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.ObjectOutputStream;
    import java.util.ArrayList;
    
    import test.xhq.day22.object.Person;
    
    public class Demo3_ObjectOutputStream {
    
        /**
         * @param args
         * @throws IOException 
         * 存档
         */
        public static void main(String[] args) throws IOException {
    //        Person pe=new Person("张三", 23);
    //        Person pe2=new Person("李四", 24);
    //        
    //        ObjectOutputStream os= new ObjectOutputStream(new FileOutputStream("e.txt"));
    //        os.writeObject(pe2);
    //        os.writeObject(pe2);
    //        os.close();
            
            Person pe=new Person("张三", 23);
            Person pe2=new Person("李四", 24);
            Person pe3= new Person("王五", 25);
            Person pe4= new Person("赵六", 26);
            
            ArrayList<Person> list=new ArrayList<Person>();
            list.add(pe);
            list.add(pe2);
            list.add(pe3);
            list.add(pe4);    
            ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("e.txt"));
            oos.writeObject(list);        //把整个集合对象一次写出
            oos.close();
        }
    
    }
    =======================================================================
    package test.xhq.day22;
    
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.ObjectInputStream;
    import java.util.ArrayList;
    
    import test.xhq.day22.object.Person;
    
    public class Demo4_ObjectInputStream {
    
        /**
         * @param args
         * @throws IOException 
         * @throws FileNotFoundException 
         * @throws ClassNotFoundException 
         * 对象输入流,反序列化
         */
        public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
    //        ObjectInputStream ois= new ObjectInputStream(new FileInputStream("e.txt"));
    //        Person p1= (Person)ois.readObject();
    //        Person p2=(Person)ois.readObject();
    //        //Person p3=(Person)ois.readObject();//当文件读取到了末尾时出现EOFException
    //        System.out.println(p1);
    //        System.out.println(p2);
    //        ois.close();
            
            ObjectInputStream ois= new ObjectInputStream(new FileInputStream("e.txt"));
            ArrayList<Person> list=(ArrayList<Person>)ois.readObject();    //将集合对象一次读取
            for(Person person:list){
                System.out.println(person); 
            }
            ois.close();
        }
    
    }
    ==========================================================================
    package test.xhq.day22;
    
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.PrintStream;
    import java.io.PrintWriter;
    
    import test.xhq.day22.object.Person;
    
    public class Demo5_PrintStream {
    
        /**
         * @param args
         * @throws FileNotFoundException 
         * PrintStream和PrintWriter分别是打印的字节流和字符流
         * 且只操作数据目的的
         */
        public static void main(String[] args) throws FileNotFoundException {
            //Demo1();
            //测试
            PrintWriter  pw= new PrintWriter(new FileOutputStream("f.txt"),true);
            pw.println(97);            //自动刷出只针对的是Println()方法
            //pw.write(97);
            pw.close();
    
        }
    
        private static void Demo1() {
            System.out.println("aaa");
            PrintStream ps = System.out;
            ps.println(97); // 底层通过ToString()将97转换成对应的字符串打印出来
            ps.write(97); // 查找码表找到对应的a打印出来
            
            Person p1= new Person("张三",23);
            ps.println(p1);         //默认调用p1的ToString()方法
            
            Person p2= null;
            ps.println(p2);//打印引用数据类型。如果是null。就打印null,如果不是null,就打印对象的ToString()
            ps.close();
        }
    
    }
    ============================================================================
    package test.xhq.day22;
    
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.PrintStream;
    
    public class Demo6_SystemIn {
        public static void main(String[] args) throws IOException {
            //Demo1();
            System.setIn(new FileInputStream("a.txt"));//改变标准输入流
            System.setOut(new PrintStream("b.txt"));//改变标准输出流
            
            InputStream is= System.in;//获取标准键盘输入流,改变后指向文件
            PrintStream ps=System.out;//获取标准输出流,默认指向的是控制台,改变后就指向文件
            
            int b;
            while((b=is.read())!=-1){
                ps.write(b);
            }
            System.out.println();//也是一个标准输出流,不用关闭,因为没有和硬盘上的文件有关联的管道
            is.close();
            ps.close();
        }
    
        private static void Demo1() throws IOException {
            InputStream is= System.in;//一般情况下将systemin封装到scaner中,效果更好
            int x=is.read();
            System.out.println(x);
            is.close();
            
            InputStream is2=System.in;
            int y=is2.read();
            System.out.println(y);
        }
        
        
    }
    ================================================
    package test.xhq.day22;
    
    import java.io.ByteArrayOutputStream;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    
    public class Test1 {
    
        /**
         * @param args
         * 定义一个文件输入流。调用read(byte[]b)方法。将a.txt文件中的内容打印出来
         * 
         * 分析:
         * 1、read(byte[] b)是字节输入流中的方法,创建FileInputStream。关联a.txt
         * 2、创建内存输出流,将读到的数据写到内存输出流中
         * 3、创建字节数组,长度为5(读取中文肯定会乱码,所以采取ByteArrayOutputStream 方式)
         * 4、将内存输出流的数据全部转换为字符串打印
         * 5、关闭输入流
         * @throws IOException 
         */
        public static void main(String[] args) throws IOException {
            //1、read(byte[] b)是字节输入流中的方法,创建FileInputStream。关联a.txt
            FileInputStream fis= new FileInputStream("a.txt");
            //2、创建内存输出流,将读到的数据写到内存输出流中
            ByteArrayOutputStream baos= new ByteArrayOutputStream();
            //3、创建字节数组,长度为5
            byte[] arr= new byte[5];
            int len ;
            
            while((len=fis.read(arr))!=-1){
                baos.write(arr,0,len);
                System.out.println(new String (arr,0,len));//如果没有ByteArrayOutputStream,则使用此方式,而此方式肯定会乱码
            }
            // 4、将内存输出流的数据全部转换为字符串打印
            System.out.println(baos);//即使没有调用,底层也会默认帮我们调用toString()方法
            //5、关闭输入流
            fis.close();
    
        }
    
    }
    ==========================================
    package test.xhq.day22;
    
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.PrintStream;
    
    
    //输入流也可以拷贝图片,但是开发中部推荐使用
    public class Test2 {
        public static void main(String[] args) throws IOException {
            System.setIn(new FileInputStream("xx.jpg"));//改变标准输入流
            System.setOut(new PrintStream("copy.jpg"));//改变标准输出流
            InputStream is=System.in;
            PrintStream ps=System.out;
            
            byte[] arr=new byte[1024];
            int len;
            while((len=is.read(arr))!=-1){
                ps.write(arr,0,len);
            }
            is.close();
            ps.close();
            
        }
    }
    =====================================================
    package test.xhq.day22.object;
    
    import java.io.Serializable;
    
    public class Person implements Serializable{ //序列化
        /**
         * 
         */
        private static final long serialVersionUID = 1L;//可以有可以没有,如果没有,则在修改此类后没
                                                        //有存档(即没有执行Demo3_ObjectOutputStream类)
                                                        //直接读取(即直接执行Demo4_ObjectInputStream类)
                                                        //时,提示序列号错误,并且序列号随机,而加上此行,则没修
                                                        //改一次手动将该值加1,在没有存档直接读取时就会报出具体的
                                                        //序列号。注意:无论加不加此行,当修改了本类后没有存档直接
                                                        //直接读取都会报错
    
    
        /**
         * @param args
         */
        private String name;
        private int age;
        private String gender;
        
        public Person(String name, int age) {
            super();
            this.name = name;
            this.age = age;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    
        @Override
        public String toString() {
            return "Person [name=" + name + ", age=" + age + ", getName()="
                    + getName() + ", getAge()=" + getAge() + ", getClass()="
                    + getClass() + ", hashCode()=" + hashCode() + ", toString()="
                    + super.toString() + "]";
        }
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
    
        }
    
    }

    到此最近的内容总结完毕!继续下一阶段的学习。

  • 相关阅读:
    awk 字符串函数
    C标准函数库中获取时间与日期、对时间与日期数据操作及格式化
    Redis 命令总结
    个人网站和博客赚钱之路(转)
    二叉树的前序、中序、后序遍历与创建
    16个值得个人站长做的广告联盟[转自cnzz]
    mysql 添加[取消]timestamp的自动更新
    转:函数式编程初探
    可爱的 Python : Python中的函数式编程,第三部分
    可爱的 Python : Python中函数式编程,第二部分
  • 原文地址:https://www.cnblogs.com/sunshine5683/p/10063960.html
Copyright © 2020-2023  润新知