深的东西以后在数现在就说说怎么用java的这个功能:
其实这个功能说白了就是为了java可以对文件读或写进行操作而添加的。
他的头文件是java.io.*;。。。这里面有很多类,这次要用到两大类的类,Inputstream类和
Outputstream类,以及他们的子类。
再开始之前我们要知道一个file类。。。。它的功能说白了就是获得文件的地址。。(太直白了好像就是这么回事)。他的一些方法方法:
File file =new File (String 文件地址,String 文件名);//硬性规定下的文件
File file =new File (String 文件地址);// 成为目录
File file = new File (String 文件名);//当前目录下的文件
File file =new File (File 目录,String 文件名);//制定目录下的文件
boolean createNewFile(); 不存在返回true 存在返回false
boolean mkdir(); //创建目录
boolean mkdirs();//创建多级目录
boolean delete();//删除
boolean deleteOnExit(); //文件使用完成后删除
boolean canExecute();//判断文件是否可执行
boolean canRead();//判断文件是否可读
boolean canWrite();// 判断文件是否可写
boolean exists(); //判断文件是否存在
boolean isDirectory(); //目录
boolean isFile();
boolean isHidden();//隐藏
boolean isAbsolute();//判断是否是绝对路径 文件不存在也能判断
String getName();
String getPath();//路径
String getAbsolutePath();
String getParent();//如果没有父目录返回null
long lastModified();//获取最后一次修改的时间
long length();
boolean renameTo(File f);
File[] liseRoots();//获取机器盘符
File[] list()//;所有文件
String[] list();//以字符串的形式返回
File[] list(FilenameFilter filter) ;//返回目录下啊的所有文件以文件类
String[] list(FilenameFilter filter);//。。。。。以字符串
注意最后一个方法:里面有一个FilenameFilter接口(这个是个接口!)其中有一个accpet(File 调用它的文件类,String 选中的文件名)方法 当list()方法被调用时,FilenameFilter接口里的accpet被不停的调用判断是不是制定后缀名的文件需要重写这个方法:
file.list (new filenameFilter(){
String s=".java";
public accpect(File air, String name){
return name.endWith(s);
}
});
插曲:运行可执行文件: 类:Runtime 包:java.lang.*;---创建方法Runtime(); 打开方法 exec(String 地址);例子
/*
Runtime ru ;
ru=Runtime.getRuntime();
ru.exec("sss.exe");*/
到正题了:
先介绍文件字节流:FileInputStream, FIleOutputStream :字节流主要是操作byte类型数据,也byte数组为准 他的一些方法方法::
FIleOutputstream方法:
public FileOutputStream(File file) throws FileNotFoundException ;//构造方法覆盖文件内容哦
public FileOutputStream(String file) throws FileNotFoundException ;//构造方法覆盖文件内容哦
FileOutputStream(File file, boolean append) ;//构造方法在文件中追加内容更
public void close() throws IOException ;//关闭输出流
public void flush() throws IOException ;//刷新缓冲区
public void write(byte[] b) throws IOException ;//将一个byte数组写入数据流
public void write(byte[] b,int off,int len)throws IOException;// 将一个指定范围的byte数组写入数据流
public abstract void write(int b) throws IOException ;//将一个字节数据写入数据流
FIleInputstream 方法:
public FileInputStream(File file) throws FileNotFoundException ;//构造方法覆盖文件内容哦
public FileInputStream(String file) throws FileNotFoundException ;//构造方法覆盖文件内容哦
public int available() throws IOException ;//可以取得输入文件的大小
public void close() throws IOException ;//关闭输入流
public abstract int read() throws IOException;// 读取内容,以数字的方式读取
public int read(byte[] b) throws IOException;// 将内容读到byte数组之中,同时返回个数
public int read(byte[] b,int off,int len) throws IOException;//将一个指定范围的byte数组写入数据流
然后文件字符流::
在程序中一个字符等于2个字节 (byte 1 个字节);
类 filewriter filereader
Filewriter方法:
public FileWriter(File file) throws IOException;//构造方法
public FileWriter(String file) throws IOException;//构造方法
public abstract void close() throws IOException ;//关闭输出流
public void write(String str) throws IOException ;//将字符串输出
public void write(char[] cbuf) throws IOException;// 将字符数组输出
public void write(char[] cbuf,int off ,int len) throws IOException;// 将字符数组输出从指定位置
public abstract void flush() throws IOException ;//强制性清空缓存
Filereader方法:
public FileReader(File file) throws IOException;//构造方法
public FileReader(String file) throws IOException;//构造方法
public abstract void close() throws IOException;// 关闭输出流
public int read() throws IOException;// 读取单个字符
public int read(char[] cbuf) throws IOException;// 将内容读到字符串数组中,返回读入的长度
public int read(char[] cbuf,int off ,int len) throws IOException;// 将内容读到字符串数组中,返回读入的长度,从指定位置
然后缓冲流:
为文件字符流提供更强大的功能:类butteredReaderButteredwriter
ButtueredReader:方法:
Butteredwriter 方法;:
Butterewiter(Filereader x);//构造方法
public String readline();//读取一行
public abstract void close() throws IOException ;
Butteredreader方法:
Butteredwrited (Filewriter x);
public write(String s ,int off ,int len );//读取
public abstract void close() throws IOException ;//关闭流
随机流: 类randomacccessfile: ---不是java.io中的类,也不是inputsteam outputstream中的类;
方法:
RandomAccessFile(String name,String mode);//莫得定义r(只读)w(只写)rw(读写),name文件名
RandomAccessFile(File name,String mode);//莫得定义r(只读)w(只写)rw(读写),name文件名
数组流:
::读取内存
类 ByteArrayInputStreamByteArrayoutputStream
方法
ByteArrayInputStream方法:
ByteArrayInputStream(byte[] a,int off ,int len);//构造方法从off处读取len个字节单元
ByteArrayIntputStream(byte[] a);//构造方法
public int read();//读
public int read(byte[],int off,int len);//从指定位置
ByteArrayoutputStream方法:
ByteArrayOutputStream();//构造方法
ByteArrayOutputStream(int size);//指定大小的构造方法
public void write(int b);
public void write(byte [] ,int off ,int len);//写入len个字节
数据流::
不用担心有多少字节,与机器无关,
DataInputStream DataoutputStream
构造方法:
DataInputStream (InputStream a);//构造方法
DataOutputStream (InputStream a);//构造方法
例子
import java.io.*;
public class T1 {
public static void main(String[] args) {
String[] name = {"Tom","Jack","Mary"};
double[] salary = {300, 400, 1200};
try{
FileOutputStream fos = new FileOutputStream("res/salary.txt");
DataOutputStream dos = new DataOutputStream(fos);
for(int i=0; i<name.length; i++){
dos.writeUTF(name[i]);
dos.writeDouble(salary[i]);
}
dos.close();
fos.close();
///
FileInputStream fin = new FileInputStream("res/salary.txt");
DataInputStream dis = new DataInputStream(fin);
for(int i=0; i<name.length; i++){
String n = dis.readUTF();
double s = dis.readDouble();
System.out.println(n + " :" + s);
}
dis.close();
fin.close();
}catch(Exception e){
e.printStackTrace();
}
对象流:
使用前要进行序列化,,,,为了方便读写:: ***[ 通过这个可以进行文件的深度拷贝(clone方法也可以拷呗,但是不能拷贝引用成员变量)]
//注意实现接口
ObjectInputStream(InputStream x);//构造方法
readOject(Object x);//写入对象
ObjectOutputStream(InputStream x);//构造方法
writerObject(Object x);//写出对象
Scanner类
Scanner类读文件是以空白为分隔符(回车 空格 ; )
方法
canner(File source) //构造一个新的Scanner,它生成的值是从指定文件扫描的
Scanner(InputStream source) //构造一个新的 Scanner,它生成的值是从指定的输入流扫描的
Scanner(String source) //构造一个新的Scanner,它生成的值是从指定字符串 扫描的。
int nextInt(); //将输入信息的下一个标记扫描为一个 int
double nextDouble(); //将输入信息的下一个标记扫描为一个double
String nextNext(); //查找并返回来自此扫描器的下一个完整标记(字符串)
String nextLine(); //此扫描器执行当前行,并返回跳过的输入信息
boolean hasNext(); //如果此扫描器的输入中有另一个标记,则返回true
boolean hasNext(Pattern pattern); //如果下一个完整标记与指定模式匹配,则 返回true
boolean hasNext(String pattern); //如果下一个标记与从指定字符串构造的模式 匹配,则返回true
boolean hasNextInt(); //如果通过使用nextInt()方法,此扫描器输入信息中的下 一个标记可以解释为指定基数中的一个int 值,则返回true。
例子:
public static void main(String[] args) throws FileNotFoundException {
InputStream in = new FileInputStream(new File("C:\\AutoSubmit.java"));
Scanner s = new Scanner(in);
while(s.hasNextLine()){
System.out.println(s.nextLine());
}
}