a我考网

 找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 55|回复: 1

[JAVA] 2011年计算机等考二级JAVA学习精华整理(38)

[复制链接]
发表于 2012-7-31 22:04:26 | 显示全部楼层 |阅读模式
 1.3.4 File 类  File 可以表示文件也可以表示目录,File 类控制所有硬盘操作  b& I: R# b* K
  构造器:
8 n& T9 a; ~: e  File(File parent,String child) 用父类和文件名构造6 [" y1 Q5 E4 a
  File(String pathname) 用绝对路径构造
$ B! b) P6 ]# T2 }  File(String parent,String child) 用父目录和文件名构造
. y/ C7 u* ]0 {4 {! _! ?  File(URI uri) 用远程文件构造
* ]7 S+ P  m. u( Q" i4 d  常用方法:
' q: p0 \- k; n+ \, Z! K: Z  boolean createNewFile();8 @  C. r/ M) l" B6 D% I- E& Q2 E  k
  boolean exists();$ j' u/ M  [4 b6 S4 c
  例子:9 Z& T: r4 J' Q# \
  //建立 test.txt 文件对象,判断是否存在,不存在就创建
1 D7 Q1 ]/ J0 M( v$ w' I. x  import java.io.*;( I: R" j, b) ~8 N! o
  public class CreateNewFile{  w4 m" O3 d1 Z; W9 g! ]! D( E8 x
  public static void main(String args[]){
! R* A5 K( b- g/ \$ O2 Y/ a* E  File f=new File("test.txt");% r, e0 t5 J6 c! |& H) d, P
  try{
" f0 G. }/ L( ]" R1 j  if(!f.exists())
! w: c, s* s2 }2 _; q  f.createNewFile();
6 u7 @/ ?: [$ F+ C' z1 e$ P  else' f, O8 ~& ?$ \9 T. Y/ x
  System.out.println("exists");& w- ]* ?/ B( e! \4 t9 }6 {+ K
  }catch(Exception e){
) H' C" m1 d5 ]$ c' N1 \+ v  e.printStackTrace();* l+ z9 x9 I' `+ ]& Z9 G0 ?2 v, K3 U0 v
  }
$ |1 @' h5 }& X" _$ T% U  }% }; V% _( Q+ R! [* L
  }  g' `# {2 @; A% b5 N) X' n
  boolean mkdir()/mkdirs()
' u& b/ P& m8 W2 Z  boolean renameTo(File destination)$ `- I% \! c- M& ^$ z8 E" i3 S
  例子://看一下这 mkdir()/mkdirs() 的区别和 renameTo 的用法
+ ^; m6 S3 O* V# ?  import java.io.*;/ l7 E3 o  t5 |: D  e
  public class CreateDir{
6 B2 j, w" }) _/ Z- l! F  public static void main(String args[]){
) x5 e4 t- Z* R  h  File f=new File("test.txt");
, N' v8 D8 W, q# W  File f1=new File("Dir");
4 \" j  `6 j8 Q, O  File f2=new File("Top/Bottom");- q# o) c! i8 k3 O) {
  File f3=new File("newTest.txt");- ?: d) [0 X6 n2 I: E- H- y8 j
  try{
: |. B4 y* d) C5 a8 _3 G  f.renameTo(f3);& N# F. S, Y+ J: I5 @$ ~& e& h
  f1.mkdir();+ }2 M+ n* N& q% T5 D0 B
  f2.mkdirs();( X7 f/ k3 l- s, x$ W+ U% I0 u
  }catch(Exception e){- o$ J- h- @+ q. f  j# ~1 j
  e.printStackTrace();
. ]; t9 y- J1 \3 K7 |- _  }( M9 }) y% V, Q5 d6 |/ W
  }! l2 c: b# \6 P0 U; c: l6 l* ^0 r
  }
- f3 j, |" |3 A, ~  String getPath()/getAbsolutePath()$ L- ?/ p% y& d+ O  X4 M; F+ F7 F
  String getParent()/getName()
