• 文件操作


    package 文件操作;
    
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.io.PrintStream;
    import java.util.Scanner;
    
    public class FileOption {
    	String filename = "C:\Users\Administrator\Desktop\新建文件.txt";
    	String inputname;
    	int selectnum = 0;
    	public FileOption() {
    		Scanner scanner = new Scanner(System.in);
    		System.out.println("请输入文件地址:");
    		inputname = scanner.nextLine();
    		
    		System.out.println("请输入你的选择:"
    				+ "1:创建文件"
    				+ "2:删除文件"
    				+ "3:读取文件"
    				+ "4:写入文件");
    		selectnum = scanner.nextInt();
    		while(true) {
    			
    			switch (selectnum) {
    			case 1:
    				CreateFile(inputname);
    				selectnum = scanner.nextInt();
    				break;
    			case 2:
    				DeleteFile(inputname);
    				selectnum = scanner.nextInt();
    				break;
    			case 3:
    				ReadFile(inputname);
    				selectnum = scanner.nextInt();
    				break;	 
    			case 4:
    				WriteFile(inputname);
    				selectnum = scanner.nextInt();
    				break;
    			default:
    				break;
    			}
    			
    		}
    		
    //		DeleteFile(filename);
    //		ReadFile(filename);
    	}
    	private void CreateFile(String Filename) {
    		File file = new File(Filename);	
    		try {
    			if(!file.exists()) {
    				file.createNewFile();
    			}
    		} catch (IOException e) {
    			// TODO 自动生成的 catch 块
    			e.printStackTrace();
    		}
    	
    	}
    	private void DeleteFile(String DeleteFile) {
    		File file = new File(DeleteFile);
    		if(file.exists()) {
    			file.delete();			
    		}
    	}
    	private String ReadFile(String ReadFile) {
    		File file = new File(ReadFile);
    		String data = null;
    		char buff[] = new char[1024];
    		try {
    			FileReader fileReader = new FileReader(file);
    			fileReader.read(buff);
    			data = new String(buff);
    			System.out.println("读取到:"+data);			
    		} catch (IOException e) {
    			// TODO 自动生成的 catch 块
    			e.printStackTrace();
    		}
    		return  data;		
    	}
    	private void WriteFile(String Writedata) {
    		
    		File file = new File(Writedata);
    		try {
    			PrintStream ps = new PrintStream(file);
    			ps.append("我是新写入的文件内容 ");
    		} catch (FileNotFoundException e) {
    			// TODO 自动生成的 catch 块
    			e.printStackTrace();
    		}
    		
    	}
    }
    //主函数部分
    package 文件操作;
    
    public class Main {
    
    	public static void main(String[] args) {
    		// TODO 自动生成的方法存根
    		new FileOption();
    	}
    
    }
    

      

    
    

      

    归去来兮
  • 相关阅读:
    使用ngx_lua构建高并发应用(1)
    nginx+lua项目学习
    学习乱
    if---(switch-case)语句初步学习总结
    数据类型转换
    总结:C#变量,占位符等相关知识
    学习随笔
    开始我的.NET的学习旅程
    Python 网络爬虫 008 (编程) 通过ID索引号遍历目标网页里链接的所有网页
    Python 网络爬虫 007 (编程) 通过网站地图爬取目标站点的所有网页
  • 原文地址:https://www.cnblogs.com/zoute/p/9470505.html
Copyright © 2020-2023  润新知