2012-11-10 4 views
0

안드로이드 장치 안의 텍스트 파일에 데이터를 쓰려면 어떻게해야합니까? 현재 저는이 데이터를 json 형식으로 쓰는 서버에 보내고 있습니다. 그러나 내 프로그램이 Android에 직접 데이터를 쓰길 원합니다.안드로이드 안의 데이터 쓰기

센서가 값을 변경할 때마다 센서 값을 기록하고 싶습니다. 다음 코드가 있습니다. 주석 대신 무엇을 넣어야합니까?

public void onSensorChanged(SensorEvent event) 
{ 
    switch(event.sensor.getType()) 
    { 
     case Sensor.TYPE_LINEAR_ACCELERATION: 
//writing event.values[0], event.values[1] and event.values[2] to result.txt 
     break; 
     .......... 
    } 
} 
+0

아직 시도한 것은 무엇입니까? 그런데 디버깅을 제외한 센서의 모든 상태를 저장하는 것은 나쁜 생각입니다. 그 데이터를 저장하는 데 많은 시간과 노력을 낭비합니다. – rekire

+0

안녕하세요, 그러나 "result.txt"를 찾을 수 있습니다. FileOutputStream fOut; 문자열 fileLocation = "result.txt"; 문자열 TESTSTRING = 새로운 문자열 (sensordata); fOut = openFileOutput (fileLocation, MODE_WORLD_READABLE); \t \t \t \t \t \t fOut.write (TESTSTRING.getBytes()); fOut.close(); –

+0

테스트 용입니다. 그런데 빠른 응답에 감사드립니다. –

답변

0
String strContent = "Write File using Java FileOutputStream example !"; 
FileOutputStream fileOut = openFileOutput(outputFile, MODE_APPEND); 
OutputStreamWriter osw = new OutputStreamWriter(fileOut); 
osw.writeBytes(strContent.getBytes()); 
osw.flush(); 

기타 코드 :

File logFile = new File("sdcard/log.file"); 
    if (!logFile.exists()) 
    { 
     try 
     { 
     logFile.createNewFile(); 
     } 
     catch (IOException e) 
     { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
     } 
    } 
    try 
    { 
     //BufferedWriter for performance, true to set append to file flag 
     BufferedWriter buf = new BufferedWriter(new FileWriter(logFile, true)); 
     buf.append(text); 
     buf.newLine(); 
     buf.close(); 
    } 
    catch (IOException e) 
    { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

내가 파일 매번 sensot 변경 쓸 것이 아니라 단지 관련 데이터와 문자열을 구축하고 마지막에 파일에 작성합니다 프로세스의

+0

문자열에 데이터를 사용할 수 있습니까? (데이터 대신 "myData"를 사용하려고하면 오류가 발생합니다.) 파일이 저장되는 위치는 어디입니까? 감사합니다. –

+0

답변 됨 edited. 3 개의 휴대 전화와 2 개의 태블릿에서 작동합니다. –

관련 문제