자바-IO 흐름 실험

6193 단어 JavaIO 흐름
머리말
프로젝트 구 조 는 다음 과 같 습 니 다.코드 를 사용 할 때 가방 이름과 클래스 로 수정 하 는 것 에 주의 하 십시오.
在这里插入图片描述
자원 관리자
[1].제목
지정 한 디 렉 터 리 아래 의 모든 파일 과 폴 더 를 표시 할 자원 관리자 클래스 를 설계 합 니 다.
  • 명령 행 에서 경 로 를 입력 하고 디 렉 터 리 가 아니라면 출력 알림 정보
  • 디 렉 터 리 가 존재 하고 존재 한다 면 이 디 렉 터 리 에 있 는 모든 파일 과 폴 더 의 이름 을 표시 합 니 다
  • 4.567917.존재 하지 않 으 면 출력 에 이 디 렉 터 리 가 존재 하지 않 습 니 다.4.567918.
    [2].실례
    在这里插入图片描述
    在这里插入图片描述
    [3].코드
    
    package p1;
    import java.util.*;
    import java.io.*;
    public class Main {
    	public static void main(String[] args) {
    		Scanner scanner = new Scanner(System.in);
    		System.out.println("     :");
    		String path = scanner.next();
    		File file = new File(path);
    		if(file.isDirectory()) {//      
    			if(!file.exists()) {//      
    				System.out.println("      !");
    			}else {
    				System.out.println("          :");
    				String[] sub = file.list();//           、   
    				for(String i:sub) {//  
    					System.out.println(i);
    				}
    			}
    		}else {//     
    			System.out.println("       !");
    		}
    	}
    }
    
    2.파일 복사 와 편집
    [1].제목
    파일 작업 클래스(FileOperation)를 만 듭 니 다.복사 와 편집 두 가지 방법 이 있 습 니 다.요구:
    소스 경로 와 목표 경 로 는 콘 솔 에서 입력 합 니 다4.567917.정태 적 인 방법 을 사용한다.4.567918.
    [2].복사
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    [3].잘라 내기
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    [4].코드
    
    package p2;
    import java.util.*;
    import java.io.*;
    public class Main {
    	public static void main(String[] args)  throws Exception{
    		Scanner scanner = new Scanner(System.in);
    		System.out.println("1.   ");
    		System.out.println("2.   ");
    		System.out.println("   : ");
    		int choose = scanner.nextInt();
    		System.out.println("   : ");
    		String resource = scanner.next();
    		System.out.println("    : ");
    		String target = scanner.next();
    		if(choose==1) {//  
    			FileOperation.copy(resource,target);
    		}else {//  
    			FileOperation.cut(resource, target);
    		}
    		System.out.println("    ");
    	}
    }
    class FileOperation{//     
    	public static void copy(String resource,String target)  throws Exception{//    
    		//     
    		File file_resource = new File(resource);//   
    		File file_target = new File(target);//    
    		//         
    		FileInputStream fis = new FileInputStream(file_resource);//   ,    
    		FileOutputStream fos = new FileOutputStream(file_target);//   ,    
    		//      
    		byte[] buff_resource = new byte[(int) file_resource.length()];
    		fis.read(buff_resource);//      
    		//      
    		String str = new String(buff_resource);
    		byte[] buff_target = str.getBytes();
    		fos.write(buff_target);//      
    		//     
    		fis.close();
    		fos.close();
    	}
    	public static void cut(String resource,String target)  throws Exception{//    
    		//     
    		//File file_delete = new File(resource);//   
    		File file_resource = new File(resource);//   
    		File file_target = new File(target);//    
    		//         
    		FileInputStream fis = new FileInputStream(file_resource);//   ,    
    		FileOutputStream fos = new FileOutputStream(file_target);//   ,    
    		//      
    		byte[] buff_resource = new byte[(int) file_resource.length()];
    		fis.read(buff_resource);//      
    		//      
    		String str = new String(buff_resource);
    		byte[] buff_target = str.getBytes();
    		fos.write(buff_target);//      
    		//     
    		fis.close();
    		fos.close();
    		//     ,     fis            
    		file_resource.delete();
    	}
    }
    
    3.파일 데이터 읽 기와 쓰기
    [1].제목
    "2018 FIFA World Cup will play in Russia."를 D:\data.txt 파일 에 기록 한 다음 이 파일 에서 모든 내용 을 읽 고 콘 솔 에 표시 합 니 다.
    [2].실례
    在这里插入图片描述
    在这里插入图片描述
    [3].코드
    
    package p3;
    import java.io.*;
    public class Main {
    	public static void main(String[] args) throws IOException{
    		String str = "2018 FIFA World Cup will play in Russia.";
    		Write("D:\\data.txt",str);
    		System.out.println(Read("D:\\data.txt"));
    	}
    	public static String Read(String filename) throws IOException{//    
    		File file = new File(filename);
    		FileInputStream fis = new FileInputStream(file);//   ,    
    		byte[] buff = new byte[(int) file.length()];
    		fis.read(buff);//    
    		String str = new String(buff);
    		fis.close();
    		return str;
    	}
    	public static void Write(String filename,String str) throws IOException{//    
    		File file = new File(filename);
    		FileOutputStream fos = new FileOutputStream(file);//   ,    
    		byte[] buff = str.getBytes();
    		fos.write(buff);//    
    		fos.close();
    	}
    }
    
    총결산
    이 글 은 여기까지 입 니 다.당신 에 게 도움 을 줄 수 있 기 를 바 랍 니 다.또한 당신 이 우리 의 더 많은 내용 에 관심 을 가 져 주 실 수 있 기 를 바 랍 니 다!

    좋은 웹페이지 즐겨찾기