Java文件操作实例详解
作者:宝贝垚 发布时间:2023-11-25 10:29:40
标签:java,文件操作
本文实例为大家分享了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


猜你喜欢
- 数据绑定API用于JSON转换和使用属性访问或使用注解POJO(普通Java对象)。以下是它的两个类型。简单数据绑定 - 转换JSON,从J
- 1、对属性进行封装,使用户不能直接输入数据,我们需要避免用户再使用"对象.属性"的方式对属性进行赋值。则需要将属性声明为
- 数组的用处是什么呢?——当你需要将30个数进行大小排列的时候,用数组这样的数据结构存储是个很好的选择,当你是一个班的班主任的时候,每次要记录
- ADO.NET数据访问技术的一个突出特点就是支持离线访问,而实现这种离线访问的技术核心急速DataSet对象,该对象通过数据驻留在内存来实现
- 本文实例讲述了C#利用Windows自带gdi32.dll实现抓取屏幕功能,是C#应用程序设计中一个非常实用的功能,现分享给大家供大家参考借
- 前言在我们做后端服务Dao层开发,特别是大数据批量插入的时候,这时候普通的ORM框架(Mybatis、hibernate、JPA)就无法满足
- 《Android开发艺术探索》笔记:在Activity的onCreate()或者onResume()中去获得View的高度的时候不能正确获得
- 前言:我们都知道Android开发者的必备工具:AndroidStudio。是开发人员用来编译、测试的专用工具。今天在使用And
- 本文介绍了AndroidStudio3.0开发调试安卓NDK的C++代码,分享给大家,具有如下:一、新建项目新建项目,没有发现Include
- 1. 是否需要整合 ?不需要 : 单独使用Springmvc. 需要将原先Spring中的内容通通迁移到Springmvc中. 例如:数据源
- 动态规划的基本思想是将待求解问题分解成若干个子问题,先求解子问题,并将这些子问题的解保存起来,如果以后在求解较大子问题的时候需要用到这些子问
- 本文以实例形式详细讲述了dotnetcharting控件的用法。分享给大家供大家参考。具体用法分析如下:dotnetcharting 是一个
- 引言综合应用Java的GUI编程和网络编程,实现一个能够支持多组用户同时使用的聊天室软件。该聊天室具有比较友好的GUI界面,并使用C/S模式
- RESTful 一种软件架构风格,设计风格而不是标准,只是提供了一组设计原则和约束条件。它主要用于客户端和服务器交互类的软件。基于这个风格设
- 实现刮刮卡我们可以Get到哪些技能?* 圆形圆角图片的实现原理* 双缓冲技术绘图* Bitmap获取像素值数据* 获取绘制文本的长宽* 自定
- Swagger以及knife4j基本使用Swagger 介绍:官网:https://swagger.io/Swagger是一个规范和完整的框
- 请求转发的三种方式SpringMVC请求转发区别于重定向,请求转发地址栏不会发生改变、只发送一次请求、能携带原有的参数,但只可以在同一个服务
- 一、引言在移动应用程序的架构设计中,界面与数据即不可分割又不可混淆。在绝大部分的开发经历中,我们都是使用Fragment来进行界面编程,即使
- 测试参数设置:1、循环调用new A()实现堆溢出,java.lang.OutOfMemoryError: Java heap space,
- 本文实例为大家分享了swing登录注册界面展示的具体代码,供大家参考,具体内容如下开发环境:Eclipse Neon.3 Release (