IO流
java.io.File类的应用
文件流 FileInputStream/FileOutputStream/FileReader/FileWriter
缓冲流 BufferedInputStream/BufferedOutputStream/BufferedReader/BufferedWriter
File类
File可以新建、删除、重命名文件和目录,但File不能访问文件内容本身,如果需要访问文件内容本身,需要使用输入/输出流
xxxxxxxxxx
import java.io.File;
public class Test {
public static void main(String[] args) {
File f = new File("路径"); //双斜杠
//注意,\在文件中是路径的分隔符,但在java编程中一个\的意思是转义符,在Java中\\或/才是文件的路径
File f1 = new File("路径1" + File.separator + "路径2");
System.out.println(f.getName()); //获取文件名,或者当前文件夹名
System.out.println(g.getPath()); //获取文件或者文件夹的(绝对或者相对)路径
System.out.println(g.getAbsolutePath()); //获取绝对路径
File f2= new File("src/dayxx/Test.java"); //相对路径
System.out.println(f2.getParent()); //返回一个父级路径
f.renameTo(new File("xx.新名字.txt"));
System.out.println(f2.exists()); //看文件存不存在
System.out.println(f2.canWrite()); //判断是否可写
System.out.println(f2.canRead()); //判断是否可读
System.out.println(f2.isFile());//判断是不是文件
System.out.println(f2.isDictionary());//文件夹
System.out.pritnlm(f2.lastModified());//文件最后的修改时间,返回一个毫秒数
System.out.pritnln(f2.length());//文件长度,字节数
//相关操作
File f3 = new File("路径");
if(!f3.exists()) { //判断文件是否存在
try {
f3.creatNewFile(); //创建一个新文件
} catch (IOException e) {
e.printStackTrace ();
}
}
f3.delete(); //删除
File f4 = new File("路径");
f4.mkdir(); //创建单层目录
File f5 = new File ("路径");
f5.mkdirs();//这个方法可以创建多级目录
File file = new File ("路径");
String[] fl = fll.list(); //返回的是当前文件夹的子集名字
for(String s : fl) {
System.out.println(s);
}
File [] fs = fll.listFiles(); //得到当前文件夹下面全部的对象
for(File ff : fs) {
System.out.println(ff);
}
}
}
IO原理
java程序中,对于数据的输入/输出操作以流的方式进行
流的分类
按操作数据单位不同分为:字节流,字符流 按数据流的流向不同分为:输入流,输出流 按流的角色不同分为:节点流,处理流
(抽象基类) | 字节流 | 字符流 |
---|---|---|
输入流 | InputStream | Reader |
输出流 | OutputStream | Writer |
java的io流一共有40多个类,都是从以上四个抽象基类中派生出来的
文件字节输入流
xxxxxxxxxx
public class Test {
public static void main(String[] args) {
try {
FileInputStream in = new FileInputStream("路径");
byte[] b = new byte [10]; //设置一个比特数组接受读取文件的内容
int len = 0; //设置一个读取数据的长度
//read函数有一个返回值:读取的数据的长度,如果读取完成了,还会向后读一个,返回值是-1,当read的返回值是-1时,文件读取完毕
while(len = n.read(b) != -1) {
System.out.println(new String(b, 0, len)); //参数一缓冲数据的数组,参数二从数组的哪个位置开始转化成字符串,参数三转换成字符串的长度
}
in.close(); //流在使用完毕之后一定要关闭
} catch (Exception e) {
e.printStackTrace ();
}
}
}
文件字节输出流
xxxxxxxxxx
public class Test {
public static void main(String[] args) {
try {
FileOutputStream out = new FileOutputStream("指定输出文件路径");
String str = "i hava an apple";
out.write (str.getBytes());//把数据写道内存中
out.flush();//把内存中的数据写道磁盘
out.close ();
} catch (Exception e) {
e.printStackTrace ();
}
}
}
字节流文件复制
xxxxxxxxxx
public static void main(String [] args) {
try {
FileInputStream in = new FileInputStream("x");
FileOutputStream out = new FileOutputStream("x");
byte[] b = new byte [100];j
int len = 0;
whlie(in.read(b) != -1) {
out.write (b, 0, len);
}
out.flush();
} catch (Exception e) {
e.printStackTrace ();
}
}
文件字符流
与文件字节流的差别:
- FileReader类
- 临时数组:char[] = new char[1024];
- 调用流对象的读取方法讲流中的数据读入到数组中 对象.read(ch);
缓冲流
为了提高数据读写的数据,java API提供了带缓冲功能的流类,在使用这些流类时,会创建一个内部缓冲区数组
根据数据操作单位可以将缓冲流分为:
- BufferedInputStream和BufferedOutputStream:针对FileInputStream和FileOutputStream作缓冲流
- BufferedReader和BufferedWriter:针对FileReader和FileWriter作缓冲流
xxxxxxxxxx
public class Test {
public static void main(String[] args) {
}
//缓冲字节输入流
public static void testBufferedInputStream() thorws Exception {
//文件字节输入流
FileInputStream in = new FileInputStream("");
//缓冲字节流输入流对象
BufferedInputStream br = new BufferedInputStream(in);
byte [] b = new byte [10];
int len = 0;
while((len = br.read(b)) != -1) {
System.out.println(new String(b, 0, len));
}
br.close();
in.close(); //新开的后关
}
}
Comments | NOTHING