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();
}
}

 

posted on 2020-01-03 16:59  北朔  阅读(190)  评论(0编辑  收藏  举报