IO流
1.创建文件夹
public static void main(String[] args) {
File file = new File("/Users/admin");
if (!file.exists()){
boolean result = file.mkdir();
System.out.println(result);
}
}
2.修改文件夹名称
public static void main(String[] args) {
File file = new File("/Users/admin");
if (file.exists()){
boolean result = file.renameTo(new File("/Users/admins"));
System.out.println(result);
}else {
System.out.println(false);
}
}
3.删除文件夹
如果文件夹的目录下还有其他文件即文件夹不为空,就不能删除文件夹
public static void main(String[] args) {
File file = new File("/Users/admin");
if (file.exists()){
boolean delete = file.delete();
System.out.println(delete);
}
}
4.删除文件夹里面有文件的文件夹
file.isDirect() 判断是不是一个目录
public static boolean recursionDelete(File file){
if (file.isDirectory() && file.exists()){
File[] files = file.listFiles();
if (files != null) {
for (File f : files) {
recursionDelete(f);
}
}
}
return file.delete();
}
5.字符是如何存在电脑上的
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-nHwqYSZx-1658109314844)(IO.assets/image-20220713114406914.png)]
字节输入流
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-oJhigz0A-1658109314846)(IO.assets/image-20220713114519062.png)]
1.FileInputStream 文件字节输入流 ,
1.可以读取文本,图片,音乐,视频等文件数据
//传入文件路径
public FileInputStream(String name){
}
//表示文件的file对象
public FileInputStream(File file){
}
//传入文件描述
public FileInputStream(FileDescriptor fdObj){
}
2.read()方法每次读取一个字节 返回 一个值
public class TestFileInputStream {
public static void main(String[] args) {
try {
FileInputStream in = new FileInputStream("/Users/admin/IO.txt");
int b;
while ((b=in.read())!= -1){
System.out.print((char)b);
}
// System.out.println((char) in.read());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-8ybFA7aN-1658109314846)(IO.assets/image-20220713141814784.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-y7lZoPdl-1658109314847)(IO.assets/image-20220713142012828.png)]
3.关闭流
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-bydWCdZQ-1658109314847)(IO.assets/image-20220713142150049.png)]
public class TestFileInputStream {
public static void main(String[] args) {
FileInputStream in = null;
try {
in = new FileInputStream("/Users/admin/IO.txt");
int b;
while ((b = in.read()) != -1) {
// 输出字节
System.out.print((char) b);
}
//System.out.println((char) in.read());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
//当流不为空的时候
if (in != null) {
try {
//关闭流
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
具体流程
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-8jS0n2Yv-1658109314848)(IO.assets/image-20220713142605731.png)]
4.读取多个字节
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-1rOfbPQQ-1658109314848)(IO.assets/image-20220713142812206.png)]
public class TestFileInputStream {
public static void main(String[] args) {
FileInputStream in = null;
try {
//初始化字节流
in = new FileInputStream("/Users/admin/IO.txt");
//创建字节数组
byte[] bytes = new byte[3];
//读取的字节数
int length;
while ((length=in.read(bytes))!=-1){
System.out.print(new String(bytes,0,length));
}
// int b;
// while ((b = in.read()) != -1) {
// // 输出字节
// System.out.print((char) b);
// }
//System.out.println((char) in.read());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
//当流不为空的时候
if (in != null) {
try {
//关闭流
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
OutputStream 字节输出流
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-02nxdCHe-1658109314849)(IO.assets/image-20220713143917542.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Y4u6Fh8f-1658109314849)(IO.assets/image-20220713144006149.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-FTfwTjoP-1658109314849)(IO.assets/image-20220713144047257.png)]
1.构造方法
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-DzcVVkMu-1658109314850)(IO.assets/image-20220713144125290.png)]
输出过程 现根据编码表找到对应的十进制码值,再将码值 转化为字节 再调用输出流的write()方法依次写入文件中 1.每次写入单个字节 2.每次写入一个字节数组。
public class OutputStreamTest {
public static void main(String[] args) {
//文件字节输出流
FileOutputStream out = null;
//初始化文件字节输出流
try {
out = new FileOutputStream("/Users/admin/OutputStream.txt");
//创建要写入的数据
String str = "abcdefgh";
//转化为字节数组
byte[] bytes = str.getBytes();
//将数据输入到文件中
out.write(bytes);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
//当流补位空
if (out != null){
try {
//关闭流
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
public class OutputStreamTest {
public static void main(String[] args) {
//文件字节输出流
FileOutputStream out = null;
//初始化文件字节输出流
try {
File file = new File("/Users/admin/OutputStream.txt");
if (!file.exists()){
file.createNewFile();
}
out = new FileOutputStream(file);
//创建要写入的数据
String str = "abcdefgh22";
//转化为字节数组
byte[] bytes = str.getBytes();
//将数据输入到文件中
out.write(bytes);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
//当流补位空
if (out != null){
try {
//关闭流
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
2.方法说明
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-JXEmkjgH-1658109314850)(IO.assets/image-20220713145729777.png)]
3.指定写入方式
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-5WX5wUXr-1658109314850)(IO.assets/image-20220713150039060.png)]
4.append true:追加 false:覆盖无论你有没有写入数据只要执行方法原有的数据都会被清除
没带 append 的两个构造方法是内部调用 有append的方法 其中 append 默认为false; 即为覆盖 无论你有没有写入数据只要执行方法原有的数据都会被清除
追加模式
public class OutputStreamTest {
public static void main(String[] args) {
//文件字节输出流
FileOutputStream out = null;
//初始化文件字节输出流
try {
File file = new File("/Users/admin/OutputStream.txt");
if (!file.exists()){
file.createNewFile();
}
//开启追加模式
out = new FileOutputStream(file,true);
//创建要写入的数据
String str = "你好呀";
//转化为字节数组
byte[] bytes = str.getBytes();
//将数据输入到文件中
out.write(bytes);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
//当流补位空
if (out != null){
try {
//关闭流
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-OORSgifp-1658109314851)(IO.assets/image-20220713151131893.png)]
5.复制文件
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-jf0M3s48-1658109314851)(IO.assets/image-20220713151234348.png)]
边读边写 读到-1 的时候结束
public class TestInAndOut {
public static void main(String[] args) {
//输入流
InputStream in = null;
//输出流
OutputStream out= null;
try {
//初始化输入流
in = new FileInputStream("/Users/admin/IO.txt");
//初始化输出流
out = new FileOutputStream("/Users/admin/OutputStream.txt");
byte[] bytes = new byte[1024];
//定义读取字节长度
int length;
while ((length = in.read(bytes)) != -1){
// 写出长度0-length
out.write(bytes,0,length);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} 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();
}
}
}
}
}
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-wEdYWSIz-1658109314851)(IO.assets/image-20220713154704557.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-nVG87lB0-1658109314852)(IO.assets/image-20220713155905861.png)]
6.创建未知文件
File file = new File("/Users/admin/java/OutputStream.txt");
if (!file.exists()){
//创建目录
if (!file.getParentFile().mkdirs()){
System.out.println("目录创建失败");
return;
}
}
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-lwlndBbK-1658109314852)(IO.assets/image-20220713171043389.png)]
7.UTF-8
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-MFJTA0FF-1658109314852)(IO.assets/image-20220713172109913.png)]
以字符为单位读取数据
字符占几个字节,就连续读几个字节。
1.字符输入流
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-f5AMXjSH-1658109314852)(IO.assets/image-20220713172403323.png)]
构造方法
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-HmXzRJlj-1658109314853)(IO.assets/image-20220713172452180.png)]
public class FileReaders {
public static void main(String[] args) {
//声明字符输入流
FileReader fileReader = null;
try {
fileReader = new FileReader("/Users/admin/IO.txt");
byte[] bytes = new byte[4];
int b;
while ((b=fileReader.read()) != -1){
System.out.print((char) b);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fileReader!=null){
try {
fileReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-iP5Bmbze-1658109314853)(IO.assets/image-20220713173309222.png)]
2. 写入数据 字符写入
3.字符输出流 Writer
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ouJ06wUe-1658109314853)(IO.assets/image-20220713221643483.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-KPmGZGgj-1658109314854)(IO.assets/image-20220713221746702.png)]n’bnb
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-sE3fw5nK-1658109314854)(IO.assets/image-20220713222037139.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-mC4YJhtd-1658109314854)(IO.assets/image-20220713222254395.png)]