字节输出流写入数据到文件和字符输出流写多个字节的方法·
字节输出流写入数据到文件
package com.yang.Test.IOStudy;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* 写入数据的原理(内存-->硬盘)
* java程序-->JVM(java虚拟机)-->OS(操作系统)-->OS调用写数据的方法-->吧数据写入到文件中
*
* 字节输出流的使用步骤:
* 1.常见一个FileOutputStream对象,构造方法中传递写入数据的目的地
* 2.调用FileOutputStrean对象中的方法write,吧数据写入到文件中
* 3.释放资源(流使用会占用一定的内存,使用完毕要把内存清空,提供程序的效率)
*/
public class OutStudy01 {
public static void main(String[] args) throws IOException {
FileOutputStream fos = new FileOutputStream("Document/1.txt");
fos.write(97);
fos.close();
}
}
字符输出流写多个字节的方法·
代码:
package com.yang.Test.IOStudy;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class OutStudy01 {
public static void main(String[] args) throws IOException {
FileOutputStream fos = new FileOutputStream(new File("Document\\2.txt"));
byte[] b = {97,98,99,100,97};
fos.write(b);
// byte[] bytes = "你好哈哈哈啦啦啦啦".getBytes();
// fos.write(bytes,0,bytes.length);
// fos.write("啦啦啦啦啦啦".getBytes());
fos.close();
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)