Академический Документы
Профессиональный Документы
Культура Документы
A File object represents a file or directory pathname. You can pass an absolute path to a
file or directory like this:
File file1 = new File ("C:\\temp\\myNote.txt"); // in Windows
File file2 = new File ("/tmp/myNote.txt"); // in Linux/Unix
getName(): Returns the name of the file without the path or the directory name
import java.io.File;
public class MainClass {
public static void main(String[] a) {
File myFile = new File("C:" + File.separator + "jdk1.5.0" + File.sep
arator, "File.java");
System.out.println(myFile.getName());
System.out.println(myFile.getPath());
}
}
import java.io.File;
import java.net.URI;
public class MainClass {
public static void main(String[] a) throws Exception {
File remoteFile = new File(new URI("file:///index.htm"));
}
}
11. 2. 4. isAbsolute(): Returns true if the File object refers to an absolute pathname, and false
otherwise.
import java.io.File;
public class MainClass {
public static void main(String[] a) {
File myFile = new File("C:" + File.separator + "jdk1.5.0" + File.sep
arator, "File.java");
System.out.println(myFile.isAbsolute());
}
}
true
The string that is returned is the same as that returned by the getPath() method.
import java.io.File;
public class MainClass {
public static void main(String[] a) {
File myFile = new File("C:" + File.separator + "jdk1.5.0" + File.sep
arator, "File.java");
System.out.println(myFile.getParentFile());
System.out.println(myFile);
}
}
11. 2. 14. getAbsolutePath(): Returns the absolute path for the directory or file referenced by the current File
import java.io.File;
public class MainClass {
public static void main(String[] a) {
File myFile = new File("C:" + File.separator + "jdk1.5.0" + File.separator, "File.j
System.out.println(myFile.getAbsolutePath());
}
}
C:\jdk1.5.0\File.java
import java.io.File;
public class MainClass {
public static void main(String[] a) {
File file = new File("c:\\test\\");
file.mkdir();
}
}
public class FileAttributesDemo {
public static void main(String[] args) throws IOException {
// Create a new file, by default canWrite=true, readonly=false
File file = new File("test.txt");
if (file.exists()) {
file.delete();
}
file.createNewFile();
System.out.println("Before. canWrite?" + file.canWrite());
// set to read-only, atau canWrite = false */
file.setWritable(false);
System.out.println("After. canWrite?" + file.canWrite());
}
}
import java.io.DataInputStream;
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.IOException;
public class MainClass {
public static void main(String[] args) throws Exception {
DataInputStream din = null;
try {
FileInputStream fin = new FileInputStream("myfile.dat");
din = new DataInputStream(fin);
while (true) {
int theNumber = din.readInt();
System.out.println(theNumber);
}
} catch (EOFException ex) {
din.close();
} catch (IOException ex) {
System.err.println(ex);
}
}
}
public class MainClass {
public static void main(String[] args) throws IOException {
DataInputStream dis = new DataInputStream(new BufferedInputStream(ne
w FileInputStream(
"temp.tmp")));
for (int i = 0; i < 10; i++)
dis.readInt();
dis.close();
}
}
public class MainClass {
public static void main(String[] args) throws IOException {
try {
DataInputStream in3 = new DataInputStream(
new ByteArrayInputStream("a dbcde".getBytes()));
while(true)
System.out.print((char)in3.readByte());
} catch(EOFException e) {
System.err.println("End of stream");
}
}
}//
import java.io.DataInputStream;
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.IOException;
public class MainClass {
public static void main(String[] args) throws Exception {
DataInputStream din = null;
try {
FileInputStream fin = new FileInputStream("myfile.dat");
din = new DataInputStream(fin);
while (true) {
int theNumber = din.readInt();
System.out.println(theNumber);
}
} catch (EOFException ex) {
din.close();
} catch (IOException ex) {
System.err.println(ex);
}
}
}
import java.io.DataInputStream;
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.IOException;
public class MainClass {
public static void main(String[] args) {
try {
FileInputStream fin = new FileInputStream("myFile.dat");
DataInputStream din = new DataInputStream(fin);
while (true) {
double theNumber = din.readDouble();
System.out.println(theNumber);
}
} catch (EOFException e) {
} catch (IOException e) {
// abnormal termination
System.err.println(e);
}
}