- 字符型文件流
package com.uncle.test_io.readerandwriter;
/**
* 字符型文件流
* FileReader FileWriter
* 只能操作纯文本文件(文件右键打开方式 记事本打开 能看懂)
* .txt
*
* FileReader
* 1.java.io包
* 2.继承 InputStreamReader Reader
* 3.构造方法
* 4.常用
* read()
* read(char[])
* FileWriter
* 1.java.io包
* 2.继承 OutputStreamWriter Writer
* 3.构造方法
* 带file参数 带file,boolean参数
* 带String参数 带String,boolean参数
* 4.常用
* write(int)
* write(char[])
* write(string)
* flush close
*
*
* FileInputStream/FileOutputStream
* FileReader/FileWriter
* read write
*/
import java.io.*;
public class TestFileReader {
public static void main(String[] args){
try {
//字符型文件输入流
File file = new File("D://test//Test.txt");
FileReader fr = new FileReader(file);
String str = "abc";
char[] c = str.toCharArray();
FileWriter fw = new FileWriter(file);
fw.write(97);
fw.write(c);
fw.write(str);
fw.flush();
// int code = fr.read();
// System.out.println(code);
// char[] c = new char[1024];
// int count = fr.read(c);
// while(count!=-1){
// System.out.print(new String(c,0,count));
// count = fr.read(c);
// }
} catch (IOException e) {
e.printStackTrace();
}
// try {
// //演示字节型文件输入流 读取中文可能会产生问题
// //字节流好处在于什么类型的文件都可以处理 不好在于处理纯文本的文件可能会产生乱码的问题
// FileInputStream fis = new FileInputStream("D://test//Test.txt");
// byte[] b = new byte[5];
// int count = fis.read(b);// 16bit
// while(count!=-1){
// System.out.print(new String(b,0,count,"GBK"));
// count = fis.read(b);
// }
// } catch (IOException e) {
// e.printStackTrace();
// }
}
}
- 操作文件
package com.uncle.test_io.readerandwriter;
import java.io.*;
public class OperateFile {
//设计一个方法 文件的复制
// public void copyFile(File file,String path){//当做file是一个文件 C://Test.txt
// FileInputStream fis = null;
// FileOutputStream fos = null;
// try {
// //创建一个输入流
// fis = new FileInputStream(file);
// //创建一个新的file对象
// File newFile = new File(path+"//"+file.getName());
// fos = new FileOutputStream(newFile);
// //读取文件中的信息
// byte[] b = new byte[1024];//通常创建的数组 1kb--8kb之间
// int count = fis.read(b);//1024 21有效的
// while(count!=-1) {
// //做点手脚
// fos.write(b,0,count);//将读取到的有效字节 写入
// fos.flush();
// count = fis.read(b);
// }
// System.out.println("复制完毕");
// } catch (IOException e) {
// e.printStackTrace();
// } finally {
// //关闭
// try {
// if(fis!=null) {
// fis.close();
// }
// } catch (IOException e) {
// e.printStackTrace();
// }
// try {
// if(fos!=null) {
// fos.close();
// }
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
// }
//设计一个方法 文件的复制
public void jiaMiFile(File file,String path){
//当做file是一个文件 C://Test.txt
FileInputStream fis = null;
FileOutputStream fos = null;
try {
//创建一个输入流
fis = new FileInputStream(file);
//创建一个新的file对象
File newFile = new File(path+"//"+file.getName());
fos = new FileOutputStream(newFile);
//读取文件中的信息
byte[] b = new byte[1024];//通常创建的数组 1kb--8kb之间
int count = fis.read(b);//1024 21有效的
while(count!=-1) {
//做点手脚 每一次数组的前两个元素位置互换 1024
byte temp = b[0];
b[0] = b[1];
b[1] = temp;
fos.write(b,0,count);//将读取到的有效字节 写入
fos.flush();
count = fis.read(b);
}
System.out.println("加密完毕");
} catch (IOException e) {
e.printStackTrace();
} finally {
//关闭
try {
if(fis!=null) {
fis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if(fos!=null) {
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
//deleteFile(file);
}
//文件夹的复制 file可以代表文件 可以代表文件夹
public void superCopyFile(File file,String newPath){
//获取file的绝对路径 拼串的方式获取新文件的名字
String oldFilePath = file.getAbsolutePath();// C://aaa//bbb文件夹--->D://test//aaa//bbb
String newFilePath = newPath+oldFilePath.split(":")[1];
//创建一个新的file对象
File newFile = new File(newFilePath);
//判断当前传递进来的file是个文件还是文件夹 isFile isDirectory listFiles
File[] files = file.listFiles();//获取当前传递进来的file对象所有子元素
if(files!=null){
//file是一个文件夹 才有数组对象
//通过新的file对象操作 在硬盘上创建一个文件夹
newFile.mkdir();
System.out.println(newFile.getName()+"文件夹复制完毕");
//里面的元素
if(files.length!=0){
for(File f:files){
this.superCopyFile(f,newPath);
}
}
}else{
//file是一个文件 没有子元素 不需要数组对象
//创建两个文件流 分别读取旧的file和写入新的newFile
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(file);
fos = new FileOutputStream(newFile);
byte[] b = new byte[1024];
int count = fis.read(b);
while(count!=-1){
fos.write(b,0,count);
fos.flush();
count = fis.read(b);//别忘了再读一遍
}
System.out.println(newFile.getName()+"文件复制完毕");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(fis!=null) {
fis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if(fos!=null) {
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args){
OperateFile of = new OperateFile();
//of.copyFile(new File("C://js实现照片墙.zip"),"D://test");
//of.jiaMiFile(new File("C://js实现照片墙.zip"),"D://test");
of.superCopyFile(new File("C://Java第54课---字节型文件流"),"D://test");
}
}