IO: FileInputStream 및 FileOutputStream
2530 단어 F#
FileInputStream 클래스를 만드는 디스크 파일의 Stream 객체에는 다음 두 가지 방법이 있습니다.
(1)FileInputStream fis = new FileInputStream("test.txt");
(2)File f = new File("test.txt");
FileInputStream fis = new FileInputStream(f);
두 번째는test 파일에 대해 많은 File 클래스를 조작할 수 있다. 예를 들어 존재 여부, 쓰기 가능 여부 등이다.이것은 File 그 문장의 내용이다.
FileOutputStream 인스턴스 객체를 만들 때는 아직 존재하지 않는 파일 이름을 지정할 수 있지만 다른 프로그램에서 열 파일은 지정할 수 없습니다.
다음은 File OutputStream 클래스로 파일에 문자열을 쓴 다음 File InputStream으로 쓴 내용을 읽습니다.
import java.io.*;
public class FileStream {
public static void main(String[] args) throws Exception{
FileOutputStream out = new FileOutputStream("test.txt");
out.write("www.cublog.com".getBytes());// ,write() ,
out.close();//
byte[] buf = new byte[1024];
File f = new File("test.txt");
FileInputStream in = new FileInputStream(f);
int len = in.read(buf);//
System.out.println(new String(buf,0,len));
in.close();
}
}
Reader 클래스 및 Writer 클래스:
java의 문자는 유니코드로 인코딩된 두 바이트입니다.위의 File Input Stream과 File Output Stream은 모두 바이트를 처리하는 데 사용되며, 위의 문자열을 처리할 때 문자열을 바이트로 변환한 다음에 파일에 쓰고, 문자열을 읽을 때도 먼저 읽은 바이트 그룹을 문자열로 변환합니다.이것은 추가 바이트와 문자 사이의 변환 코드를 작성해야 한다.자바에는 입출력 장치에 문자를 입력하고 출력하는 데 사용되는 단독 클래스가 있습니다. 예를 들어 아까의 예는 FileReader와 FileWriter를 사용할 수 있습니다.
Reader 클래스와 Writer 클래스는 텍스트 파일을 읽는 데 사용되며 InputStream과 OutputStream은 이진 형식의 파일의 내용을 읽는 데 사용됩니다
위의 예는 텍스트 파일이므로 FileReader 및 FileWriter 클래스를 사용하여 다음과 같이 재정의할 수 있습니다.
import java.io.*;
public class FileStream {
public static void main(String[] args) throws Exception{
FileWriter out = new FileWriter("test.txt");
out.write("www.cublog.com");
out.close();
char[] buf = new char[1024];
FileReader in = new FileReader("test.txt");
int len = in.read(buf);
System.out.println(new String(buf,0,len));
in.close();
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
F#에서 Android 앱 개발 Helloworld 편F#에서 android 앱 개발의 기본이되는 Helloworld를 출력하는 방법을 소개합니다. 먼저 Visual Studio 2019를 시작합니다. [새 프로젝트 만들기(N)]→[빈 앱(Android)]을 선택합니다...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.