文件操作实例(Java)

2 篇文章 0 订阅
2 篇文章 0 订阅

简介

本程序主要采用了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();
			}
		}
	}
    
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值