Java文件操作的方法
作者:归人云淡风轻
时间:2023-04-27
来源:互联网
浏览:0
简介本程序主要采用了FileInputStream和FileOutputStream两类对文件进行操作。具体包括通过相对路径打开文件,三种方法读取文件,查看文件属性,追加文件数据等。效果图:完整代码:packageCode.a;importjava.io.*;publicclassFileInputStreamDemo{publicstaticvoidmain(String[]args){//获取当前目录;Filef=newFile(".");System.out.print("absolutepath:
简介
本程序主要采用了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();
}
}
}
}
作者最新文章
荣耀MagicOS 11发布计划与Agent Harness架构解析
2026-09-08 19:23
AI重构企业业务架构:超聚变“智企”范式核心解析
2026-09-08 18:39
PDF合并工具怎么选?在线合并5步实操指南
2026-09-04 17:05
PDF图片压缩工具推荐与批量处理实操指南
2026-09-03 12:14
照片如何转成PDF格式?三种图片转PDF操作方法
2026-09-03 11:04
上一篇:
java线程池如何关闭
热门文章
更多
精品专题
更多
Mac软件
更多
WINDOWS
更多
Windows 10
Windows
Windows 10 是一款微软推出的经典操作系统,拥有硬件兼容性与多任务处理能力。它更偏向把系统状态查看和常用调节动作放在一起,适合需要持续观察和微调设备状态的场景。
极度公式
Windows/macOS/Linux
极度公式是一款跨平台专业LaTeX公式识别编辑软件,支持OCR公式识别和多平台编辑。和使用说明,避免使用,享受完整功能与稳定支持。做扫描整理、文字提取和表格转换时,它能把识别后的处理步骤接得更顺,资料录入这类场景会省下不少时间。
















