目录
1.I/O流的概念
2.file类
2.1file的常用方法
3.字节流与字符流
———————————————————————————————————————————
1.I/O流的概念
I/O流就是以流的方式进行输入,输出
2.file类
是文件和目录路径名的抽象表示形式
2.1file的常用方法
file.mkdir() 创建目录
file.mkdirs() 创建多个目录
3.字节流与字符流
字节输入流
InputStream
字节输出流
OutputStream
实例
package IO.textstream;
import java.io.*;
public class streamText {
public static void main(String[] args) {
//字节输入流
InputStream inputStream = null;
//字节输出流
OutputStream outputStream = null;
try {
inputStream = new FileInputStream("D:\\abc.txt");
outputStream = new FileOutputStream("D:a.txt");
//通过流获取文件长度
byte[] bytes = new byte[inputStream.available()];
//读取
inputStream.read(bytes);
for (int i = 0; i < bytes.length; i++) {
System.out.println(bytes[i]);
}
outputStream.write(bytes);
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}finally {
try {
//后来先关
outputStream.close();
inputStream.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
带缓冲区的输入流
package IO.textbufferedsttream;
import java.io.*;
public class bufferedStreamText {
public static void main(String[] args) {
//字节输入流
InputStream inputStream = null;
BufferedInputStream bufferedInputStream = null;
//字节输出流
OutputStream outputStream = null;
BufferedOutputStream bufferedOutputStream = null;
try {
inputStream = new FileInputStream("D:\\截图");
bufferedInputStream = new BufferedInputStream(inputStream);
outputStream = new FileOutputStream("D:\\a");
bufferedOutputStream = new BufferedOutputStream(outputStream);
//获取系统当前时间
long l = System.currentTimeMillis();
//通过流获取文件长度
//分批次复制文件
//最佳数组长度需要多次进行测试得到
byte[] bytes = new byte[1024];
//读取
int read = bufferedInputStream.read(bytes);
//判断是否有读回来的内容 -1表示没有了
while (read != -1){
//每次写入时只写入读回来的长度
bufferedOutputStream.write(bytes,0,read);
//写完一批在读一批,在此进入循环写入
read = bufferedInputStream.read(bytes);
}
long l1 = System.currentTimeMillis();
System.out.println("复制文件所需要的时间为"+(l-l1));
bufferedOutputStream.flush(); //刷新缓冲区
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}finally {
try {
if (bufferedOutputStream != null)
bufferedOutputStream.close();
if (outputStream != null)
outputStream.close();
if (bufferedInputStream != null)
bufferedOutputStream.close();
if (inputStream != null)
inputStream.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
字符输入流 Reader 字符输出流 Writer
带缓冲区的字符输入流 BufferedReader 带缓冲区的字符输出流 BufferedWriter
package IO.textreader;
import java.io.*;
public class readerText {
public static void main(String[] args) {
//字符输入流
Reader reader = null;
BufferedReader bufferedReader = null;
//字符输出流
Writer writer = null;
BufferedWriter bufferedWriter = null;
try {
reader = new FileReader("D:\\abc.txt");
bufferedReader = new BufferedReader(reader);
writer = new FileWriter("D:\\a.txt");
bufferedWriter = new BufferedWriter(writer);
char[] chars = new char[11];
bufferedReader.read(chars);
for (char c:chars) {
System.out.println(c);
}
bufferedWriter.write(chars);
bufferedWriter.flush();//刷新
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}finally {
try {
if (bufferedWriter != null)
bufferedWriter.close();
if (writer != null)
writer.close();
if (bufferedReader != null)
bufferedReader.close();
if (reader != null)
reader.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}