IO流的分类
1, 输入流(从硬盘到内存),输出流(从内存到硬盘)
2,字节流,字符流(按照数据划分)
所以IO流就是字节输出流,字节输入流,字符输出流,字符输入流
字节输入,输出的超类
InputStream(此抽象类是表示字节输入流的所有类的超类)
FileInputStream类(从文件系统中的某个文件中获得输入字节 用于读取诸如图像数据之类的原始字节流)
read()方法
public static void main(String[] args) throws IOException {
//1,获取一个输入流
FileInputStream fis =new FileInputStream("test.txt");
//2,从此输入流中读取一个数据字节
//int cha =fis.read();//cha表示的是char所对应的ASCII吗,所以是int
//System.out.println(cha);
//使用循环读取,循环的终止条件是返回-1,说明读到文件的末尾了
int cha;
//read()相当于Iterator里面的next()是一个一个进行读取
while((cha = fis.read())!=-1) {
System.out.println((char)cha);//如果读取中文会有问题
}
read(byte[] b)方法
public static void main(String[] args) throws IOException {
//1,获取一个输入流
FileInputStream fis =new FileInputStream("test.txt");
//2,从此输入流中读取数据字节
byte [] ch =new byte [5];
//int len =fis.read(ch);
//System.out.println(len+" "+new String(ch));
//使用循环读取,循环的终止条件是返回-1,说明读到文件的末尾了
int len;
while((len = fis.read(ch))!=-1) {
System.out.println(len+" "+new String(ch,0,len));
}
OutputStream(此抽象类是表示输出字节流的所有类的超类)
FileOutputStream 文件输出流是用于将数据写入 File
或 FileDescriptor
的输出流
write(byte[] b)将b.length个字节从指定byte数组写入此文件输出流中
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
//1,创建一个向具有指定 name 的文件中写入数据的输出文件流。如果第二个参数为 true,则将字节写入文件末尾处,而不是写入文件开始处
FileOutputStream out =new FileOutputStream("test.txt",true);//如果文件不存在,可以去创建
String s ="you are great";
//getBytes()使用给定的 charset 将此 String 编码到 byte 序列,并将结果存储到新的 byte 数组。
out.write(s.getBytes());
System.out.println("完毕");
}
复制文件的小案例
public static void main(String[] args) {
// TODO Auto-generated method stub
File source =new File("周杰伦 - 超人不会飞.m4a");
File rerive =new File("复制版周杰伦 - 超人不会飞.m4a");
FileInputStream fis = null;
FileOutputStream out = null;
try {
//获取输入流
fis =new FileInputStream(source);
//获取输出流
out =new FileOutputStream(rerive);
//读取数据
byte [] cha =new byte[1024];
int len;
while((len=fis.read(cha))!=-1) {
out.write(cha,0,len);
}
} 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();
}
}
if(out!=null) {
try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
将集合的内容写入到一个文件中小案例
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
TreeSet<Student> tree =new TreeSet<Student>(Collections.reverseOrder(new Comparator<Student> (){
@Override
public int compare(Student o1, Student o2) {
// TODO Auto-generated method stub
return o2.getTotalScore()-o1.getTotalScore();
}
}));
//获取当前操作系统的一个换行
String LINE_SEPARATOR =System.getProperty("line.separator");
tree.add(new Student("张三",12,34,56));
tree.add(new Student("张四",12,34,50));
tree.add(new Student("张五",12,34,89));
tree.add(new Student("张六",12,30,80));
//System.out.println(tree);
//新建一个File类
File f =new File("stu.txt");
//获取输出流
FileOutputStream out =new FileOutputStream(f);
//遍历集合
for(Student s:tree) {
String ss=s.toString();
//将文件写入
out.write(ss.getBytes());
//换行
out.write(LINE_SEPARATOR.getBytes());
}
out.close();
}
}
class Student{
private String name;
private int score1;
private int score2;
private int score3;
private int totalScore;
public Student(String name,int score1,int score2,int score3) {
this.name =name;
this.score1 =score1;
this.score2 = score2;
this.score3 =score3;
this.totalScore =score1+score2+score3;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + score1;
result = prime * result + score2;
result = prime * result + score3;
result = prime * result + totalScore;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (score1 != other.score1)
return false;
if (score2 != other.score2)
return false;
if (score3 != other.score3)
return false;
if (totalScore != other.totalScore)
return false;
return true;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getScore1() {
return score1;
}
public void setScore1(int score1) {
this.score1 = score1;
}
public int getScore2() {
return score2;
}
public void setScore2(int score2) {
this.score2 = score2;
}
public int getScore3() {
return score3;
}
public void setScore3(int score3) {
this.score3 = score3;
}
public int getTotalScore() {
return totalScore;
}
public void setTotalScore(int totalScore) {
this.totalScore = totalScore;
}
@Override
public String toString() {
return "Student [name=" + name + ", score1=" + score1 + ", score2=" + score2 + ", score3=" + score3
+ ", totalScore=" + totalScore + "]";
}
}
文件复制小案例
* 需求:将一个目录中的所有以html结尾的文件统一复制到一个sourcecode的文件夹中
* 1,遍历当前目录中的所有文件和文件夹
* 2,选择是html结尾的文件(文件过滤)
* 3,复制文件到统一目录
public class CopyJavaFile {
/**
* 需求:将一个目录中的所有以html结尾的文件统一复制到一个sourcecode的文件夹中
* 1,遍历当前目录中的所有文件和文件夹
* 2,选择是html结尾的文件(文件过滤)
* 3,复制文件到统一目录
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
//新建File类
File source =new File("D:\\eclipse");
File aim =new File("sourcecode");
//如果目录不存在,创建一个目录
if(!aim.exists()) {
aim.mkdir();
}
//来个集合
LinkedList<File> queue =new LinkedList<File>();
//进队
queue.offer(source);
while(!queue.isEmpty()) {
File am =queue.poll();
File [] files =am.listFiles();
for(File f:files) {
if(f.isDirectory()) {
queue.offer(f);
}
else {
//如果文件的后缀名是HTML,则进行复制
if(f.getName().endsWith(".html")) {
copy(aim,f);
}
}
}
}
}
private static void copy(File aim, File f) throws IOException {
// TODO Auto-generated method stub
//拷贝后的文件,路径名是aim+f.getName()
File javaFile =new File(aim,f.getName());
//获得输入流
FileInputStream fis =new FileInputStream(f);
//获得输出流
FileOutputStream out =new FileOutputStream(javaFile);
int len;
byte [] bf =new byte [1024];
while((len = fis.read(bf))!=-1) {
out.write(bf,0,len);
}
fis.close();
out.close();
}
}
文件复制的总结
1,创建文件类
File source =new File(directory)
File aim =new File(directory2)
2,获取输入流,输出流
FileInputStream f =new FileInputStream(source);
FileOutputStream f1=new FileOutputStream(aim);
3,每次一1024K进行读和写
int len;
byte [] bf =new byte[1024];
while((len= f.read(bf))!=-1){
//写入
f1.write(bf,0,len);
}
4,关闭流
f.close();
f1.close();