java笔记之String与Object的转换

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
public class FileTest {
 public static void main(String[] args) throws IOException {
  // TODO Auto-generated method stub
  File file1 = new File("D:/filetest/1/");
  // file1.createNewFile();
  // System.out.println(file1);
  // System.out.println(file1.getPath());
  // System.out.println(file1.exists());
  // System.out.println(file1.createNewFile());
  // System.out.println(file1.getAbsolutePath());
  // System.out.println(file1.getName());
  // String[] str = file1.list();
  // System.out.println(Arrays.toString(str));
  File[] stt = file1.listFiles();
  System.out.println(Arrays.toString(stt));
  // System.out.println(str.length);
  int len = stt.length;
  int len1 = 0;
  ArrayList list = new ArrayList();
  for (int i = 0; i < len; i++) {
   if (stt[i].isFile()) {
    list.add(stt[i].toString());
    len1++;
   }
  }
/*
 * 三种object转String方式
 * (String)
 * toString
 * String.valueOf(object)
 */
  Object[] sty = list.toArray();
//   String[] sty = (String[])list.toArray();//error
  String[] sto = new String[10];
  String[] sti = list.toArray(sto);//2<集合长度,所以集合sty指向了新的数组而不是sty
  for (int i = 0; i < len1; i++) {
   // System.out.println(sty[i].lastIndexOf(".")+" "+sty[i].length());
   if (((String) sty[i]).substring((String.valueOf(sty[i])).lastIndexOf("."), ((String) sty[i]).length())
     .equals(".java")) {
    System.out.println(sty[i].toString());
   }
  }
//  for (int i = 0; i < len1; i++) {
//   // System.out.println(sty[i].lastIndexOf(".")+" "+sty[i].length());
//   if (((String) sty[i]).substring(((String) sty[i]).lastIndexOf("."), ((String) sty[i]).length())
//     .equals(".class")) {
//    System.out.println((String) sty[i]);
//   }
//  }
  for (int i = 0; i < len1; i++) {
   // System.out.println(sty[i].lastIndexOf(".")+" "+sty[i].length());
   if ((sti[i]).substring((sti[i]).lastIndexOf("."), ( sti[i]).length())
     .equals(".java")) {
    System.out.println(sti[i]);
   }
  }
  
 }
}

你可能感兴趣的:(java笔记之String与Object的转换)