列出某个目录下的所有文件:
File file = new File("e:\\总结"); File[] files = file.listFiles(); for(int i=0; i<files.length; i++){ if(files[i].isFile()) System.out.println(files[i]); }
列出某个目录下的所有子目录:
File file = new File("e:\\总结"); File[] files = file.listFiles(); for(int i=0; i<files.length; i++){ if(files[i].isDirectory()) System.out.println(files[i]); }
如何读写文件,代码如下,有读取,复制,写入:
1 public class ReadWriteCopy { 2 public static void main(String args[]) { 3 String from = "h:\\Android\\readtext.txt"; 4 String to = "h:\\Android\\writetext.txt"; 5 readline(from); 6 copyText(from,to); 7 writeText(to); 8 //deletefile(from); 9 } 10 11 public static void deletefile(String from){ 12 File fromFile=new File(from); 13 if(fromFile.exists()){ 14 fromFile.delete(); 15 } 16 } 17 public static void readline(String from) { 18 File fromFile = new File(from); 19 BufferedReader reader=null; 20 //FileReader fr=null;//可以用这个 21 if (fromFile.exists()) { 22 try { 23 //InputStreamReader isr=new InputStreamReader(new DataInputStream(new FileInputStream(fromFile)),"gb2312"); 24 //或者 25 InputStreamReader isr=new InputStreamReader(new BufferedInputStream(new FileInputStream(fromFile),1024),"gb2312"); 26 reader=new BufferedReader(isr); 27 List<users> userList=new ArrayList<users>(); 28 String row=""; 29 while ((row=reader.readLine()) != null) { 30 users u=new users(); 31 String[] text=row.split(","); 32 u.setUserName(text[0]); 33 u.setUserId(text[1]); 34 u.setUserDate(text[2]); 35 userList.add(u); 36 } 37 for(users u:userList){ 38 System.out.println("用户名:"+u.getUserName()+"id:"+u.getUserId()+"date:"+u.getUserDate()); 39 } 40 }catch (IOException e1) { 41 // TODO Auto-generated catch block 42 e1.printStackTrace(); 43 }finally{ 44 try { 45 reader.close(); 46 } catch (IOException e) { 47 // TODO Auto-generated catch block 48 e.printStackTrace(); 49 } 50 } 51 } 52 } 53 54 public static void copyText(String from,String to){ 55 File fromFile = new File(from); 56 File toFile = new File(to); 57 BufferedInputStream fis=null; 58 BufferedOutputStream fos=null; 59 try { 60 fis=new BufferedInputStream(new FileInputStream(fromFile)); 61 fos=new BufferedOutputStream(new FileOutputStream(toFile)); 62 63 int num; 64 while((num=fis.read())!=-1){ 65 fos.write(num); 66 } 67 } catch (FileNotFoundException e) { 68 // TODO Auto-generated catch block 69 e.printStackTrace(); 70 } catch (IOException e) { 71 // TODO Auto-generated catch block 72 e.printStackTrace(); 73 }finally{ 74 try { 75 fis.close(); 76 fos.close(); 77 } catch (IOException e) { 78 // TODO Auto-generated catch block 79 e.printStackTrace(); 80 } 81 } 82 83 } 84 85 public static void writeText(String to){ 86 String text="往txt文件里写入该字符串"; 87 File toFile = new File(to); 88 FileOutputStream fos=null; 89 FileWriter fw=null; 90 try { 91 fos=new FileOutputStream(toFile); 92 fos.write(text.getBytes()); 93 94 fw=new FileWriter(toFile); 95 fw.write(text); 96 } catch (FileNotFoundException e) { 97 // TODO Auto-generated catch block 98 e.printStackTrace(); 99 } catch (IOException e) { 100 // TODO Auto-generated catch block 101 e.printStackTrace(); 102 } 103 } 104 }
时间: 2024-11-30 02:08:29