java io流,
java io流,
一、file类
1、概念:java中把文件或者目录(文件夹)都封装成File对象。也就是说如果我们要去操作硬盘上的文件,或者文件夹只要找到File这个类即可。
2、file方法:
boolean exits 判断目录是否存在
boolean isFile 判断是否是文件
boolean isDirectory 判断是否存在目录
string getPath 返回相对路径名
string getAbsolutePath 返回绝对路径名
string getname 返回此对象指定的目录或文件
boolean delete 删除
boolean createTempFile 添加一个文件返回路径
3、使用实例
(1)查询所有的文件和目录
public class Demo3 {
public static void main(String[] args) {
// TODO Auto-generated method stub
File file =new File("F:\\java");
File[] files=file.listFiles(new f3());
for (File file2 : files) {
System.out.println(file2);
}
}
//自定义一个接口
public class f3 implements FileFilter{
//返回所有文件目录
@Override
public boolean accept(File pathname) {
// TODO Auto-generated method stub
return pathname.exists();
}
}
(2)file常用方法使用方法
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
File file=new File("F:\\shiyan");
System.out.println(file.exists());//判断是否存在文件或目录
System.out.println(file.isDirectory());//判断是否存在目录
System.out.println(file.isFile());//判断是否存在文件
System.out.println(file.getPath());//返回此对象保存的相对路径名
System.out.println(file.getAbsolutePath());//返回此对象的绝对路径名
//System.out.println(file.delete());//删除此对象指定的目录或文件夹
System.out.println(File.createTempFile("我喜欢你", ".txt").getPath());//创建文件,返回file路径
System.out.println(file.length());//返回文件长度
}
二、字节流
******************************注 意***********************************
关闭流的时候,采用先打开后关闭,后打开先关闭方法
**********************************************************************
1:字节输出流
OutputStream此抽象类,是表示输出字节流的所有类的超类。操作的数据都是字节,定义了输出字节流的基本共性功能方法
输出流中定义都是写write方法
close 关闭
flush 刷新
write 指定字节写入输出
2:FileOutputStream类
是OutputStream的一个子类,可以用来写入数据到文件
FileOutputStream类,即文件输出流,是用于将数据写入 File的输出流。
直接new FileOutputStream(file)这样创建对象,写入数据,会覆盖原有的文件
实例:
public class Demo1 {
public static void main(String[] args) throws IOException {
File file=new File("F:\\shiyan\\a.txt");
FileOutputStream fos=new FileOutputStream(file);
//写入
byte [] data="abcdefg".getBytes();
fos.write(data);
fos.close();
}
}
3:给文件中续写和换行
在FileOutputStream的构造函数中,可以接受一个boolean类型的值,如果值true,就会在文件末位继续添加。
实例:
public class Demo1 {
public static void main(String[] args) throws IOException {
Write("f://shiyan//c.txt");
}
private static void Write(String string) throws IOException {
// TODO Auto-generated method stub
//如果文件不存在,会自从创一个文件
FileOutputStream file=new FileOutputStream(string, false);//默认false 替换原有的内容 如果true则续写
String a="我喜欢你,喜欢你很久了,第一次见到你,我就深深的喜欢上了你!!!!!";
try {
file.write(a.getBytes());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("添加字符失败!");
}
file.close();
System.out.println("文件已经更新");
}
}
4:读取fileinputsteam
在读取文件中的数据时,调用read方法,实现从文件中读取数据
实例:
public class Demo2 {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
readtxt("F://shiyan//a.txt");
}
private static void readtxt(String string) throws IOException {
// TODO Auto-generated method stub
FileInputStream fis = null;
try {
fis = new FileInputStream(string);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
System.out.println("文件不存在!");
e.printStackTrace();
}
byte[] b=new byte[1024];
//可读取的字符数
int c=fis.available();
System.out.println("可读取的字符数为:"+c);
fis.read(b);
//打印
String a=new String(b);
System.out.println("文件内容为:"+a);
fis.close();
}
5:复制文件
采用fileinputsteam和fileoutputsteam
实例:
public static void main(String[] args) throws IOException {
//1,明确源和目的。
File srcFile = new File("c:\\YesDir\test.JPG");
File destFile = new File("copyTest.JPG");
//2,明确字节流 输入流和源相关联,输出流和目的关联。
FileInputStream fis = new FileInputStream(srcFile);
FileOutputStream fos = new FileOutputStream(destFile);
//3, 使用输入流的读取方法读取字节,并将字节写入到目的中。
int ch = 0;
while((ch=fis.read())!=-1){
fos.write(ch);
}
//4,关闭资源。
fos.close();
fis.close();
}
输入流和输出流之间是通过ch这个变量进行数据交换的。
采用这种方法效率比较低下,每次都从源文件读取一个,然后在写到指定文件,接着再读取一个字符,然后再写一个,一直这样下去。效率极低。
相关文章
- 暂无相关文章
用户点评