File类
File位于java.io包中,用于操作文件及目录
(1)File类操作文件
--[1]boolean createNewFile() 创建文件
--[2]boolean exists() 判断file对象所指的磁盘上的文件是否存在
--[3]boolean delete()删除文件
--[4]String getPath()获取相对路径
--[5]String getAbsolutePath()获取绝对路径
--[6]String getName()获取文件名
--[7]boolean isFile()判断fi对象是否是文件
--[8]long length()获取文件中内容的字节数
package com.bjsxt.file;
import java.io.File;
import java.io.IOException;
public class TestFile {
public static void main(String[] args) throws IOException {
//创建File类的对象
File fi=new File("D:\\a.txt");
System.out.println("D:\\a.txt在磁盘上是否存在?"+fi.exists());
/**创建a.txt文件
* 如果a.txt在磁盘上不存在,则创建,如果存在,返回值为flase*/
System.out.println(fi.createNewFile()); //创建
System.out.println("绝对路径:"+fi.getAbsolutePath());
System.out.println("相对路径:"+fi.getPath());
System.out.println("文件名称:"+fi.getName());
System.out.println("父级目录"+fi.getParent());
System.out.println("是否是文件:"+fi.isFile());
//调用toString()方法时,实际上是在执行getPath()
System.out.println(fi.toString());
System.out.println("文件中字节的个数"+fi.length());//一个英文一个字节,一个汉字两个字节
System.out.println(fi.delete());
//创建文件对象
System.out.println(); //直接从磁盘上删除
/*File file =new File(fi.getParent(),"b.txt");
//File file=new File("D:/b.txt");
System.out.println(file.createNewFile());*/
System.out.println();
}
}
(2)File用于操作目录
--[1]mkdir()
--[2]mkdirs()
--[3]String [] list()
--[4]File []listFiles()
--[5]boolean delete()删除空目录
package com.bjsxt.file;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
public class TestDirectory {
public static void main(String[] args) throws IOException {
//创建File对象
File f=new File("d:\\180226");
/**创建目录*/
f.mkdir();//创建单层目录,父目录需存在
//f.mkdirs(); 创建多层目录(无论父目录是否存)
System.out.println("判断是否是目录:"+f.isDirectory());
System.out.println("判断是否是文件:"+f.isFile());
System.out.println(f.getAbsolutePath());
System.out.println(f.getPath());
System.out.println(f.getName());
System.out.println(f);
System.out.println(f.getParent());
//System.out.println(f.delete());//要求必须 是空目录
String [] str=f.list();//获取指定目录下的所有File对象的中的getName()String形式
System.out.println(str.length); //16个对象 String
for (String s : str) {
System.out.println(s);
}
File []file=f.listFiles();
System.out.println(file.length); //16个对象 File对象
for (File fi : file) {
System.out.println(fi);
}
}
}
案例:查看指定目录下的所有的文件(子目录下的文件)
package com.bjsxt.file;
import java.io.File;
public class Test {
public static void main(String[] args) {
File file=new File("E:\\010规范化\\03.新班开学第一天下发资料");
//调用方法
showFiles(file);
}
public static void showFiles(File file){
//(1)获取指定目录下的所有的File对象
File []files=file.listFiles();
//(2)遍历每一个File对象
for(int i=0;i<files.length;i++){
//(3)判断是否是目录
if (files[i].isFile()) {//结束的条件
System.out.println(files[i].getPath()); //结束时的操作
}else{
//自己调用自己,
showFiles(files[i]);
}
}
}
}
IO流
(1)字节输入流InputStream
数据源是文件-->FileInputStream
使用步骤:
(1)搭建桥梁 new 对象
(2)读数据 read()
(3)关闭close()
package com.bjsxt.file;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
public class TestInputStream {
public static void main(String[] args) throws IOException {
//(1)在数据源与程序之间搭建桥梁
InputStream is=new FileInputStream("D:\\a.txt");
/*System.out.println(is.read());
System.out.println(is.read());
System.out.println(is.read());
System.out.println(is.read()); //到达文件的末尾 -1
*/
//重复的事情使用循环 ,while ,循环次数不固定
int temp=0;//声明变量,用于存储读取的字节
while((temp=is.read())!=-1){
System.out.print((char)temp);
}
is.close();//关闭流
}
}
使用缓存byte数组存储 ,循环的次数,取决于byte数组的大小
package com.bjsxt.file;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
public class TestInputStream2 {
public static void main(String[] args) throws IOException {
//(1)创建对象,搭建桥梁
InputStream is=new FileInputStream("D:\\a.txt");
//(2)读数据
byte [] buf=new byte[1024]; //创建了一个数组, buffer array
//int length=is.read(buf); //length读到了字节的个数
/**遍历数组
for (byte b : buf) {
System.out.println(b);
}*/
//借助String类的构造方法
//String str=new String(buf,0,length);
//System.out.println(str);
int length=0;//声明变是,用于存储读到的字节的长度
while((length=is.read(buf))!=-1){
System.out.print(new String(buf,0,length));
}
//关闭流
is.close();
}
}
数据源是标准的输入设备(键盘) System.in
package com.bjsxt.file;
import java.io.IOException;
import java.io.InputStream;
public class TestSystemIN {
public static void main(String[] args) throws IOException {
//(1)搭建桥梁,程序与键盘之间搭建桥梁
//InputStream is=new FileInputStream("D:\\a.txt"); 等号的右侧,说明数据源是文件
InputStream is=System.in; //等号右侧 ,说明数据源是键盘
//(2)读数据
System.out.println((char)is.read());
//(3)关闭流
is.close();
}
}
数据源来源是网络 URL (统一资源定位器/符 ) http://www.baidu.com
package com.bjsxt.file;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
public class TestNet {
public static void main(String[] args) throws IOException {
//InputStream is=System.in;
//InputStream is=new FileInputStream("D:\\a.txt");
//数据源是网络
URL u=new URL("http://www.baidu.com");
InputStream is= u.openStream();
/*int temp=0;
while((temp=is.read())!=-1){
System.out.print((char)temp);
}*/
//读到byte的缓冲数组中
byte [] buf=new byte[1024];
int length=0;//用于存储读到的字节的个数
while((length=is.read(buf))!=-1){
System.out.println(new String(buf,0,length));
}
is.close();
}
}
字节输出流 OutputStream
-->目的是文件 FileOutputStream
package com.bjsxt.out;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class TestFileOutputStream {
public static void main(String[] args) throws IOException {
//(1)搭桥
OutputStream os=new FileOutputStream("D:\\b.txt");
//(2)写数据 写到目的地
os.write('a'); //char-->int
os.write(97);
//(3)关闭流
os.close();
}
}
将缓冲数组的数据,一次性“写”到目的
package com.bjsxt.out;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class TestFileOutputStream2 {
public static void main(String[] args) throws IOException {
//(1)搭,第二个参数为true,表示在源有文件内容的基础上追加内容,如果为false则表示对源有内容进行覆盖,默认为false
//OutputStream os=new FileOutputStream("d:\\b.txt",true);
OutputStream os=new FileOutputStream("d:\\b.txt");
//(2)写
//byte [] buf={97,98,99,100};
byte [] buf="helloworld".getBytes();
os.write(buf);
//(3)关
os.close();
}
}
--目的是标准的输出设备 “显示器 " PrintStream (字节打印流) System.out
package com.bjsxt.out;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
public class TestSystemOut {
public static void main(String[] args) throws IOException {
//OutputStream os=new FileOutputStream("D:\\b.txt");
OutputStream os=System.out;//说明目的是“显示器”
os.write(97); //写
os.write("helloworld".getBytes());
os.close();
PrintStream ps=System.out;
ps.println("\n");
ps.println("hello");
ps.println(true);
ps.write(97);
//ps相当与下列代码中的System.out是同一个含义,可以等量替换
System.out.println("\n");
System.out.println("hello");
ps.close();
}
}
案例:文件的拷贝
package com.bjsxt.out;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class Copy {
public static void main(String[] args) throws IOException {
//(1)数据源与程序搭桥
InputStream is=new FileInputStream("D:\\star.jpg");
//(2)程序与目的地搭桥
OutputStream os=new FileOutputStream("E:\\star2.bmp");
//(3)边读边写
int temp=0;//用于存储读到了byte值
while((temp=is.read())!=-1){
os.write(temp);
}
//(4)关闭流
os.close();
is.close();
}
}
IO流的分类
(1)按照流向
输入流 InputStream ,Reader
输出流 OutputStream ,Writer
(2)处理单元
字节流 InputStream, OutputStream
字符流 Reader, Writer
字符流
-->字符输入流 Reader
-->数据源是文件 FileReader
int read() 读到是0-65535之间unicode值, 文件尾为-1
package com.bjsxt.reader;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
public class TestFileRreader {
public static void main(String[] args) throws IOException {
//(1)搭桥
Reader r=new FileReader("D:\\a.txt");
//(2)读
int temp=0;//读到了字符的unicode码值
while((temp=r.read())!=-1){
System.out.println(temp+"--->"+(char)temp);
}
//(3)关
r.close();
}
}
读到缓冲数组中, int read(char [] ch) 读到的字符的个数, 文件尾为-1
package com.bjsxt.reader;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
public class TestFileReader2 {
public static void main(String[] args) throws IOException {
//(1)搭桥
Reader r=new FileReader("D:\\a.txt");
//(2)
char [] ch=new char[1024];
int length=0;//字符的个数
while((length=r.read(ch))!=-1){
//借助String的构造方法
System.out.println(new String(ch,0,length));
}
//(3)关
r.close();
}
}
字符的输出流
Writer
-->目地的是文件FileWriter
package com.bjsxt.writer;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
public class TestFileWriter {
public static void main(String[] args) throws IOException {
//(1)搭桥
Writer w=new FileWriter("d:\\a.txt");
//(2)写
w.write("还有两天清明放假,请问你去哪?");
//(3)关
w.close();
}
}
(1)File类
(2)字节输入流 (数据源) read() ,read(byte [] b)
(3)字节输出流 (目的地) write(int b) ,write(byte [] b)
(4)字符输入流 (数据源) read() ,read(char []ch)
(5)字符输出流 (目的地) write(String s)