读书人

JAVA内的最简单的IO操作有关问题

发布时间: 2012-02-28 13:06:36 作者: rapoo

JAVA内的最简单的IO操作问题
package 使用FILE类;

import java.io.*;
import java.util.*;

public class TestFileClass {
public static void main(String[] args) {
// Create a File object
File file = new File( "D:\\JAVA\\eclipse\\工作区\\简单输入输出\\us.txt ");
System.out.println( "Does it exist? " + file.exists());
System.out.println( "Can it be read? " + file.canRead());
System.out.println( "Can it be written? " + file.canRead());
System.out.println( "Is it a directory? " + file.isDirectory());
System.out.println( "Is it a file? " + file.isFile());
System.out.println( "Is it absolute? " + file.isAbsolute());
System.out.println( "Is it hidden? " + file.isHidden());
System.out.println( "What is its absolute path? " +
file.getAbsolutePath());



try {
System.out.println( "What is its canonical path? " +
file.getCanonicalPath());
}
catch (IOException ex) { }

System.out.println( "What is its name? " + file.getName());
System.out.println( "What is its path? " + file.getPath());
System.out.println( "When was it last modified? " +
new Date(file.lastModified()));

System.out.println( "What is the path separator? " +
File.pathSeparatorChar);
System.out.println( "What is the name separator? " +
File.separatorChar);



}
}
哪位达人帮小弟看一下这个程序,为什么创建不也不了文件呢,不胜感激啊


[解决办法]
new File并不是在硬盘上创建文件,你可以调用create方法来创建,或者当你调用FileOutputStream时系统也会创建
[解决办法]
同意楼上
new File是在内存里创建的
[解决办法]
同意,new File()用来获取文件信息,不能创建新文件
[解决办法]
new File()用来创建一个文件对象,这个时候只是在内存中建立了文件信息,需要将文件对象转换为输出流输出到文件。

[解决办法]
public class File
extends Object
implements Serializable, Comparable <File>

An abstract representation of file and directory pathnames.
[解决办法]
现在你只是创建了一个FILE对像.并没有创建文件.如果把你的程序改成这样File file = new File( "D:\\JAVA\\eclipse\\工作区\\简单输入输出 ");还可以创建目录呢.
[解决办法]
hoho,继续努力
[解决办法]
File f....
f.createNewFile();//创建文件
f.mkdirs();//创建目录
[解决办法]
...

File file = new File( "fileNamePath ");
file.createNewFile();//创文件
file.mkdirs;//创目录

...

[解决办法]
f.createNewFile();//创建文件
会抛出异常

读书人网 >J2SE开发

热点推荐