JAVASE自学笔记IO File

来源:互联网 发布:linux 进程调度 编辑:程序博客网 时间:2024/05/17 11:59
package com.atguigu.javase.lesson10;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;


import org.junit.Test;


public class IOTest {

/**
* 利用字符输入输出流, 完成 hello.txt 文件的复制.
* 把该文件复制为 hello2.txt
* @throws IOException 
*/

/**
* 利用字节输入输出流, 完成 hello.txt 文件的复制.
* 把该文件复制为 hello2.txt
* @throws IOException 
*/
@Test
public void testCopyFile() throws IOException{
//1. 创建定位到 hello.txt 的文件的输入流
InputStream in = new FileInputStream("枚举类.avi");

//2. 创建定位到 hello2.txt 的文件输出流
OutputStream out = new FileOutputStream("枚举类2.avi");

//3. 创建一个 byte 数组, 用于读写文件 
byte [] buffer = new byte[1024 * 10];
int len = 0;

//4. 读写文件:
//in.read(buffer); out.write(buffer, 0, len);
while((len = in.read(buffer)) != -1){
out.write(buffer);
}

//5. 关闭流资源. 
out.close();
in.close();
}

/**
* 测试字节输出流
* @throws IOException
*/
@Test
public void testOutputStream() throws IOException{
OutputStream out = new FileOutputStream("abcd.txt");

String content = "www.atguigu.com\nHello Java!";
// int len = 10;
//
// byte [] contentBytes = content.getBytes();
//
// for(int i = 0; i < content.length() / 10; i++){
// //把 String 拆分成多个 buffer
// out.write(contentBytes, i * 10, len);
// }
//
// if(content.length() % 10 != 0){
// out.write(contentBytes, 10 * (content.length() / 10), 
// content.length() - (10 * (content.length() / 10)));
// }

out.write(content.getBytes());

out.close();
}

/**
* 测试字符输入流.
* @throws IOException 
*/
@Test
public void testReader() throws IOException{
//利用字符输入流读取 hello.txt 文档的内容, 输出到控制台. 
Reader reader = new FileReader("hello.txt");

char [] buffer = new char[10];
int len = 0;

while((len = reader.read(buffer)) != -1){
for(int i = 0; i < len; i++){
System.out.print(buffer[i]);
}
}

reader.close();
}

/**
* 测试字节输入流
* @throws IOException
*/
@Test
public void testInputStream() throws IOException{
//1. 创建了一个字节输入流.
InputStream in = new FileInputStream("hello.txt");

//2. 读取文件的内容
//2.1 第一读取一个字节. 效率很低, 不建议这样读. -1 表示读取到文件的结尾处
// int result = in.read();
//
// while(result != -1){
// System.out.print((char)result);
// result = in.read();
// }

//2.2 一次读取一组: 一组字符. 
//返回一次实际读取的字节数, 若为 -1 表示读取到文件的结尾
// byte [] buffer = new byte[10];
// int len = 0;
//
// while((len = in.read(buffer)) != -1){
// for(int i = 0; i < len; i++){
// System.out.print((char)buffer[i]);
// }
// }

//2.3 把内容读取到字节数组的部分连续的元素中.
byte [] result = new byte[1024 * 10];
in.read(result, 10, in.available());

//3. 关闭流资源
in.close();
}

/**
* File: 代表物理的意义的文件或目录
* @throws IOException   
*/
@Test
public void testFile() throws IOException{
//1. 创建 File 对象
File file = new File("hello.txt");

//2. 测试 File 对象的方法. 
//2.1 文件名相关的方法
String fileName = file.getName();
System.out.println(fileName); 

//2.2 访问文件的绝对路径
String path = file.getAbsolutePath();
System.out.println(path); 

//2.3 为文件重命名
//file.renameTo(new File("d:\\hello.txt"));

//3. 文件检测相关的方法
System.out.println(file.exists());
File dir = new File("atguigu");
System.out.println(dir.isFile()); 

//4. 获取文件的常规信息
System.out.println(file.length()); 

//5. 文件操作相关.
File file2 = new File("abcd.txt");
file2.createNewFile();
}

}