2013-10-03 3 views
1

인스턴스가 FileOutputStream이고 파일을 쓰고 싶습니다.FileOutputStream이 파일을 쓰는 위치를 아는 방법?

실행 후 내 코드 파일 시스템에서 파일을 찾을 수 없습니다.

FileOutputStream 내가 쓸 곳을 알 수있는 방법이 있습니까?

+0

[FileOutputStream에서 파일 이름 가져 오기] (0120-998-504) – Behe

+1

절대 경로를 지정하지 않으면 상대 경로가됩니다. 현재 작업 디렉토리로 이동합니다. 그것이 어디 있는지 모른다면, 절대 경로를 사용하거나'System.out.println (새 파일 ("."). getAbsolutePath())'; – Thilo

답변

6

생성자를 호출 할 때 파일을 저장할 위치를 결정합니다.

new FileOutputStream("path/to/my/file.txt"); 

몇 가지 유사한 생성자가 있습니다. File 또는 FileDescriptor 매개 변수도 전달할 수 있습니다. Java API Doc를 읽으십시오. 당신이 FileOutputStream 인스턴스를 생성 할 때

http://docs.oracle.com/javase/7/docs/api/java/io/FileOutputStream.html

2

, 당신은 File 객체 나 String path (파일 이름이 절대 경로) 또는 FileDescriptor 개체 중 하나를 준 것입니다. 이 경로는 파일이 저장되는 경로입니다.

FileOutputStream의 다양한 생성자를보고 어떤 것이 사용되었는지 확인하십시오.

+0

또는 FileDescriptor –

+0

@ MichaëlBenjaminSaerens - 그래, 이미 그 대답에 언급! :) – SudoRahul

+0

당신은 편집했거나 그 중 하나를 놓쳤습니다 : P –

0

FileOutputStream 객체의 contructor에는 다른 매개 변수가 사용됩니다. 그 중 하나는 파일에 대한 경로 문자열입니다. 다른 하나는 File 객체입니다.이 경우 File 객체를 만들 때 파일에 대한 경로를 정의했습니다.

1
FileOutputStream(File file) 
Creates a file output stream to write to the file represented by the specified File object. 
FileOutputStream(File file, boolean append) 
Creates a file output stream to write to the file represented by the specified File object. 
FileOutputStream(FileDescriptor fdObj) 
Creates a file output stream to write to the specified file descriptor, which represents an existing connection to an actual file in the file system. 
FileOutputStream(String name) 
Creates a file output stream to write to the file with the specified name. 
FileOutputStream(String name, boolean append) 
Creates a file output stream to write to the file with the specified name. 

오버로드 된 모든 생성자는 filename을 사용합니다. 파일이 절대 경로에 존재하지 않으면 새로운 경로가 생성됩니다. 절대 경로가 제공되지 않으면 파일은 현재 디렉토리에 저장됩니다.

관련 문제