자바-IO 흐름 실험
프로젝트 구 조 는 다음 과 같 습 니 다.코드 를 사용 할 때 가방 이름과 클래스 로 수정 하 는 것 에 주의 하 십시오.
자원 관리자
[1].제목
지정 한 디 렉 터 리 아래 의 모든 파일 과 폴 더 를 표시 할 자원 관리자 클래스 를 설계 합 니 다.
[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();
}
}
총결산이 글 은 여기까지 입 니 다.당신 에 게 도움 을 줄 수 있 기 를 바 랍 니 다.또한 당신 이 우리 의 더 많은 내용 에 관심 을 가 져 주 실 수 있 기 를 바 랍 니 다!
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
JPA + QueryDSL 계층형 댓글, 대댓글 구현(2)이번엔 전편에 이어서 계층형 댓글, 대댓글을 다시 리팩토링해볼 예정이다. 이전 게시글에서는 계층형 댓글, 대댓글을 구현은 되었지만 N+1 문제가 있었다. 이번에는 그 N+1 문제를 해결해 볼 것이다. 위의 로직은 이...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.