您的位置:首页 > 编程语言 > Java开发

【Java学习笔记】18.Java 流(Stream)、文件(File)和IO

2017-05-18 18:39 856 查看
1。Java的File类

1.File的构造方法

File(String pathname);//把一个路径名称封装成File对象
File(String parent, String child);//把一个父路径和一个子路径封装成一个File对象
File(File parent, String child);
//把一个父路径File对象和一个子路径封装成一个File对象


2.常见的功能方法

A.创建文件

public boolean createNewFile();//如果文件不存在,就创建。否则,不创建。


B.创建目录

public boolean mkdir();//如果目录不存在,就创建。否则,不创建。
public boolean mkdirs();//如果目录不存在,就创建。否则,不创建。
//即时父目录不存在,也可以连父目录一起创建。


C.删除文件

public boolean delete();//既可以删除文件,又可以删除目录。


D.其他功能

public boolean isDirectory();//是否是目录
public boolean isFile();//:是否是文件
public boolean exists();//是否存在
public boolean canRead();//是否可读
public boolean canWrite();//是否可写
public boolean isHidden();//是否隐藏
public String getAbsolutePath();//获取绝对路径
public String getPath();//获取相对路径
public String getName();//获取名称


4.一个小程序例子

eg:在D盘目录下创建hello.txt文件

import java.io.*;

public class hello{
public static void main(String[] args) {
File f=new File("D:\\hello.txt");
try{
f.createNewFile();
}catch (Exception e) {
e.printStackTrace();
}
}
}


2.Java中的流

1.I/O流分类

> 一般按照数据类型分类(两大类):字节流,字符流
按照输入输出又分有:输入流和输出流


1)字节流:

字节输入流 InputStream(抽象类)

字节输出流 OutputStream(抽象类)

2)字符流:

字符输入流 Reader

字符输出流 Writer

2.字节流

eg:用字节流往一个文本文件中写一句话:”helloworld”。

import java.io.FileOutputStream;
import java.io.IOException;

public class OutputStreamDemo {
public static void main(String[] args) throws IOException {

//创建文件输出流对象
FileOutputStream fos = new FileOutputStream("a.txt");
//调用输出流的写数据的方法给文件中写数据
//public void write(byte[] b)
byte[] byf = "helloworld".getBytes();
fos.write(byf);

//释放资源,关流操作
fos.close();
}

}


eg:把a.txt的内容复制到b.txt中

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class CopyFile {
public static void main(String[] args) throws IOException {
//封装数据源和目的地
FileInputStream fis = new FileInputStream("OutputStreamDemo.java");
FileOutputStream fos = new FileOutputStream("c.java");
//2.读取数据源中的数据,将读取到的数据写入目的地中
int by;
while ((by=fis.read())!=-1) {
//将读取到的字节写入fos中
fos.write(by);
}
//释放资源
fos.close();
fis.close();
}
}


3.字符流

* OutputStreamWriter:把字节输出流转换为字符输出流

* InputStreamReader:把字节输入流转换为字符输入流

1)eg:字符流实现文件复制

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class CopyFile {
public static void main(String[] args) throws IOException {

//1.封装数据源和目的地
FileReader fr = new FileReader("InputStreamReaderDemo.java");
FileWriter fw = new FileWriter("d.java");

//2.1一次读写一个字符
/*int ch;
while ((ch=fr.read())!=-1) {
fw.write(ch);//一次写一个字符
fw.flush();//每写一次刷新缓冲区
}*/
//2.2一次读写一个字符数组
char[] chs = new char[1024];
int len;
while ((len=fr.read(chs))!=-1) {
fw.write(chs, 0, len);
fw.flush();
}
//3.关流
fw.close();
fr.close();
}

}


2)eg: 键盘录入5个学生信息(姓名,语文成绩,数学成绩,英语成绩),按照总分从高到低存入文本文件

/*
*Student类
*/
public class Student {
private String name;
private int chinese;
private int math;
private int english;
public Student(String name, int chinese, int math, int english) {
super();
this.name = name;
this.chinese = chinese;
this.math = math;
this.english = english;
}
public Student() {
super();
// TODO Auto-generated constructor stub
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getChinese() {
return chinese;
}
public void setChinese(int chinese) {
this.chinese = chinese;
}
public int getMath() {
return math;
}
public void setMath(int math) {
this.math = math;
}
public int getEnglish() {
return english;
}
public void setEnglish(int english) {
this.english = english;
}

//获取总成绩的方法
public int getAllScore(){
return chinese+math+english;
}

}


import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Comparator;
import java.util.Scanner;
import java.util.TreeSet;

public class StudentTest {
public static void main(String[] args) throws IOException {
//创建一个TreeSet集合,并写一个比较器
TreeSet<Student> ts = new TreeSet<Student>(new Comparator<Student>() {
@Override
public int compare(Student s1, Student s2) {
int num = s1.getAllScore() - s2.getAllScore();
int num2 = num==0?s1.getName().compareTo(s2.getName()):num;
return num2;
}
});

//键盘录入5个学生信息
for (int i = 0; i < 5; i++) {
//创建键盘录入对象
Scanner sc = new Scanner(System.in);
//接受键盘录入数据
System.out.println("请输入你的姓名");
String name = sc.nextLine();
System.out.println("请输入你的语文成绩");
int chinese = sc.nextInt();
System.out.println("请输入你的数学成绩");
int math = sc.nextInt();
System.out.println("请输入你的英语成绩");
int english = sc.nextInt();
//封装成学生对象,存储集合
Student s = new Student(name, chinese, math, english);
ts.add(s);
}
System.out.println("数据录入完毕。。");
//遍历集合,将集合中的数据全取不出来之后写入文件中,每一个学生信息占一行
BufferedWriter bw = new BufferedWriter(new FileWriter("score.txt"));
for (Student s : ts) {
String info = s.getName()+"  "+s.getChinese()+"  "+s.getMath()+"  "+s.getEnglish()+"  "+s.getAllScore();
//将拼接成的字符串写入文件中
bw.write(info);
bw.newLine();
bw.flush();
}

//释放资源
bw.close();

}

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