zoukankan      html  css  js  c++  java
  • 文件操作

    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();
    	}
    
    }
    

      

    
    

      

    归去来兮
  • 相关阅读:
    ubuntu16.04添加开机启动任务
    Elasticsearch-5.0.0移植到ubuntu16.04
    转:解决npm install慢的问题
    解决virtualbox装ghost xp装驱动时报portcls.sys蓝屏的问题
    git文件迁移到新架构
    ubuntu16.04文件形式安装mongodb
    linux启动流程
    启动WAMPSERVER出现计算机中丢失 MSVCR110.dll
    socket编程的网络协议
    PHP7的新特性
  • 原文地址:https://www.cnblogs.com/zoute/p/9470505.html
Copyright © 2011-2022 走看看