Java IO 的一般使用原则 :
一、按数据来源(去向)分类:
1 、是文件: FileInputStream, FileOutputStream, ( 字节流 )FileReader, FileWriter( 字符 )
2 、是 byte[] : ByteArrayInputStream, ByteArrayOutputStream( 字节流 )
3 、是 Char[]: CharArrayReader, CharArrayWriter( 字符流 )
4 、是 String: StringBufferInputStream, StringBufferOuputStream ( 字节流 )StringReader, StringWriter( 字符流 )
5 、网络数据流: InputStream, OutputStream,( 字节流 ) Reader, Writer( 字符流 )
二、按是否格式化输出分:
1 、要格式化输出: PrintStream, PrintWriter
三、按是否要缓冲分:
1 、要缓冲: BufferedInputStream, BufferedOutputStream,( 字节流 ) BufferedReader, BufferedWriter( 字符流 )
四、按数据格式分:
1 、二进制格式(只要不能确定是纯文本的) : InputStream, OutputStream 及其所有带 Stream 结束的子类
2 、纯文本格式(含纯英文与汉字或其他编码方式); Reader, Writer 及其所有带 Reader, Writer 的子类
五、按输入输出分:
1 、输入: Reader, InputStream 类型的子类
2 、输出: Writer, OutputStream 类型的子类
六、特殊需要:
1 、从 Stream 到 Reader,Writer 的转换类: InputStreamReader, OutputStreamWriter
2 、对象输入输出: ObjectInputStream, ObjectOutputStream
3 、进程间通信: PipeInputStream, PipeOutputStream, PipeReader, PipeWriter
4 、合并输入: SequenceInputStream
5 、更特殊的需要: PushbackInputStream, PushbackReader, LineNumberInputStream, LineNumberReader
决定使用哪个类以及它的构造进程的一般准则如下(不考虑特殊需要):
首先,考虑最原始的数据格式是什么: 原则四
第二,是输入还是输出:原则五
第三,是否需要转换流:原则六第 1 点
第四,数据来源(去向)是什么:原则一
第五,是否要缓冲:原则三 (特别注明:一定要注意的是 readLine() 是否有定义,有什么比 read, write 更特殊的输入或输出方法)
第六,是否要格式化输出:原则二 ==
自己写的小demo
import java.io.*;
import java.nio.charset.Charset;
import java.util.Arrays;
/** IO相关
* @author wangtianqi
* @create 2020-09-01 9:30
*/
public class Test2 {
public static void main(String[] args) throws IOException {
File test = new File("C:/Users/dell/Desktop/TianQiCode/test1.txt");
File img = new File("C:/Users/dell/Desktop/TianQiCode/test.jpg");
File imgTo = new File("C:/Users/dell/Desktop/TianQiCode/test/test.jpg");
Test2.testInputStreamReader(test);
Test2.testOutputStream("123456一二三四五六");
Test2.testCopyFile(img,imgTo);
}
// 从系统中读取文本文件,并将内容打印到控制台
public static void testInputStreamReader(File test) throws IOException{
InputStream txtInputStream = new FileInputStream(test);
// 指定将字节流以何种方式解码为字符流
InputStreamReader txtReader = new InputStreamReader(new FileInputStream(test), Charset.forName("GBK"));
byte[] bt = new byte[(int) test.length()];
char[] ch = new char[(int) test.length()];
if (txtInputStream.read(bt)!= -1) {
System.out.println(Arrays.toString(bt));
String string = new String(bt);
System.out.print(string+" ");
}
System.out.println();
if (txtReader.read(ch) != -1) {
String string = new String(ch);
System.out.print(string+" ");
}
}
// 将字符串写入到新建文本文档中
public static void testOutputStream(String txt) throws IOException{
String name = "test";
int i = 0;
File f = new File("C:/Users/dell/Desktop/TianQiCode/");
while (f.exists() == true) {
f = new File("C:/Users/dell/Desktop/TianQiCode/" + name + (i++) + ".txt");
}
f.createNewFile();
OutputStreamWriter txtWriter = new FileWriter(f);
char[] txtChar = txt.toCharArray();
System.out.println(Arrays.toString(txtChar));
try {
txtWriter.write(txtChar);// 数据没有直接写到文件,其实是写到了内存缓冲区。
txtWriter.close();
System.out.println("文件写入成功");
} catch (IOException e) {
System.out.println("文件写入失败");
e.printStackTrace();
}
}
//复制文件到另一个目录下
public static void testCopyFile(File from, File to) throws IOException{
InputStream fileFrom = new FileInputStream(from);
byte[] fileByte = new byte[(int) from.length()];
fileFrom.read(fileByte);
OutputStream fileTo = new FileOutputStream(to);
try {
fileTo.write(fileByte);
fileTo.close();
System.out.println("复制文件成功");
} catch (IOException e) {
System.out.println("复制文件失败");
e.printStackTrace();
}
}
}