1.目录
- java.io.file类的使用
- io原理及流的分类
- 节点流
- 缓冲流
- 转换流
- 标准输入输出
- 打印流
- 数据流
- 对象流
- 随机存取文件
- NIO中的path,paths,file类的使用。
1. java.io.file类的使用
public class File
extends Object
implements Serializable, Comparable<File>
- 文件和目录的抽象表示形式,与平台无关
- file能新建,删除,重命名文件和目录,但是File不能访问文件的内容。
- 如果需要访问文件的内容,这时候就需要使用到输入,输出流。
- 路径名:
File file=new File("E:\\WorkSpace\\java\\Practice\\io\\hello.txt");
// 当然可以直接使用
File file=new File("E:/WorkSpace/java/Practice/io/hello.txt");
// 原因是在windows系统中,\是转义的意思
- 绝对路径:带盘符的:
E:\\WorkSpace
- 相对路径:相对于当前的工程来说的,
2. 字符流
- reader以及writer(抽象基类),一般处理的是“.txt”文件的,也属于解码
3. 字节流
- inputStream,OutputStream.(抽象基类)
实现类
- FileInputStream,FileOutputStream 属于文件流,也是字节流。
- BufferedInputStream,BufferedOutPutStream,处理流的一种,提高数据传输的速度。
! - 转换流
public class FileTest {
public static void main(String[] args) {
try {
File file = new File("hello.txt");
createFixLengthFile(file, 2147483333);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void textFileInputStream1() {
FileInputStream fileInputStream = null;
File file = new File("hell.txt");
try {
fileInputStream = new FileInputStream(file);
byte[] buffer = new byte[5];
int len;
while ((len = fileInputStream.read(buffer)) != -1) {
String str = new String(buffer, 0, len);
System.out.print(str);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fileInputStream != null) {
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void testFileOutputStream() {
File file = new File("abc.txt");
FileOutputStream fos = null;
byte[] buffer = "i love China i love Beijing".getBytes();
try {
fos = new FileOutputStream(file);
fos.write(buffer);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void testFileInputAndFileoutPut() {
File src = new File("hello.txt");
File dec = new File("hello1.txt");
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(src);
fos = new FileOutputStream(dec);
byte[] buffer = new byte[5];
int len;
while ((len = fis.read(buffer)) != -1) {
fos.write(buffer, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (fos != null) {
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void createFixLengthFile(File file, long length) throws IOException {
long start = System.currentTimeMillis();
FileOutputStream fos = null;
FileChannel output = null;
fos = new FileOutputStream(file);
output = fos.getChannel();
output.write(ByteBuffer.allocate(1), length - 1);
if (output != null) {
output.close();
}
if (fos != null) {
fos.close();
}
long end = System.currentTimeMillis();
System.out.println("total times" + (end - start));
}
public static void create(File file, long length) throws IOException {
long start = System.currentTimeMillis();
RandomAccessFile r = null;
try {
r = new RandomAccessFile(file, "rw");
r.setLength(length);
} finally {
if (r != null) {
try {
r.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
long end = System.currentTimeMillis();
System.out.println(end - start);
}
}
public class BufferedTest {
public static void main(String[] args){
}
public static void testBuffered() throws IOException {
File file=new File("abc.txt");
File dext=new File("hello.txt");
FileInputStream fis=null;
FileOutputStream fos=null;
fis=new FileInputStream(file);
fos=new FileOutputStream(dext);
BufferedInputStream bis=new BufferedInputStream(fis);
BufferedOutputStream bos=new BufferedOutputStream(fos);
byte[] buffer=new byte[1024];
int len;
while ((len=bis.read())!=-1){
bos.write(buffer,0,len);
}
bos.close();
bis.close();
fos.close();
fis.close();
}
}
参考文献
- aliyun