字节、字符输入输出流
字节和字符输入输出流:
1、字节
输出流:超类OutputStream,对文件的输出流使用子类FileOutputStream,用来写入
输入流:超类InputStrean,对文件的输入流使用子类FileInputStream,用来读取
代码演示:
字节输出流:将文字写入到对应lili.txt文件上
public class Test3 {
public static void main(String[] args) {
out();
}
private static void out() {
//输入位置
File f1 = new File("E:\\idea_workspace3\\yangli\\class_obj\\src\\com\\lili\\file\\lili.txt");
try {
OutputStream out = new FileOutputStream(f1, true);//append 为true表示追加内容
//内容写到文件
out.write("小河流水哗啦啦".getBytes());
// 关闭流
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
字节输入流:读取lili.txt上的文字:
public class Test3 {
public static void main(String[] args) {
input();
}
private static void input() {
File f1 = new File("E:\\idea_workspace3\\yangli\\class_obj\\src\\com\\lili\\file\\lili.txt");
try {
InputStream input = new FileInputStream(f1);
byte[] bytes = new byte[1024];
StringBuilder stringBuilder = new StringBuilder();
int len = -1;
while ((len = input.read(bytes)) != -1) {
stringBuilder.append(new String(bytes, 0, len));
}
System.out.println(stringBuilder);
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
1、字符
字符输出流:Writer,对文件的操作使用子类:FileWriter
字符输入流:Reader,对文件的操作使用子类:FileReader
每次操作的都是一个字符,一般用于读取或写入文字
代码演示:
字符输出流:将文字写入到对应lili.txt文件上
public class Test4 {
public static void main(String[] args){
out();
}
private static void out(){
File f1 = new File("E:\\idea_workspace3\\yangli\\class_obj\\src\\com\\lili\\file\\lili.txt");
try {
Writer out = new FileWriter(f1,true);
out.write("我是字符输出流");
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
字符输入流:读取lili.txt上的文字:
public class Test4 {
public static void main(String[] args){
input();
}
private static void input(){
File f1 = new File("E:\\idea_workspace3\\yangli\\class_obj\\src\\com\\lili\\file\\lili.txt");
try {
Reader input = new FileReader(f1);
char[] chars = new char[1];
StringBuilder builder = new StringBuilder();
int len = -1;
while((len = input.read(chars))!=-1){
builder.append(new String(chars,0,len));
}
input.close();
System.out.println(builder);
} catch (IOException e) {
e.printStackTrace();
}
}
}
案例:复制一张图片到桌面上:
分析:图片传输,我们应该使用字节输入输出流,读取后再输出到桌面上即可
代码实现:
public class Test5 {
public static void main(String[] args) {
// 将哪里的文件复制到哪里去
copy("C:\\Users\\qijingjing\\Pictures\\Saved Pictures\\wife\\m5.jpg", "C:\\Users\\qijingjing\\Desktop\\m5.jpg");
}
private static void copy(String str, String target) {
// 需要被复制的文件
File file1 = new File(str);
// 复制文件到何地
File file2 = new File(target);
InputStream in = null;
OutputStream out = null;
try {
// 创建一个输入流
in = new FileInputStream(file1);
// 创建一个输出流
out = new FileOutputStream(file2);
byte[] bytes = new byte[1024];
int len = -1;
while ((len = in.read(bytes)) != -1) {
// 输入
out.write(bytes, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
// 关闭流
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (out != null) {
try {
// 关闭流
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!