>Java >java지도 시간 >SD 카드의 특정 폴더에 파일을 쓰는 방법은 무엇입니까?

SD 카드의 특정 폴더에 파일을 쓰는 방법은 무엇입니까?

Patricia Arquette
Patricia Arquette원래의
2024-12-05 20:54:11998검색

How to Write Files to a Specific Folder on the SD Card?

SD 카드의 폴더에 쓰기

현재 코드는 Environment.getExternalStorageDirectory()를 사용하여 SD 카드의 루트 디렉터리에 파일을 저장합니다. SD 카드. 특정 폴더에 쓰려면 다음 단계를 따르세요.

  1. SD 카드의 절대 경로 가져오기:

    File sdCard = Environment.getExternalStorageDirectory();
  2. 원하는 만들기 폴더:

    File dir = new File (sdCard.getAbsolutePath() + "/myapplication/downloads");
    dir.mkdirs(); // Creates non-existent directories
  3. 폴더 내에 파일 만들기:

    File file = new File(dir, "myfile.txt");
  4. 파일 출력 스트림을 열고 데이터를 file:

    FileOutputStream f = new FileOutputStream(file);
    f.write(...);
    f.close();

예제 코드:

File sdCard = Environment.getExternalStorageDirectory();
File dir = new File (sdCard.getAbsolutePath() + "/myapp/downloads");
dir.mkdirs();
File file = new File(dir, "file.txt");

FileOutputStream f = new FileOutputStream(file);
f.write("Hello world!".getBytes());
f.close();

이 접근 방식을 사용하면 이제 특정 위치에 파일을 쓸 수 있습니다. 폴더에 저장하세요.

위 내용은 SD 카드의 특정 폴더에 파일을 쓰는 방법은 무엇입니까?의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.