IO系统结构思维导图

第一部分 字符流
1、字符流读String 缓存流 常用 (StringReader/BufferedReader)
/**
* 字符流读String 缓存流 常用
*/
public static void readFileStringbuf() throws Exception{
Reader reader = new StringReader("eddydasdsa\r\nddsadsad "); //该构造器是纯文本,如何是路径不会解析文件内容
BufferedReader buf = new BufferedReader(reader);
String line = null;
StringBuilder builder = new StringBuilder("");
while (null !=(line = buf.readLine())){
builder.append(line);
}
System.out.println(builder.toString());
buf.close();
reader.close();
}
2、字符流读文本文件 缓存流 常用 (BufferedReader)
/**
* 字符流读文本文件 缓存流 常用
*/
public static void readFileSFbuf() throws Exception{
Reader reader = new FileReader("D:\\file\\text\\test.txt");
BufferedReader buf = new BufferedReader(reader);
String line = null;
StringBuilder builder = new StringBuilder("");
while (null !=(line = buf.readLine())){
builder.append(line);
}
System.out.println(builder.toString());
buf.close();
reader.close();
}
3、字符流读文本文件 (FileReader)
/**
* 字符流读文件
*/
public static void readFileString() throws Exception{
Reader reader = new FileReader(new File("D:\\file\\text\\test.txt"));
char[] chars = new char[1024];
int len = 0;
while ((len = reader.read(chars)) != -1){
String content = new String(chars);
System.out.println(content);
// System.out.println(new String(chars,0,len)); //等同
}
reader.close();
}
4、字符流读String (StringReader)
/**
* 字符流读String
*/
public static void readFileString1() throws Exception{
Reader reader = new StringReader("eddydasdsad"); //该构造器是纯文本,如何是路径不会解析文件内容
char[] chars = new char[1024];
int len = 0;
while ((len = reader.read(chars)) != -1){
String content = new String(chars);
System.out.println(content);
// System.out.println(new String(chars,0,len)); //等同
}
reader.close();
}
5、字符流读文本(控制读取长度)(FileReader)
/**
* 字符流读文本(控制读取长度)
*/
public static void readFileStringlen() throws Exception{
Reader reader = new FileReader(new File("D:\\file\\text\\test.txt"));
char[] chars = new char[3]; //控制一次性读取长度
// int aa= reader.read(); //读一位
// System.out.println((char) aa);
reader.read(chars); //只读前3位
String content = new String(chars);
System.out.println(content);
int len = 0;
while ((len = reader.read(chars)) != -1){ //流,前面已读的3个不会再读,从第三位重新读
System.out.println(new String(chars));
// System.out.println(new String(chars,0,len)); //等同
}
reader.close();
}
6、单写入字符到文本文件(FileWriter)
/**
* 单纯写内容
* @throws Exception
*/
public static void onlyWrite() throws Exception{
FileWriter fileWriter = new FileWriter("D:\\file\\text\\test.txt");
String cont = "abcdefgh";
fileWriter.write(cont);
fileWriter.flush();
fileWriter.close();
}
第二部分 字节流
1、字节流读文件 缓存流 常用(FileInputStream/BufferedInputStream)
/**
* 字节流读文件 缓存流 常用
*/
public static void inFileSFbuf() throws Exception{
InputStream in = new FileInputStream("D:\\file\\text\\test.txt");
BufferedInputStream buf = new BufferedInputStream(in);
// BufferedInputStream buf = new BufferedInputStream(in,4); //可指定缓存区大小
byte[] bytes = new byte[1024]; //每次从buf缓存区读1024个字节
int len = 0;
while ((len = buf.read(bytes)) != -1){
System.out.println(new String(bytes));
}
buf.close();
in.close();
}
2、字节流读文件 (FileInputStream byte[])
/**
* 字节流读文件(文本) FileInputStream byte[]
*/
public static void readFile1() throws Exception{
// File file = new File("D:\\file\\text\\test.txt");
// InputStream in = new FileInputStream(file);
InputStream in = new FileInputStream("D:\\file\\text\\test.txt");
byte[] bytes = new byte[1024];
int len = 0;
StringBuilder builder = new StringBuilder("");
while ((len=in.read(bytes))!=-1){
builder.append(new String(bytes,"utf-8")); //如果通过电脑编辑gb2312
}
System.out.println(builder.toString());
in.close();
}
3、字节流写文件(FileOutputStream)
/**
* 字节流写文件
*/
public static void writeFile() throws Exception{
OutputStream out = new FileOutputStream("D:\\file\\text\\test.txt");
String cont = "abcdefgh";
out.write(cont.getBytes());
out.flush();
out.close();
}
4、字节流拷贝文件(FileInputStream/FileOutputStream)
/**
* 字节流拷贝文件
*/
public static void copyFile() throws Exception{
File file = new File("D:\\file\\text\\copy.txt");
if(null == file){
file.createNewFile();
}
InputStream in = new FileInputStream("D:\\file\\text\\test.txt");
OutputStream out = new FileOutputStream(file);
int len = 0;
byte[] bytes = new byte[1024];
while ((len = in.read(bytes)) != -1){
out.write(bytes);
}
out.flush();
out.close();
in.close();
}
第三部分 转换流
1、转换流 BufferedReader 读 InputStream 通过InputStreamReader(也是实现Reader) 常用
/**
* 转换流 BufferedReader 读 InputStream 通过InputStreamReader(也是实现Reader) 常用
*/
public static void test() throws Exception{
InputStream in = new FileInputStream("D:\\file\\text\\test.txt");
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line = null;
StringBuilder builder = new StringBuilder("");
while (null !=(line = reader.readLine())){
// System.out.println(line);
builder.append(line);
}
System.out.println(builder.toString());
reader.close();
in.close();
}
如转载请注明出处:https://blog.youkuaiyun.com/eddyjoe/article/details/88342117
本文深入讲解了IO系统的结构,包括字符流和字节流的读写操作,以及转换流的应用。详细介绍了各种流的使用方法,如StringReader、BufferedReader、FileReader、FileWriter、FileInputStream、BufferedInputStream、FileOutputStream和BufferedReader结合InputStreamReader的使用。
2926

被折叠的 条评论
为什么被折叠?