回复

使用道具 举报

 楼主| 发表于 2012-7-31 22:04:27 | 显示全部楼层

2011年计算机等考二级JAVA学习精华整理(38)

 例子://硬盘上并没有parent 目录和 test.txt 文件,但我们仍然可以操作,因为我们创建了他们的对象,是对对象进行操作  import java.io.*;; Q* }9 X0 |7 y2 Y+ t3 y; F4 V. Z1 `
  public class Test{3 e5 W( P: @- l) v
  public static void main(String args[]){
- D* ^7 P* f4 M# k% J  File f=new File("parent/test.txt");# l7 i- P0 i' {7 F0 Y
  File f1=new File("newTest.txt");
% W: p% Z9 j/ D9 ~, e7 _  try{" _- R2 m6 {& P0 B
  System.out.println(f.getParent());
3 }3 |6 t* Q9 v# W. `  System.out.println(f.getName());( u% T1 f$ }5 z3 N% `  N: @6 x
  System.out.println(f1.getPath());# p) q, I  j! T3 N* l  u8 U: q: V
  System.out.println(f1.getAbsolutePath());
, ~; M, K) A( s' K4 W, V  c  }catch(Exception e){" R3 O) v* j5 n! f3 K8 a" M
  e.printStackTrace();$ C5 N; n( n4 {
  }
4 F4 Q3 S! t* z$ C  }
% @3 X: d: l1 O8 e. q& D  }
6 D+ ?6 u8 a9 n/ Y9 u7 k  String list[] //显示目录下所有文件  d4 B8 K% v6 H: N' S) h* B1 Z
  long lastModified() //返回 1970.1.1 到最后修改时间的秒数+ m) F+ F* V( \; }- W, N3 _
  boolean isDirectory()( |0 n+ G% ~; j* J5 B( z+ R
  例子://列出目录下的所有文件和目录,最后修改时间,是目录的后面标出
, [; T2 U; b- a( Q  ,是文件的后面标出文件长度
9 Q7 `! R  r) y' W& |- I  import java.io.*;8 S; k9 a7 b7 b  e/ l# @
  import java.util.*;3 n' W$ |$ C+ N. T$ |
  public class Dir{# d" F) d+ `& H3 J" a9 D) r7 b
  public static void main(String args[]){
" m- {( V: E8 d( L  H3 s9 @  File f=new File("Dir");* H  |  H+ [& z! H& U
  String[] listAll=null;$ t3 u* O8 H$ o3 z7 ?5 t
  File temp=null;
: z8 B9 z; `1 ^" F- b/ B  try{0 J5 Z1 j9 _& s# J2 y
  listAll=f.list();0 P3 }8 p( @- M
  for(int i=0;i
: h4 y" P+ @2 U' \% W( ]  temp=new File(listAll);
/ w( b$ U' ~1 \  j( p! \  System.out.print(listAll+"\t");# k0 q  a' ~0 m, H
  if(temp.isDirectory())" e' _5 r9 D- P' E- e  c
  System.out.print("\t8 _5 j6 s/ X3 X) X0 |# x! A, [
  \t");! Y$ @$ ~' f# V
  else
! y- W( `. e% w) E' [) V  System.out.print(temp.length()+"\t");
5 K9 j( [( }& v' {6 M" m  System.out.println(new Date(temp.lastModified()));
6 Q! A1 q) O( e0 Y  }  s$ y( [. w  e( B
  }catch(Exception e){5 o' ]; v4 X6 b$ @; W
  e.printStackTrace();
4 s4 l: G& T( b- h, s  }0 S# S- _; _9 \
  }8 S4 l3 o! b- S% C# K$ d3 U
  }
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|Woexam.Com ( 湘ICP备18023104号 )

GMT+8, 2024-5-17 17:47 , Processed in 0.151380 second(s), 23 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表