Java第十七天-IO流,文件流
IO流用来处理设备之间的数据传输,
Java中对数据的输入输出操作以“流(steam)”的方式进行
java.io包下提供了各种“流”类和接口,用以获取不同种类的数据,并通过标准的方法输入或输出数据
节点流:程序直接操作文件
处理流:包着节点流或者处理流的流,提高节点流的速率,不直接作用于文件
//从硬盘存在的文件中读取其内容到程序中,使用FileInputSteam
//被读取的文件一定要存在,不然会报FileNotFoundException
//1.创建一个File类的对象
File file = new File("Hello");
//2.创建一个FileInputStream类的对象
FileInputStream fis = new FileInputStream(file);
//3.使用FileInputStream的方法
// int b = fis.read();
// while(b!=-1){
// System.out.print((char)b);
// b = fis.read();
// }
int b;
while((b = fis.read())!= -1){
System.out.print((char)b);
}
// 4.关闭相应的流
fis.close();
字节流对非文本文件的读取,用字符的的方式
// 使用try-catch方式更好:保证流的关闭操作一定进行,用throws的方式,若程序发生异常
// 则会跳出此方法,导致后续代码无法执行,持续消耗内存
// 1.创建一个File类的对象
// 2.创建一个FileInputStream类的对象
FileInputStream fis = null;
try {
File file = new File("Hello");
fis = new FileInputStream(file);
// 3.使用FileInputStream的方法
int b;
while ((b = fis.read()) != -1) {
System.out.print((char) b);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if(fis !=null){
// 4.关闭相应的流
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
实际过程中不使用read(),使用read(byte[] b),因为以数组的方式传输,效率更高
字节流对非文本文件的读取,用数组的方式
@Test
public void testFileInputSteam2() {
FileInputStream fis = null;
try {
File file = new File("Hello");
fis = new FileInputStream(file);
byte[] b = new byte[5];
int len;// 记录每次写入的长度
while ((len = fis.read(b)) != -1) {
//第一种方式
// for(int i = 0;i < len;i++ ){
// System.out.print((char)b[i]);
// }
//第二种方式
String str = new String(b, 0, len);
System.out.print(str);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if(fis != null){
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
字节流对非文本文件的写入
// 1.创建一个File对象,表明要写入的文件位置
// 输出的物理文件可以不存在,当执行过程中,若不存在,程序会自动创建
File file = new File("Hello2.txt");
//2.创建一个FileOuputStream的对象,将file的对象作为形参传递给FileOuputStream的构造器
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
//3.写入操作
fos.write(new String("I love you").getBytes());// write方法接收的是byte型的数组,需要转换
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
//4.关闭数据流
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
字符流对本本文件进行读取
// 从硬盘中读取一个文件,并写入到另一个位置(相当于文件的赋值)
@Test
public void testFileInputOutputStream() {
// 1.提供读入,写出的文件
File file1 = new File("C:\\Users\\47337\\Desktop\\1.jpg");
File file2 = new File("C:\\Users\\47337\\Desktop\\2.jpg");
// 2.提供相应的流
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(file1);
fos = new FileOutputStream(file2);
// 3.实现文件的复制
byte[] b = new byte[20];
int len;
while ((len = fis.read(b)) != -1) {
// fos.write(b)是错误的写法,相当于fos.write(b,0,b.length)
fos.write(b, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fos != null)
try {
{
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//如果上面代码中(catch)还有其他语句,就必须写在此finally里
if (fis != null)
try {
{
fis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
字符流对文本进行复制
// 实现对文件的复制
@Test
public void copy() {
/*
* 使用FileReader FileWriter 可以实现的复制
* 对于非文本文件(视频文件,音频文件,图片)只能使用字节流
*/
//1.输入流对应的file1一定要存在
FileReader fr = null;
FileWriter fw = null;
try {
File file1 = new File("dbcp.txt");
File file2 = new File("dbcp1.txt");
fr = new FileReader(file1);
fw = new FileWriter(file2);
int len;
char[] c = new char[24];
while ((len = fr.read(c)) != -1) {
// String Str = new String(c, 0, len);
fw.write(c, 0, len);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (fr != null) {
try {
try {
fr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} finally {
if (fw != null) {
try {
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}