public class Test {
public static void main (String [] args ) {
File mFile =new File ("f://hello" );
if (!mFile .exists ()) {
try {
mFile .createNewFile ();
} catch (IOException mE ) {
mE .printStackTrace ();
}
}
}
}
public class Test {
public static void main (String [] args ) {
System .out .println (File .separator );
System .out .println (File .pathSeparator );
}
}
D :\java \jdk \b in \java "-javaagent
\
;
Process finished with exit code 0
public class Test {
public static void main (String [] args ) {
File mFile =new File ("f://hello" );
if (mFile .exists ()) {
mFile .delete ();
}
}
}
public class Test {
public static void main (String [] args ) {
File mFile =new File ("f://hello" );
if (!mFile .isDirectory ()) {
mFile .mkdirs ();
}
}
}
public class Test {
public static void main (String [] args ) {
File mFile =new File ("f://hello" );
if (!mFile .isDirectory ()) {
mFile .mkdirs ();
}
String []mStrings =mFile .list ();
for (int i = 0 ; i <mStrings .length ; i ++) {
System .out .println (mStrings [i ]);
}
File [] mFiles =mFile .listFiles ();
for (int i = 0 ; i <mFiles .length ; i ++) {
System .out .println (mFiles [i ]);
}
}
}
D :\java \jdk \b in \java "-javaagent
hello1
tet2.txt
txt1.txt
f:\h ello\h ello1
f:\h ello\t et2.txt
f:\h ello\t xt1.txt
Process finished with exit code 0
public class Test {
public static void main (String [] args ) {
File mFile =new File ("f://hello" );
if (!mFile .isDirectory ()) {
mFile .mkdirs ();
}
printAll (mFile );
}
private static void printAll (File mFile ) {
if (mFile == null ) {
return ;
}
if (mFile .isDirectory ()) {
File []mFiles =mFile .listFiles ();
if (mFiles !=null &&mFiles .length !=0 ) {
for (int i = 0 ; i < mFiles .length ; i ++) {
printAll (mFiles [i ]);
System .out .println (mFiles [i ]);
}
}else {
return ;
}
}
}
}
D :\java \jdk \b in \java "
f:\h ello\h ello1\h ello2\t ext4.txt
f:\h ello\h ello1\h ello2
f:\h ello\h ello1\t xt3.txt
f:\h ello\h ello1
f:\h ello\t et2.txt
f:\h ello\t xt1.txt
Process finished with exit code 0
public class Test {
public static void main (String [] args ) {
File mFile =new File ("f://hello" );
if (!mFile .isDirectory ()) {
mFile .mkdirs ();
}
printAll (mFile );
}
private static void printAll (File mFile ) {
if (mFile == null ) {
return ;
}
if (mFile .isDirectory ()) {
File []mFiles =mFile .listFiles ();
if (mFiles !=null &&mFiles .length !=0 ) {
for (int i = 0 ; i < mFiles .length ; i ++) {
printAll (mFiles [i ]);
if (mFiles [i ].toString ().endsWith (".txt" )) {
}
System .out .println (mFiles [i ]);
}
}else {
return ;
}
}
}
}
D :\java \jdk \b in \java
f :\hello \hello1 \hello2 \t ext4 .txt
f :\hello \hello1 \hello2
f :\hello \hello1 \t xt3 .txt
f :\hello \hello1
f :\hello \hello5
f :\hello \t et2 .txt
f :\hello \t xt1 .txt
Process finished with exit code 0
public class Test {
public static void main (String [] args ) {
File mFile =new File ("f://hello//tex1.txt" );
try {
FileOutputStream mFileOutputStream =new FileOutputStream (mFile );
String mS ="hello" ;
byte []mBytes =mS .getBytes ();
mFileOutputStream .write (mBytes );
mFileOutputStream .close ();
} catch (FileNotFoundException mE ) {
mE .printStackTrace ();
} catch (IOException mE ) {
mE .printStackTrace ();
}
}
}
public class Test {
public static void main (String [] args ) {
File mFile =new File ("f://hello//tex1.txt" );
try {
FileOutputStream mFileOutputStream =new FileOutputStream (mFile );
String mS ="hello" ;
byte []mBytes =mS .getBytes ();
for (int i = 0 ; i <mBytes .length ; i ++) {
mFileOutputStream .write (mBytes [i ]);
}
mFileOutputStream .close ();
} catch (FileNotFoundException mE ) {
mE .printStackTrace ();
} catch (IOException mE ) {
mE .printStackTrace ();
}
}
}
public class Test {
public static void main (String [] args ) {
File mFile =new File ("f://hello//tex1.txt" );
try {
FileOutputStream mFileOutputStream =new FileOutputStream (mFile ,true );
String mS ="world" ;
byte []mBytes =mS .getBytes ();
for (int i = 0 ; i <mBytes .length ; i ++) {
mFileOutputStream .write (mBytes [i ]);
}
mFileOutputStream .close ();
} catch (FileNotFoundException mE ) {
mE .printStackTrace ();
} catch (IOException mE ) {
mE .printStackTrace ();
}
}
}
public class Test {
public static void main (String [] args ) {
File mFile =new File ("f://hello//tex1.txt" );
try {
FileInputStream mFileInputStream =new FileInputStream (mFile );
byte []mBytes =new byte [(int ) mFile .length ()];
mFileInputStream .read (mBytes );
System .out .println (mBytes .length );
System .out .println (new String (mBytes ));
mFileInputStream .close ();
} catch (FileNotFoundException mE ) {
mE .printStackTrace ();
} catch (IOException mE ) {
mE .printStackTrace ();
}
}
}
public class Test {
public static void main (String [] args ) {
File mFile =new File ("f://hello//tex1.txt" );
try {
FileInputStream mFileInputStream =new FileInputStream (mFile );
byte []mBytes =new byte [(int ) mFile .length ()];
for (int i = 0 ; i <mBytes .length ; i ++) {
mBytes [i ]= (byte ) mFileInputStream .read ();
}
System .out .println (mBytes .length );
System .out .println (new String (mBytes ));
mFileInputStream .close ();
} catch (FileNotFoundException mE ) {
mE .printStackTrace ();
} catch (IOException mE ) {
mE .printStackTrace ();
}
}
}
public class Test {
public static void main (String [] args ) {
File mFile =new File ("f://hello//tex1.txt" );
try {
FileInputStream mFileInputStream =new FileInputStream (mFile );
byte []mBytes =new byte [1024 ];
int len =0 ;
int index =0 ;
while ((len = mFileInputStream .read ())!=-1 ) {
mBytes [index ++]= (byte ) len ;
}
System .out .println (index --);
System .out .println (new String (mBytes ));
mFileInputStream .close ();
} catch (FileNotFoundException mE ) {
mE .printStackTrace ();
} catch (IOException mE ) {
mE .printStackTrace ();
}
}
}
public class Test {
public static void main (String [] args ) {
File mFile =new File ("f://hello//tex1.txt" );
try {
FileWriter mFileWriter =new FileWriter (mFile );
mFileWriter .write ("helloAndroid" );
mFileWriter .close ();
} catch (IOException mE ) {
mE .printStackTrace ();
}
}
}
public class Test {
public static void main (String [] args ) {
File mFile =new File ("f://hello//tex1.txt" );
try {
FileReader mFileReader =new FileReader (mFile );
char []mChars =new char [1024 ];
mFileReader .read (mChars );
System .out .println (new String (mChars ));
} catch (IOException mE ) {
mE .printStackTrace ();
}
}
}
public class Test {
public static void main (String [] args ) {
File mFile =new File ("f://hello//tex1.txt" );
try {
FileReader mFileReader =new FileReader (mFile );
char []mChars =new char [1024 ];
int len =0 ;
int index =0 ;
while ((len =mFileReader .read ())!=-1 ){
mChars [index ++]= (char ) len ;
}
System .out .println (index --);
System .out .println (new String (mChars ));
} catch (IOException mE ) {
mE .printStackTrace ();
}
}
}
文件复制(图片之类的复制使用字节流,文本一般是使用字符流)
public class Test {
public static void main (String [] args ) {
File mFile =new File ("f://hello//tex1.txt" );
File mFile1 =new File ("f://hello//tex1Copy.txt" );
try {
FileInputStream mFileInputStream =new FileInputStream (mFile );
FileOutputStream mFileOutputStream =new FileOutputStream (mFile1 );
int len =0 ;
while ((len =mFileInputStream .read ())!=-1 ){
mFileOutputStream .write (len );
}
mFileInputStream .close ();
mFileOutputStream .close ();
} catch (FileNotFoundException mE ) {
mE .printStackTrace ();
} catch (IOException mE ) {
mE .printStackTrace ();
}
}
}
public class Test {
public static void main (String [] args ) {
File file =new File ("f://hello//tex1.txt" );
Writer out = null ;
try {
out = new OutputStreamWriter (new FileOutputStream (file ));
out .write ("hello" );
out .close ();
} catch (FileNotFoundException mE ) {
mE .printStackTrace ();
} catch (IOException mE ) {
mE .printStackTrace ();
}
}
}
public class Test {
public static void main (String [] args ) {
File file =new File ("f://hello//tex1.txt" );
Reader read = null ;
try {
read = new InputStreamReader (new FileInputStream (file ));
char [] b =new char [100 ];
int len =0 ;
int index =0 ;
while ((len =read .read ())!=-1 ){
b [index ++]= (char ) len ;
}
System .out .println (new String (b ));
read .close ();
} catch (FileNotFoundException mE ) {
mE .printStackTrace ();
} catch (IOException mE ) {
mE .printStackTrace ();
}
}
}
public class Test {
public static void main (String [] args ) {
try {
FileReader in = new FileReader ("f://hello//tex1.txt" );
FileWriter out = new FileWriter ("f://hello//tex1Copy.txt" );
BufferedReader br = new BufferedReader (in );
BufferedWriter bw = new BufferedWriter (out );
String str = null ;
while ((str = br .readLine ()) != null ){
bw .write (str );
bw .newLine ();
bw .flush ();
}
in .close ();
out .close ();
br .close ();
bw .close ();
} catch (FileNotFoundException mE ) {
mE .printStackTrace ();
} catch (IOException mE ) {
mE .printStackTrace ();
}
}
}