zoukankan      html  css  js  c++  java
  • java笔试题

    一、数据类型转换

     1.string转int

           String a = "1234";
           int b = Integer.parseInt(a);
          System.out.println(b);

         2.int转string
           int aa = 123456;
      String aString =String.valueOf(aa);
      System.out.println(aString.getClass()+" "+aString);

     3.String转日期

      传两个string的日期,然后转成date比较两个日期相差天数

    public static void main(String[] args) {

        String string1 = "20191213";
        String string2 = "20191230";

    SimpleDateFormat sdf= new SimpleDateFormat("yyyyMMdd");
          try {
                Date date1 = (Date) sdf.parse(string1);
                Date date2 = (Date) sdf.parse(string2);
           int days = (int) ((date2.getTime()-date1.getTime()) / (1000*60*60*24));
         System.out.println(days);
        } catch (ParseException e) {
          e.printStackTrace();
        }
    }

    二、获取目录下的文件和文件夹

    public static void main(String[] args) {
    //获取目录下的文件和文件夹
        String path = "D:\";
        File file = new File(path);
        File[] tempList = file.listFiles();
        System.out.println("该目录下对象个数:"+tempList.length);
        for(int i=0 ;i<tempList.length;i++){
        if(tempList[i].isFile()) {
          System.out.println("文件 "+ tempList[i]);
        }
        if(tempList[i].isDirectory()) {
          System.out.println("文件夹"+ tempList[i]);
        }
      }
    }

    三、从一个文本里读取字符,将A转成a,输出到另一个文件

    p

    public static void main(String[] args) {
    String readFilePath = "D:/test11.txt";
    String saveFilePath = "D:/test22.txt";
    String readString = "";
    String changeString = "";
    try {
    //读取文件内容
    FileInputStream inputStream = new FileInputStream(readFilePath);
    InputStreamReader streamReader = new InputStreamReader(inputStream);
    BufferedReader reader = new BufferedReader(streamReader);
    while ((readString =reader.readLine())!= null) {
    if (readString.contains("A")) {
    changeString ="a";
    }
    }
    reader.close();
    //把读取的文件内容放到另外一个文件里面
    FileOutputStream outputStream = new FileOutputStream(saveFilePath);
    OutputStreamWriter streamWriter = new OutputStreamWriter(outputStream);
    BufferedWriter bufferedWriter= new BufferedWriter(streamWriter);
    bufferedWriter.write(changeString);
    bufferedWriter.flush();
    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }

  • 相关阅读:
    codeforces 980A Links and Pearls
    zoj 3640 Help Me Escape
    sgu 495 Kids and Prizes
    poj 3071 Football
    hdu 3853 LOOPS
    hdu 4035 Maze
    hdu 4405 Aeroplane chess
    poj 2096 Collecting Bugs
    scu 4444 Travel
    zoj 3870 Team Formation
  • 原文地址:https://www.cnblogs.com/flgg/p/12145767.html
Copyright © 2011-2022 走看看