Java学习笔记之:Java 流

时间:2023-03-09 17:08:08
Java学习笔记之:Java 流

一、介绍

Java.io包几乎包含了所有操作输入、输出需要的类。所有这些流类代表了输入源和输出目标。

Java.io包中的流支持很多种格式,比如:基本类型、对象、本地化字符集等等。

一个流可以理解为一个数据的序列。输入流表示从一个源读取数据,输出流表示向一个目标写数据。

二、笔记

1.IO

import java.io.File;

/**
* IO流 1.File类:可以操作指定路径的文件,提供了对文件的增删改查
*
*
*
*/
public class IO {
public static void main(String[] args) {
try {
//test();
new IO().check("D:\\ ");
} catch (Exception e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
} public static void test() throws Exception {
String path = "D:\\xx;
// 查看文件
// 获取文件路径方法(在网络路径中才能看出效果)
File file = null;
file = new File(path);
// System.out.println(file.getAbsolutePath());
//
// String[] names = file.list();
// for (String string : names) {
// System.out.println(string);
// } // 获取当前路径下的所有的文件的文件对象
// File[] files=file.listFiles();
// for (File f : files) {
// System.out.println(f.getPath());
// System.out.println(f.getParent());
// } // 创建文件
// path+="\\a.txt";
// file=new File(path);
// boolean result=file.createNewFile();
// System.out.println(result); // 创建文件夹
// path+="\\text";
// file=new File(path);
// boolean result=file.mkdir();
// System.out.println(result); // 递归创建文件夹
// path += "\\text\\abc";
// file = new File(path);
// boolean result = file.mkdirs();
// System.out.println(result); // 重命名,并移动路径
path += "\\abc.txt";
file = new File(path);
file.renameTo(new File("D:\\xx\\abcd.txt"));
} public void check(String path) {
File file = new File(path);
File[] files = file.listFiles();
for (File f : files) {
if (f.isFile()) {
System.out.println(f.getAbsolutePath());
}
else {
check(f.getAbsolutePath());
}
}
} }

2.stream

/**
* 流:可以理解数据的滚动,就是一个数据流
1.按方向分(程序员角度):输入流,输出流
2.按处理单位:字节流(OutputStream,InputStream的子类都是字节流),字符流
3.按功能:节点流,
*
*
*
*/
public class Stream { public static void main(String[] args) { try {
// file();
// byteArray();
obj();
} catch (Exception e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
} public static void file() {
try {
FileInputStream inputStream = new FileInputStream(new File(
"D:\\xx\\abcd.txt"));
FileOutputStream outputStream = new FileOutputStream(
"D:\\xx\\a.txt");
byte[] bs = new byte[10];
int len = 0;
while ((len = inputStream.read(bs)) != -1) {
// String msg = new String(bs, 0, len);
// System.out.println(msg); outputStream.write(bs, 0, len);
}
inputStream.close();
outputStream.close();
} catch (Exception e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
} /**
* 字节数组流是最小单位的流,一般处理不了的都可以用它
*
* @throws IOException
*/
public static void byteArray() throws IOException { String string = "ABCDEFG";
byte[] bs = string.getBytes();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ByteArrayInputStream inputStream = new ByteArrayInputStream(bs); int len = 0;
byte[] r = new byte[2];
// while ((len=inputStream.read())!=-1) {//默认的读取方法,每次读取一个字节
while ((len = inputStream.read(r)) != -1) {
String msgString = new String(r, 0, len);
System.out.println(msgString);
} } // 对象流
public static void obj() throws Exception {
ObjectOutputStream outputStream = new ObjectOutputStream(
new FileOutputStream("D:\\博客\\a.txt"));
User user = new User(20, "tom");
outputStream.writeObject(user);// 如果需要将一个对象写入到流中,需要实现
outputStream.flush();
outputStream.close();
System.out.println("ok");
ObjectInputStream inputStream = new ObjectInputStream(
new FileInputStream("D:\\xx\\a.txt"));
Object object = inputStream.readObject();
if (object instanceof User) {
System.out.println(((User) object).getName());
System.out.println(((User) object).getAge());
}
} // 字符串缓冲流
public static void stringBuffer() throws Exception {
StringBufferInputStream inputStream = new StringBufferInputStream("ABCDE");
int len = 0;
byte[] bs = new byte[1024];
while ((len = inputStream.read(bs)) != -1) {
String msg = new String(bs, 0, len);
System.out.println(msg); }
}
}

3.字符流

/**
* 字符流:只能处理文字类文件,不能处理图片
* 总结
* 1.输入流 输出流
* 2.字节流(OutputStream,InputStream的子类都是字节流)
* 字符流(reader和writer的子类)
* 3.按功能:节点流和处理刘
*
*
*
*/
public class CharacterStream { public static void main(String[] args) {
try {
file();
} catch (Exception e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
} public static void file() throws Exception { FileReader reader = new FileReader(new File("D:\\xx\\abcd.txt"));
FileWriter writer = new FileWriter(new File("D:\\xx\\abcd.txt"));
// while (reader.ready()) {
// System.out.println(reader.read());
// } int len = 0;
char[] cs = new char[1024];
while ((len = reader.read(cs)) != -1) {
System.out.println(new String(cs, 0, len)); }
reader.close();
} public static void buffer() throws Exception {
BufferedReader reader = new BufferedReader(new FileReader(
"D:\\xx\\abcd.txt"));
BufferedWriter writer = new BufferedWriter(new FileWriter(
"D:\\xx\\abcd.txt"));
int len = 0;
char[] cs = new char[1024];
while ((len = reader.read(cs)) != -1) {
writer.write(cs, 0, len);
}
while (reader.ready()) {
System.out.println(reader.readLine() + "|||");
writer.write(reader.readLine());
}
reader.close();
writer.close();
}
}