您的位置:首页 > 其它

io流相关总结

2018-12-30 22:28 40 查看

1.目录

  1. java.io.file类的使用
  2. io原理及流的分类
  3. 节点流
  4. 缓冲流
  5. 转换流
  6. 标准输入输出
  7. 打印流
  8. 数据流
  9. 对象流
  10. 随机存取文件
  11. NIO中的path,paths,file类的使用。

1. java.io.file类的使用

public class File
extends Object
implements Serializable, Comparable<File>
  1. 文件和目录的抽象表示形式,与平台无关
  2. file能新建,删除,重命名文件和目录,但是File不能访问文件的内容。
  3. 如果需要访问文件的内容,这时候就需要使用到输入,输出流。
  4. 路径名:
File file=new File("E:\\WorkSpace\\java\\Practice\\io\\hello.txt");
// 当然可以直接使用
File file=new File("E:/WorkSpace/java/Practice/io/hello.txt");
// 原因是在windows系统中,\是转义的意思
  1. 绝对路径:带盘符的:
    E:\\WorkSpace
  2. 相对路径:相对于当前的工程来说的,

2. 字符流

  1. reader以及writer(抽象基类),一般处理的是“.txt”文件的,也属于解码

3. 字节流

  1. inputStream,OutputStream.(抽象基类)

实现类

  1. FileInputStream,FileOutputStream 属于文件流,也是字节流。
  2. BufferedInputStream,BufferedOutPutStream,处理流的一种,提高数据传输的速度。
    !
  3. 转换流
/**
* @Auther: linghaoDo
* @Date:Created at 2018/12/30
* @Description: 1. 流的分类
* 2. 流的流向
* 3. 操作的数据类型
* 4. 角色流(文件流) 处理流
* 5.
*/
public class FileTest {
//    File file=new File("E:\\WorkSpace\\java\\Practice\\io\\hello.txt");
public static void main(String[] args) {
//        FileTest.textFileInputStream1();
//        FileTest.testFileOutputStream();
try {
File file = new File("hello.txt");
createFixLengthFile(file, 2147483333);
} catch (IOException e) {
e.printStackTrace();
}

}

public static void textFileInputStream1() {
FileInputStream fileInputStream = null;
/*创建File类的对象*/
File file = new File("hell.txt");
/*2. 创建FileInputStream流的对象  拿到节点*/

try {
fileInputStream = new FileInputStream(file);
/*3. 读取数据的操作,这一步相当于指针的初始化*/
//            int data=fileInputStream.read();
//            while(data!=-1){
//                /*4. 这一步相当于继续移动指针*/
//                System.out.print((char) data);
//                data=fileInputStream.read();
//            }
byte[] buffer = new byte[5];
int len;
while ((len = fileInputStream.read(buffer)) != -1) {
/*读进去几个就取几个,不能使用buffer.length*/
//                for(int i=0;i<len;i++){
//                    System.out.println((char) buffer[i]);
//                }
/*方式二*/
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() {

/*1. 提供源文件和目标文件*/
File src = new File("hello.txt");
File dec = new File("hello1.txt");
/*2. 创建输入流以及输出流*/
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();

}

}

参考文献

  1. aliyun
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: