分享7:java面试(编程部分)

最近看到有小伙伴跳槽发的一些面试题,大致如下:
源码链接:https://pan.baidu.com/s/17y7d0iVISobpXYIzXEXFoQ
提取码:52r4

1.关于文件夹操作相关 :题目大致是想知道小伙伴对文件的已操作(文件查找,递归等相关知识)

在这里插入图片描述

	public void testFile(String path) {
        File file = new File(path);
        if(file.isDirectory()) {//是目录
            File [] files = file.listFiles();
            for(File f : files) {
                if(f.isDirectory()) {//目录
                    testFile(f.getPath(), ft);//递归
                } else {//文件
                	System.out.println(f.getName());
                }
            }
        } else{ //是文件
        	System.out.println(f.getName());
        }
    }

2.文件流相关:大致对缓存字符流的考察,已经正则的一些知识

在这里插入图片描述

 	File file = new File(path);
        BufferedWriter bw = null;
        BufferedReader br = null;
        FileReader fr = null;
        try {
            fr = new FileReader(file);
            br = new BufferedReader(fr);
            String brcode = null;
            String name = file.getName().split("\\.")[0];
            bw  = new BufferedWriter(new FileWriter(file.getParentFile()+File.separator+name+"-1.txt"));
            while ((brcode = br.readLine()) != null) {//读文件行读取,
                System.out.println(brcode);
                brcode = brcode.replaceAll("c/c\\+\\+test" , "java_");//替换 核心就是++在正则中的意义
                bw.newLine();//换行,可以在输出前后,建议在后
                bw.write(brcode);//写文件
            }
            bw.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            if(bw != null) {
                bw.close();
            }
            if(br != null) {
                br.close();
            }
            if(bw!= null) {
                bw.close();
            }
        }

3.数据格式读写相关:这里我们考察对象的操作,以及数据的序列化等(transient 字段的应用)

在这里插入图片描述

    public void writeObj(String path) throws Exception {
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(path));
        Data da = new Data(1, "ww", "eee" , new Date());
        System.out.println(da);
        oos.writeObject(da);
        oos.flush();
        oos.close();
    }

    public void readerObje(String path) throws Exception {
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(path));
        Data ta = (Data) ois.readObject();
        ois.close();
        System.out.println(ta);
    }

	class Data implements Serializable {
	    int id;
	    String author;
	    String comment;
	    Date time;
	   xxxx
  	}

4.读取配置文件相关:解析配置文件:已xml为例,主要涉及如何生成,以及如何读取内容

在这里插入图片描述

//解析
public void dom4j(String path) {
        SAXReader reader = new SAXReader();
        try {
            Document document = reader.read(path);//读取为Doc对象
            Element element = document.getRootElement();//获取根元素
            Iterator iterator = element.elementIterator();//迭代
            while(iterator.hasNext()) {
                Element stu = (Element) iterator.next();//获取元素
                List<Attribute> attributes = stu.attributes();//获取元素属性集合,也可以获取指定属性
                //"======获取属性值======"
                for (Attribute attribute : attributes) {//遍历所有属性
                    System.out.println(attribute.getValue());
                }
                //"======遍历子节点======"
                Iterator iterator1 = stu.elementIterator();//元素子节点
                while (iterator1.hasNext()){//这里只取了两级更多可以夸张
                    Element stuChild = (Element) iterator1.next();
                    System.out.println("节点名:"+stuChild.getName()+"---节点值:"+stuChild.getStringValue());
                }
            }
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }
    
//生成
	try {
            // 1、创建document对象
            Document document = DocumentHelper.createDocument();
            // 2、创建根节点
            Element txml = document.addElement("testxml");
            // 3、节点添加属性
            txml.addAttribute("version", "2.0");
            // 4、生成子节点及子节点内容
            Element channel = txml.addElement("tests");
            Element title = channel.addElement("test");
            title.setText("xml测试1");

            title = channel.addElement("test");
            title.setText("xml测试2");
            // 5、设置生成xml的格式
            OutputFormat format = OutputFormat.createPrettyPrint();
            // 设置编码格式
            format.setEncoding("UTF-8");


            // 6、生成xml文件
            File file = new File(path + File.separator+"testxml.xml");
            XMLWriter writer = new XMLWriter(new FileOutputStream(file), format);
            // 设置是否转义,默认使用转义字符
            writer.setEscapeText(false);
            writer.write(document);//输出doc
            writer.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

这里只是大致记录拿到这个资料后的第一步编程,后续将深入解析。


本周趣事:
1.看了上海欢乐谷的水幕表演,比3d更真实,目前也需灯光秀貌似要借助一些常见介质,不能做到全息投影的方式,猜想全息投影技术必将深入日常生活中。电影,电话,远程教学等。
2.钓鱼,体验钓鱼的趣事,感觉这样生活也和惬意呐。
3.淘宝盖楼大战,套路太深了,以为会有很多小伙伴玩这个东西,但是经过一周的助楼,发现身边很多小伙伴都不玩这个了,有一种感觉就是没有前几年的活动实在了哈。


以上仅为个人看法,望指教。

发布了55 篇原创文章 · 获赞 29 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/ljcc122/article/details/102979075
今日推荐