分享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.淘寶蓋樓大戰,套路太深了,以爲會有很多小夥伴玩這個東西,但是經過一週的助樓,發現身邊很多小夥伴都不玩這個了,有一種感覺就是沒有前幾年的活動實在了哈。


以上僅爲個人看法,望指教。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章