Java学习
IO流
File类
1. java.mkdir 不可以创建多层文件夹
java.mkdirs 可以创建多层文件夹
File file1=new File("G:\\MyCode")
2. 相对路径 与 绝对路径
file.getPath()
file.getAbusolutePath();
3 获取文件夹下的所有PNG文件
File fileTest=new File("C:\\Users\\han\\Pictures\\图片数据库");
String[] strings=fileTest.list();
for(String s:strings){
if(s.endsWith(".PNG")){
System.out.println(s);
}
}
File[] listFile=fileTest.listFiles();
for (File file:listFile){
String name=file.getName();
if(name.endsWith(".PNG"))
{
System.out.println(file.getAbsolutePath());
}
}
IO流
1 输入输出流(相对于本程序来说的)
输入(read)
输出(write)
2 内容
字节 图片 音频 视频 字节数据(解码)
字节输出流:OutputStream
字节输入流:InputStream
字符 abcd
输入 Reader
输出 Writer
3 输出一个"hello"到文件 用字符输出流
Writer
4 FileOutputStream
1)FileOutputStream(File file)
创建文件输出流以写入由指定的 File对象表示的文件。
2)FileOutputStream(File file, boolean append)
创建文件输出流以写入由指定的 File对象表示的文件。
3)FileOutputStream(FileDescriptor fdObj)
创建文件输出流以写入指定的文件描述符,表示与文件系统中实际文件的现有连接。
4)FileOutputStream(String name)
创建文件输出流以指定的名称写入文件。
5)FileOutputStream(String name, boolean append)
创建文件输出流以指定的名称写入文件。
public class IOtest {
//创建刘对象
public static void main(String[] args) throws Exception {
File file;
//创建文件输出流
FileOutputStream fos=new FileOutputStream("testIO.txt");
String str="我是韩文硕";
//转换为数据字节流
byte[] bytes=str.getBytes();
//输出病关闭
fos.write(bytes);
fos.close();
}
}
5 read一次读一个 会出现中文乱码
6 bufferedInputStream
搞一辆车 一次拉好多
public class testBufferedInputStream {
public static void main(String[] args) {
//0.水龙头工厂 将fileinputstream转化为inputstream
InputStream in= null;
try {
in = new FileInputStream("testIO.txt");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
//1.创建水龙头 形参是inputstream
BufferedInputStream bis=new BufferedInputStream(in);
//2.开水龙头 创建车辆 每次装1024瓶水 最后一车并不会装满
byte[] car=new byte[1024];
int len=0;
try {
while((len=bis.read(car))!=-1){
System.out.println(len);
}
bis.close();
in.close();
} catch (IOException e) {
e.printStackTrace();
}
//关龙头
try {
OutputStream os=new FileOutputStream("testIO.txt");
BufferedOutputStream bos=new BufferedOutputStream(os);
String s="我是韩文硕我是韩文硕我是韩文硕我是韩文硕我是韩文硕我是韩文硕我是韩文硕我是韩文硕我是韩文硕" +
"我是韩文硕我是韩文硕我是韩文硕我是韩文硕我是韩文硕我是韩文硕我是韩文硕我是韩文硕我是韩文硕我是韩文硕" +
"我是韩文硕我是韩文硕我是韩文硕我是韩文硕我是韩文硕我是韩文硕";
bos.write(s.getBytes());
//字符串直接使用bos.write(s.getBytes());
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?