Java文件操作实例详解
作者:宝贝垚 时间:2023-11-25 10:29:40
本文实例为大家分享了Java文件操作的具体代码,供大家参考,具体内容如下
简介
本程序主要采用了FileInputStream和FileOutputStream两类对文件进行操作。具体包括通过相对路径打开文件,三种方法读取文件,查看文件属性,追加文件数据等。
效果图:
完整代码:
package Code.a;
import java.io.*;
public class FileInputStreamDemo {
public static void main(String[] args) {
//获取当前目录;
File f = new File(".");
System.out.print("absolute path:"+f.getAbsolutePath()+"\n");
while(true)
{
try {
//输入命令;
System.out.print("Please input your order:");
BufferedReader stdinBufferedReader;
String str1 = null;
stdinBufferedReader = new BufferedReader(new InputStreamReader(System.in));
str1 = stdinBufferedReader.readLine();
//相对路径打开文件;
File file2 = new File(".\\src\\Code\\a\\Exception.java");
FileInputStream fis2 = new FileInputStream(file2);
根据不同的命令,执行不同操作;
//一次性读取全部数据
if(str1.equals("一次性读取全部数据"))
{
byte[] buf = new byte[(int)(file2.length())];
fis2.read(buf);
String str = new String(buf);
System.out.print(str);
System.out.print("\n");
}
//分块读取
else if(str1.equals("分块读取"))
{
int n = 1024,count;
byte[] buf = new byte[n];
while((count = fis2.read(buf)) != -1)
{
System.out.print(new String(buf,0,count));
}
System.out.print("\n");
}
//逐字读取数据
else if(str1.equals("逐字读取数据"))
{
for(int i = 0; i < file2.length(); i++)
{
char ch = (char)(fis2.read());
System.out.print(ch);
}
System.out.print("\n");
}
//退出
else if(str1.equals("退出"))
{
System.out.print("已退出\n");
break;
}
//查看文件属性
else if(str1.equals("查看文件属性"))
{
System.out.print("If the file or catalog exists:"+file2.exists()+"\n");
System.out.print("If is it a file:"+file2.isFile()+"\n");
System.out.print("If is it a catalog:"+file2.isDirectory()+"\n");
System.out.print("FileName:"+file2.getName()+"\n");
System.out.print("absolute path:"+file2.getAbsolutePath()+"\n");
System.out.print("The last time that the file was changed:"+file2.lastModified()+"\n");
System.out.print("The size of the file:"+file2.length()+" bites\n");
}
//向文件追加数据
else if(str1.equals("文件追加数据"))
{
FileOutputStream fos2 = new FileOutputStream(file2,true);
System.out.println("Please input the content: ");
BufferedReader ContentReader;
String str2 = null;
ContentReader = new BufferedReader(new InputStreamReader(System.in));
str2 = ContentReader.readLine();
fos2.write(str2.getBytes());
fos2.close();
}
//关闭流对象;
fis2.close();
}
//处理异常;
catch(FileNotFoundException fnfe) {
System.out.print("The file open unsuccessfully.");
}catch(IOException ioe) {
ioe.printStackTrace();
}
}
}
}
来源:https://blog.csdn.net/qq_43533435/article/details/109483803
标签:java,文件操作
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android支付宝支付封装代码
2023-08-02 15:10:06
Java用BigDecimal类解决Double类型精度丢失的问题
2022-01-07 16:49:24
![](https://img.aspxhome.com/file/2023/6/124706_0s.png)
Java字符串操作和C#字符串操作的不同小结
2022-02-15 07:21:14
java实现图片缩放、旋转和马赛克化
2021-07-23 03:04:54
Java实现驼峰、下划线互转的方法
2023-08-18 09:17:54
SpringBoot全局配置long转String丢失精度的问题解决
2023-02-19 22:58:49
Android中访问证书有问题的SSL网页的方法
2023-11-20 10:22:00
Unity使用鼠标旋转物体效果
2021-10-17 05:08:12
![](https://img.aspxhome.com/file/2023/6/68616_0s.png)
聊聊@RequestBody和Json之间的关系
2023-11-27 03:31:45
java中将一个List等分成n个list的工具方法(推荐)
2022-04-12 09:25:09
Java 中ThreadLocal类详解
2022-01-31 19:58:17
Java由浅入深带你了解什么是包package
2022-04-17 02:33:39
![](https://img.aspxhome.com/file/2023/4/76594_0s.png)
maven打包如何指定jdk的版本
2022-12-21 20:59:20
![](https://img.aspxhome.com/file/2023/3/67683_0s.png)
Springboot实现阿里云通信短信服务有关短信验证码的发送功能
2022-07-24 15:36:53
![](https://img.aspxhome.com/file/2023/9/94229_0s.png)
使用JPA自定义VO类型转换(EntityUtils工具类)
2023-08-26 14:56:17
java实现滑动验证解锁
2023-06-02 12:16:36
![](https://img.aspxhome.com/file/2023/4/74894_0s.jpg)
Mybatis代码生成器Mybatis Generator(MBG)实战详解
2023-12-02 23:04:31
![](https://img.aspxhome.com/file/2023/1/73031_0s.jpg)
详解context root修改无效web修改项目路径(eclipse)
2022-08-04 13:08:46
![](https://img.aspxhome.com/file/2023/2/85902_0s.png)
Spring Mvc中传递参数方法之url/requestMapping详解
2021-11-03 05:48:46
Java并发编程中的生产者与消费者模型简述
2023-02-16 20:33:18