, :
File myFile = new File(File. separator + com
+ File. separator + myfile.txt);
:
Public static final String pathSeparator;
Public static final char pathSeparatorChar;
, Unix pathSeparator=;, Windows pathSeparator=:.
File , :
/* # 1: : FileTest.java */
package chapt09;
import java.io.*;
import java.util.*;
public class FileTest {
public static void main(String[] args) {
//c File FileTest2.java
File fp = new File("chapt09" + File. separator
+ "FileTest2.java");
if (fp.exists()) {
System. out. println(fp.getName() + " ");
if (fp.isFile()) { //
System. out. println(" :\t"
+ fp.getPath());
System. out. println(" :\t"
+ fp.getAbsolutePath());
System. out. println(" :\t"
+ fp.length());
System. out. println(" :\t"
+ new Date(fp.lastModified()));
System. out. println(" :\t"
+ fp.canRead());
System. out. println(" :\t"
+ fp.canWrite());
System. out. println(" :\t"
+ fp.delete());
}
} else
System. out. println(" " + fp.getName()
+ " ");
try {
if (fp.createNewFile())
System. out. println(" " + fp.getName()
+ " ");
} catch (IOException e) {
System. err. println(e);
}
// File \
// com.learn
File dir = new File("com" + File. separator + "learn");
if (dir.exists() && dir.isDirectory()) /*
*/
System. out. println(" "
+ dir.getName() + " ");
File[] files = dir.listFiles();
for (int i = 0; i < files.length; i++){
Date date = new Date(files[i].lastModified());
System. out. print("\n" + files[i].getPath()
+ " \t| " + files[i].length() + "\t| "
+ date.toString());
// toLocaleString() toGMTString()
}
// listRoots()
File root = File. listRoots ()[1];
System. out. printf("\n%s %,d %,d .", root.getPath(),root.getUsableSpace(),root.getTotalSpace());
}
}
FileTest2.java , :
FileTest2.java
: chapt09\FileTest2.java
: D:\workspace\chapt09\FileTest2.java
|
|
: 2091
: Fri Mar 31 12:26:50 EEST 2006
: true
: true
: true
FileTest2.java
learn
com\learn\bb.txt | 9 | Fri Mar 24 15:30:33 EET 2006
com\learn\byte.txt| 8 | Thu Jan 26 12:56:46 EET 2006
com\learn\cat.gif | 670 | Tue Feb 03 00:44:44 EET 2004
C:\ 3 665 334 272 15 751 376 896 .
File - list(), listFiles(),
listRoots().
/
- . / . / .
InputStream, - OutputStream. , , , . FileInputStream FileOutputStream, .
read() read(byte[] b) InputStream. -1, , int, byte. , try-catch . . FileInputStream , System.in InputStream . write(int b) OutputStream
. , . close(), .
, , , , pipe-, . . 9.1.
. 9.1.
FilterInputStream , InputStream. DataInputStream , 1.2 deprecated . BufferedInputStream .
.
. 9.2.
FilterOutputStream . BufferedOutputStream .
|
|
1.2 java.io . , , .
Unicode Reader Writer, , . , FileInputStream FileReader. .
/.
. 9.3. /
/* # 2: () : ReadDemo.java */
package chapt09;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class ReadDemo {
public static void main(String[] args) {
File f = new File("file.txt"); // !
int b, count = 0;
try {
FileReader is = new FileReader(f);
/* FileInputStream is = newFileInputStream(f);*/ / /
while ((b = is.read())!= -1) { /**/
System. out. print((char)b);
count++;
}
is.close(); //
} catch (IOException e) {
System. err. println(" : " + e);
}
System. out. print("\n = " + count);
}
}
FileReader(f) FileInputStream(f) is f. close(). n long skip(long n).
() () FileWriter Writer FileOutputStream OutputStream. write().
// # 3: : WriteRunner.java
package chapt09;
import java.io.*;
public class WriteRunner {
public static void main(String[] args) {
String pArray[] = { "2007 ", "Java SE 6" };
File fbyte = new File("byte.txt");
File fsymb = new File("symbol.txt");
try {
FileOutputStream fos =
new FileOutputStream(fbyte);
FileWriter fw = new FileWriter(fsymb);
for (String a: pArray) {
fos.write(a.getBytes());
fw.write(a);
}
fos.close();
fw.close();
} catch (IOException e) {
System. err. println(" : " + e);
}
}
}
, .
FileInputStream FileOutputStream RandomAccessFile , . , seek(long poz). :