先贴一下我在作业中用到的三种文件输入辅助类、三种文件输出辅助类

public class BuffIn implements InHelp{
private BufferedReader bufferedReader; public BuffIn(String path) {
try {
bufferedReader = new BufferedReader(
new InputStreamReader(new FileInputStream(path), "UTF-8"));
}catch (IOException e){
System.err.println("请提供正确路径");
PathException e2 = new PathException(path,"路径地址错误");
Logs.logger.error(e2.getMessage());
}
} @Override
public String read() throws IOException {
TimeCounter.start();
String s = bufferedReader.readLine();
TimeCounter.pause();
return s;
} @Override
public void shut() throws IOException {
bufferedReader.close();
}
} public class BuffreaderIn implements InHelp{
private BufferedReader bufferedReader;
public BuffreaderIn(String path) {
try {
bufferedReader = new BufferedReader(new FileReader(path));
}catch (IOException e){
System.err.println("请提供正确路径");
PathException e2 = new PathException(path,"路径地址错误");
Logs.logger.error(e2.getMessage());
}
} @Override
public String read() throws IOException {
TimeCounter.start();
String s = bufferedReader.readLine();
TimeCounter.pause();
return s;
} @Override
public void shut() throws IOException {
bufferedReader.close();
}
} public class ScannerIn implements InHelp{
private Scanner scanner;
private java.io.File file;
public ScannerIn(String path) {
try {
file = new java.io.File(path);
scanner = new Scanner(file);
}catch (IOException e){
System.err.println("请提供正确路径");
exception.PathException e2 = new exception.PathException(path,"路径地址错误");
logs.Logs.logger.error(e2.getMessage());
}
} @Override
public String read() throws IOException {
TimeCounter.start();
if(!scanner.hasNextLine()) return null;
String s = scanner.nextLine();
TimeCounter.pause();
return s;
} @Override
public void shut() throws IOException {
scanner.close();
}
}
public class BuffOut implements OutHelp{
private BufferedWriter bufferedWriter;
private FileOutputStream fileOutputStream;
public BuffOut(String path) {
try {
fileOutputStream = new FileOutputStream(path);
bufferedWriter = new BufferedWriter(
new OutputStreamWriter(fileOutputStream, "UTF-8"));
}catch (IOException e){
System.err.println("请提供正确路径");
PathException e2 = new PathException(path,"路径地址错误");
Logs.logger.error(e2.getMessage());
}
} @Override
public void write(String s) throws IOException {
TimeCounter.start();
bufferedWriter.write(s);
TimeCounter.pause();
} @Override
public void shut() throws IOException {
bufferedWriter.close();
fileOutputStream.close();
}
} public class StreamOut implements OutHelp{
private OutputStreamWriter outputStreamWriter;
private FileOutputStream fileOutputStream; public StreamOut(String path) {
try {
fileOutputStream = new FileOutputStream(path);
outputStreamWriter = new OutputStreamWriter(fileOutputStream, "UTF-8");
}catch (IOException e){
System.err.println("请提供正确路径");
PathException e2 = new PathException(path,"路径地址错误");
Logs.logger.error(e2.getMessage());
}
} @Override
public void write(String s) throws IOException {
TimeCounter.start();
outputStreamWriter.write(s);
TimeCounter.pause();
} @Override
public void shut() throws IOException {
outputStreamWriter.close();
fileOutputStream.close();
}
} public class WriterOut implements OutHelp {
private FileWriter fileWriter; public WriterOut(String path) {
try {
fileWriter = new FileWriter(path);
}catch (IOException e){
System.err.println("请提供正确路径");
PathException e2 = new PathException(path,"路径地址错误");
Logs.logger.error(e2.getMessage());
}
} @Override
public void write(String s) throws IOException {
TimeCounter.start();
fileWriter.write(s);
TimeCounter.pause();
} @Override
public void shut() throws IOException {
fileWriter.close();
}
}

主要的类如下:

1. File(文件特征与管理):用于文件或者目录的描述信息,例如生成新目录,修改文件名,删除文件,判断文件所在路径等。

2. InputStream(二进制格式操作):抽象类,基于字节的输入操作,是所有输入流的父类。定义了所有输入流都具有的共同特征。

3. OutputStream(二进制格式操作):抽象类。基于字节的输出操作。是所有输出流的父类。定义了所有输出流都具有的共同特征。

4.Reader(文件格式操作):抽象类,基于字符的输入操作。

5. Writer(文件格式操作):抽象类,基于字符的输出操作。

6. RandomAccessFile(随机文件操作):一个独立的类,直接继承至Object.它的功能丰富,可以从文件的任意位置进行存取(输入输出)操作。

针对一些频繁的设备交互,Java语言系统预定了3个可以直接使用的流对象,分别是:

·        System.in(标准输入),通常代表键盘输入。

·        System.out(标准输出):通常写往显示器。

·        System.err(标准错误输出):通常写往显示器。

IOException异常类的子类
    1.public class  EOFException :   非正常到达文件尾或输入流尾时,抛出这种类型的异常。

2.public class FileNotFoundException:   当文件找不到时,抛出的异常。

3.public class InterruptedIOException: 当I/O操作被中断时,抛出这种类型的异常。

Java中的IO基本用法-LMLPHP

Java中的IO基本用法-LMLPHP

05-26 19:47