File 类小结
File 类
一:
File 类的对象主要用来获取文件本身的一些信息,例如文件所在的目录,文件的长度,文件读写权限等,不涉及对文件的读写操作.创建一个 File 对象的构造方法有 3 个:
File (File file String filename);
File (String filename);
File (String directoryPate);
使用 File (String filename)创建文件时,该文h) //构造方法
File (String directoryPath,String filenam)
System.out.println(" 请输入您要查询的目录 ");
String s = reader.nextline();
System.out.println("请输入您要查询文件的名字 ");
String m = reader.nextline();
File file = new File(s,m);
经常使用File类的下列方法获取文件本身的一些信息
public boolean exixts() 判断文件是否存在 .
public String getName()获取文件的名字 .
public boolean canRead()判断文件是否是可读的 .
public boolean canWrite()判断文件是否可被写入 .
public long length()获取文件的长度(单位是字节 ) .
public String getAbsolutePath()获取文件的绝对路径 .
public String getParent()获取文件的父目录 .
public boolean isFile()判断文件是否是一个正常文件,而不是目录 .
public boolean isDirectroy()判断文件是否是一个目录 .
public boolean isHidden()判断文件是否是隐藏文件 .
public long lastModified() 获取文件最后修改的时间时间是从1970 年午夜至文件最后修改时刻的毫秒数 .
二:
1 ) 创建目录
public boolean mkdir()
创建一个目录,如果创建成功返回true,否则返回false(如果该目录已经存在将返回 false).
创建目录,必须有File 的对象
File file = new File("f://300//1.java");
boolean a = file.mkdir();
if(a == true){
System.out.println(" 创建成功 ");
}
else
{
System.out.println("失败 ");
}
2)列出目录中的文件
如果File 对象是一个目录,那么该对象可以调用下述方法列出该目录下的文件和子目录
public String[] list() 用字符串形式返回目录下的全部文件 ,
public File [] listFiles()用File对象形式返回目录下的全部文件 .
File file = new File("目录 ");
1. String fileName[] = file.list[file.length()];
2. File fileName[] = file.listFiles();
然后用数组循环输出
列出目录下指定类型的文件:
public String[] list(FilenameFilter obj)
该方法用字符串形式返回目录下的指定类型的所有文件 .
public File [] listFiles(FilenameFilter obj)
该方法用File 对象返回目录下的指定类型所有文件 .
FilenameFilter 是一个接口,该接口有一个方法
public boolean accept(File dir,String name)
当向list方法传递一个实现该接口的对象时,dir调用list方法在列出文件时,将调用accept方法检查该文件name是否符合accept方法指定的目录和文件名字要求 .
在下面的例子2中,列出d:/ioio目录下的部分.java文件的名字 .
class FileAccept implements FilenameFilter{
String str = null;//定义变量后 就给赋值
FileAccept(String s){
str = "."+s;
}
public boolean accept(File dir,String name){
return name.endsWith(str);
}
}
例子:
System.out.println("==== 请输入您要查询文件的目录 ====");
Scanner reader1 = new Scanner(System.in);
String s = reader1.nextLine();
System.out.println("====请输入文件的类型 ====");
Scanner reader2 = new Scanner(System.in);
String r = reader2.nextLine();
File dir = new File(s+":");
FileAccept acceptCondition = new FileAccept(r);
File fileName[] = dir.listFiles(acceptCondition);
for(int i=0;i<fileName.length;i++){
System.out.println(fileName.getPath());
}
FileInputStream 类
这个是查看和读写文档内容的类 上面的File类 是文件夹属性
FileInputStream 和 FileOutStream
例子:
package file;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class SimpleReadFile {
public static void main(String[] aa){
int b = 0;
File f = new File("E://temp//www.wnetw.net.java");
byte tom[]=new byte[(int)f.length()];
try{
FileInputStream readfile=new FileInputStream(f);
while( ( b=readfile.read(tom,0,(int)f.length()) ) !=-1 ){
String s = new String (tom,0,b);// 把byte类型 转换成 String
System.out.println(s);
//System.out.println(b);
}
readfile.close();
}
catch(IOException e){
System.out.println("文件错误 "+e.getMessage());
}
}
}
FileOutStream类型
例子:
import java.util.*;
import java.io.*;
public class OutStream {
public static void main(String args[]){
try{
System.out.println(" 请输入您要写入的内容,并存入磁盘 ");
String s = reader.nextLine();
byte tom [] = s.getBytes();//把字符串 强制转换成byte类型;并传值给了 byte
File file = new File("文件目录 ");
FileOutputStream writefile = new FileOutputStream(file);
writefile.write(tom,0,tom.length);
System.out.println("写入成功 ");
writefile.close();
}
catch (IOException e){
System.out.println("Error");
}
}
}
FileReader类和FileWriter类
上面的FileInputStream使用字节读取文件,字节流不能直接操作Unicode字符,所以Java 提供了字符流.由于汉字在文件中占用2 个字节,如果使用字节流,读取不当会出现乱码现象,采用字符流就可以避免这个现象,因为,在Unicode 字符中,一个汉字被看作一个字符 .
1. FileReader
package file;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class SimpleFileReader {
public static void main(String aa[]){
int n = 0;
try{
File f = new File("E://temp","www.wnetw.net.java");
FileReader in = new FileReader(f);
int length = (int) f.length();
char tom[] = new char[length]; //byte tom [] = new byte [length]
while ((n = in.read(tom, 0, length)) != -1) {
String s = new String(tom, 0, n);//把字符类型 转换成 String
System.out.println(s);
}
in.close();
}
catch(IOException e){
System.out.println("error:"+e.getMessage());
}
}
}
一:
File 类的对象主要用来获取文件本身的一些信息,例如文件所在的目录,文件的长度,文件读写权限等,不涉及对文件的读写操作.创建一个 File 对象的构造方法有 3 个:
File (File file String filename);
File (String filename);
File (String directoryPate);
使用 File (String filename)创建文件时,该文h) //构造方法
File (String directoryPath,String filenam)
System.out.println(" 请输入您要查询的目录 ");
String s = reader.nextline();
System.out.println("请输入您要查询文件的名字 ");
String m = reader.nextline();
File file = new File(s,m);
经常使用File类的下列方法获取文件本身的一些信息
public boolean exixts() 判断文件是否存在 .
public String getName()获取文件的名字 .
public boolean canRead()判断文件是否是可读的 .
public boolean canWrite()判断文件是否可被写入 .
public long length()获取文件的长度(单位是字节 ) .
public String getAbsolutePath()获取文件的绝对路径 .
public String getParent()获取文件的父目录 .
public boolean isFile()判断文件是否是一个正常文件,而不是目录 .
public boolean isDirectroy()判断文件是否是一个目录 .
public boolean isHidden()判断文件是否是隐藏文件 .
public long lastModified() 获取文件最后修改的时间时间是从1970 年午夜至文件最后修改时刻的毫秒数 .
二:
1 ) 创建目录
public boolean mkdir()
创建一个目录,如果创建成功返回true,否则返回false(如果该目录已经存在将返回 false).
创建目录,必须有File 的对象
File file = new File("f://300//1.java");
boolean a = file.mkdir();
if(a == true){
System.out.println(" 创建成功 ");
}
else
{
System.out.println("失败 ");
}
2)列出目录中的文件
如果File 对象是一个目录,那么该对象可以调用下述方法列出该目录下的文件和子目录
public String[] list() 用字符串形式返回目录下的全部文件 ,
public File [] listFiles()用File对象形式返回目录下的全部文件 .
File file = new File("目录 ");
1. String fileName[] = file.list[file.length()];
2. File fileName[] = file.listFiles();
然后用数组循环输出
列出目录下指定类型的文件:
public String[] list(FilenameFilter obj)
该方法用字符串形式返回目录下的指定类型的所有文件 .
public File [] listFiles(FilenameFilter obj)
该方法用File 对象返回目录下的指定类型所有文件 .
FilenameFilter 是一个接口,该接口有一个方法
public boolean accept(File dir,String name)
当向list方法传递一个实现该接口的对象时,dir调用list方法在列出文件时,将调用accept方法检查该文件name是否符合accept方法指定的目录和文件名字要求 .
在下面的例子2中,列出d:/ioio目录下的部分.java文件的名字 .
class FileAccept implements FilenameFilter{
String str = null;//定义变量后 就给赋值
FileAccept(String s){
str = "."+s;
}
public boolean accept(File dir,String name){
return name.endsWith(str);
}
}
例子:
System.out.println("==== 请输入您要查询文件的目录 ====");
Scanner reader1 = new Scanner(System.in);
String s = reader1.nextLine();
System.out.println("====请输入文件的类型 ====");
Scanner reader2 = new Scanner(System.in);
String r = reader2.nextLine();
File dir = new File(s+":");
FileAccept acceptCondition = new FileAccept(r);
File fileName[] = dir.listFiles(acceptCondition);
for(int i=0;i<fileName.length;i++){
System.out.println(fileName.getPath());
}
FileInputStream 类
这个是查看和读写文档内容的类 上面的File类 是文件夹属性
FileInputStream 和 FileOutStream
例子:
package file;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class SimpleReadFile {
public static void main(String[] aa){
int b = 0;
File f = new File("E://temp//www.wnetw.net.java");
byte tom[]=new byte[(int)f.length()];
try{
FileInputStream readfile=new FileInputStream(f);
while( ( b=readfile.read(tom,0,(int)f.length()) ) !=-1 ){
String s = new String (tom,0,b);// 把byte类型 转换成 String
System.out.println(s);
//System.out.println(b);
}
readfile.close();
}
catch(IOException e){
System.out.println("文件错误 "+e.getMessage());
}
}
}
FileOutStream类型
例子:
import java.util.*;
import java.io.*;
public class OutStream {
public static void main(String args[]){
try{
System.out.println(" 请输入您要写入的内容,并存入磁盘 ");
String s = reader.nextLine();
byte tom [] = s.getBytes();//把字符串 强制转换成byte类型;并传值给了 byte
File file = new File("文件目录 ");
FileOutputStream writefile = new FileOutputStream(file);
writefile.write(tom,0,tom.length);
System.out.println("写入成功 ");
writefile.close();
}
catch (IOException e){
System.out.println("Error");
}
}
}
FileReader类和FileWriter类
上面的FileInputStream使用字节读取文件,字节流不能直接操作Unicode字符,所以Java 提供了字符流.由于汉字在文件中占用2 个字节,如果使用字节流,读取不当会出现乱码现象,采用字符流就可以避免这个现象,因为,在Unicode 字符中,一个汉字被看作一个字符 .
1. FileReader
package file;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class SimpleFileReader {
public static void main(String aa[]){
int n = 0;
try{
File f = new File("E://temp","www.wnetw.net.java");
FileReader in = new FileReader(f);
int length = (int) f.length();
char tom[] = new char[length]; //byte tom [] = new byte [length]
while ((n = in.read(tom, 0, length)) != -1) {
String s = new String(tom, 0, n);//把字符类型 转换成 String
System.out.println(s);
}
in.close();
}
catch(IOException e){
System.out.println("error:"+e.getMessage());
}
}
}