2012-03-01 3 views
0

나는 아래 코드를 시도했다 !! "textfile.txt"파일의 내용을 대체 할 때마다 사용자가 "filename"으로 입력 한 이름으로 SD 카드에 파일을 추가하길 원한다. 저장 버튼에 onclick 수신기를 등록했다. 코드 :SD 카드에 두 개 이상의 파일을 만드는 방법은 무엇입니까?

  EditText filename =(EditText) findViewById(R.id.filename); 

      EditText filecontent =(EditText) findViewById(R.id.filecontent); 

       public void onClick(View view) 

         { 

         String str = filename.getText().toString(); 
      String str2= filecontent.getText().toString(); 


        file sdCard = Environment.getExternalStorageDirectory(); 
      File directory = new File (sdCard.getAbsolutePath() +"/MyFiles"); 
      directory.mkdirs(); 



        File file = new File(directory,"textfile.txt"); 
        file.createNewFile(); 


        FileOutputStream fOut = new FileOutputStream(file); 
      OutputStreamWriter osw = new OutputStreamWriter(fOut); 


        osw.write(str); 
      osw.write(" "); 
      osw.write(str2); 
      osw.flush(); 
      osw.close(); 
        } 

답변

0

변화 그것에 파일 파일 = 새로운 파일 (디렉토리, STR + ". TXT"); 사용자가 부여한 이름이 저장됩니다 !!!

0

그럼 각 파일에 고유 한 이름을 지정해야합니다. "textfile.txt" File file = new File(directory,"textfile.txt"); 내부 filename

변화 : 내가 제대로 질문을 이해하면

+0

사용자가 텍스트 상자에 파일 이름을 지정하면 SD 카드에 저장된 파일의 파일 이름이됩니다 !!! – swapna

1

, 여기 당신의 솔루션입니다.

최종 결과 shouldlook : File file = new File(directory,filename);

+0

코드에 대한 오류가 표시됩니다. "생성자 파일 (File, EditText)이 정의되지 않았습니다." – swapna

관련 문제