【问题标题】:How to add data to an existing .txt file in android如何将数据添加到 android 中的现有 .txt 文件
【发布时间】:2016-04-25 03:24:57
【问题描述】:

我正在开发一个从加速度计读取数据的应用程序,我想将它们保存在 .txt 文件中,以便以后处理它们。

到目前为止,我已经能够获得读数并且只保存一个读数,据我了解,我总是创建一个覆盖先前存在的文件的新文件。但我想要的是从按下开始按钮到按下停止按钮的所有测量值。

这是我正在使用的代码:

public class MainActivity extends AppCompatActivity implements SensorEventListener, View.OnClickListener {

private SensorManager mSensorManager;

private Sensor mAccelerometer;
private Button bStart, bStop;
float[] acceleration = new float[3];
private String mString;

MyFile file = new MyFile(this);


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    bStart = (Button)findViewById(R.id.start);
    bStop = (Button)findViewById(R.id.stop);

    mSensorManager=(SensorManager)getSystemService(SENSOR_SERVICE);
    mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

    bStart.setOnClickListener(this);
    bStop.setOnClickListener(this);
    bStart.setEnabled(true);
    bStop.setEnabled(false);
}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {}

public final void onSensorChanged(SensorEvent event){
    if(event.sensor.getType() ==  Sensor.TYPE_ACCELEROMETER){
        acceleration[0] = event.values[0];
        acceleration[1] = event.values[1];
        acceleration[2] = event.values[2];
        TextView mTextview1 = (TextView) findViewById(R.id.textView1);
        TextView mTextview2 = (TextView) findViewById(R.id.textView2);
        TextView mTextview3 = (TextView) findViewById(R.id.textView3);
        mTextview1.setText("X:"+String.valueOf( acceleration[0]));
        mTextview2.setText("Y:"+String.valueOf( acceleration[1]));
        mTextview3.setText("Z:"+String.valueOf( acceleration[2]));

        file.writeToSD(acceleration[0] + "," +acceleration[1] + "," +acceleration[2] + "\n");
    }
}

@Override
public void onClick(View v) {
    switch(v.getId()){
        case R.id.start:
            bStart.setEnabled(false);
            bStop.setEnabled(true);
            mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
            break;
        case R.id.stop:
            bStart.setEnabled(true);
            bStop.setEnabled(false);
            mSensorManager.unregisterListener(this);
            break;
    }
}

}

还有 MyFile 类,完成工作的地方如下:

public class MyFile {

String TAG = "MyFile";
Context context;

public MyFile(Context context) {
    this.context = context;
}

public Boolean writeToSD(String text) {
    Boolean write_successful = false;
    File root = null;
    try {
        // check for SDcard
        root = Environment.getExternalStorageDirectory();
        Log.i(TAG, "path.." + root.getAbsolutePath());

        //check sdcard permission
        if (root.canWrite()) {
            File fileDir = new File(root.getAbsolutePath());
            fileDir.mkdirs();

            File file = new File(fileDir, "samplefile.txt");
            FileWriter filewriter = new FileWriter(file);
            BufferedWriter out = new BufferedWriter(filewriter);
            out.append(text);
            out.flush();
            out.close();
            write_successful = true;
        }
    } catch (IOException e) {
       e.printStackTrace();
        write_successful = false;
    }
    return write_successful;
}

}

我的另一个问题是为什么我不能将 .txt 文件存储在 sdcard 中,而是保存在内部存储器中。

感谢您的宝贵时间和帮助

【问题讨论】:

  • 传递第二个参数 true 以追加到文件中。像这样 FileWriter fw = new FileWriter("outfilename", true);
  • 非常感谢。有效。顺便问一下,为什么使用此代码将 .txt 文件保存在设备的内部存储器中而不是 sdcard 中?我已经使用了权限
  • 您在模拟器或设备中进行测试?您可以检查它在记录器中打印的路径。您已经打印了该路径。所以检查一下。
  • 返回内存中的路径。我不知道为什么会这样。我正在测试两者。在实际设备中,我在内部存储器中找到该文件。在模拟器中我无法访问内部存储器
  • 为此你可以检查一下 - stackoverflow.com/a/6049446/2128166

标签: android file-io accelerometer


【解决方案1】:

试试这个代码。将 FileWriter 的第二个参数设置为 true。因此,您可以附加现有文件

 File file = new File("Hello.txt");    
 file.createNewFile();    
 FileWriter writer = new FileWriter(file,true); 
 writer.write("Writes the content to the file"); 
 writer.flush();
 writer.close();

【讨论】:

    猜你喜欢
    • 2020-08-07
    • 2022-01-20
    • 2016-04-22
    • 2017-09-07
    • 2013-07-06
    • 2016-06-11
    • 2015-05-20
    相关资源
    最近更新 更多