Android 附加文本文件

Android append text file(Android 附加文本文件)

本文介绍了Android 附加文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将对话选择器中的唤醒时间和睡眠时间记录到这样的文本文件中,但是对方法 commitToFile2 的调用不会附加文本文件savedData.txt".

I am tring to record the wake time and sleep time from a dialog picker to a text file like this, but the call to the method commitToFile2 doesn't append the text file "savedData.txt."

我知道这段代码很脏.我是 Java 新手,因此我们将不胜感激任何其他建议.

I know this code is very very dirty. I'm new to Java so any other suggestions would be greatly appreciated.

package com.buttinyourface;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.AdapterView.OnItemClickListener;

public class SettingsActivity extends Activity {

    public int wakeHour;
    public int wakeMinute;
    public int sleepHour;
    public int sleepMinute;
    public String sleepHourText = "No Data";
    public String sleepMinuteText = "No Data";
    public String outputTime = "No Data";
    public String wakeHourText = "No Data";
    private ListView lv;
    Dialog newDialogBox;
    Context appContext;
    protected Context context;
    static final private int WAKE_TIME = 0;
    static final private int SLEEP_TIME = 1;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.settings);
        final String[] settingsList = getResources().getStringArray(
                R.array.settingsStringArray);
        lv = (ListView) findViewById(R.id.list);
        TextView wakeHourTextView = (TextView) findViewById(R.id.TextView01);
        lv.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                int settingsPosition = position;
                if (settingsPosition == 0) {
                    showDialog(WAKE_TIME);
                    wakeHourText = Integer.toString(wakeHour);
                }
                if (settingsPosition == 1) {
                    showDialog(SLEEP_TIME);
                }
            }
        });
        lv.setAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, settingsList));
        wakeHourTextView.setText(outputTime);
    }

    public void onPrepareDialog(int id, Dialog dialog) {
        switch (id) {
        case WAKE_TIME:
            break;
        case SLEEP_TIME:
            break;
        }
    }

    @Override
    public Dialog onCreateDialog(int id) {
        switch (id) {
        case WAKE_TIME:
            return new TimePickerDialog(this, WakeTimeSetListener, wakeHour,
                    wakeMinute, false);
        case SLEEP_TIME:
            return new TimePickerDialog(this, SleepTimeSetListener, sleepHour,
                    sleepMinute, false);
        }
        return null;
    }

    private TimePickerDialog.OnTimeSetListener WakeTimeSetListener =
                          new TimePickerDialog.OnTimeSetListener() {
        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
            wakeHour = hourOfDay;
            wakeMinute = minute;
            String wakeHourText = Integer.toString(hourOfDay);
            String wakeMinuteText = Integer.toString(minute);
            String preftime = hourOfDay + ":" + minute;
            SimpleDateFormat df = new SimpleDateFormat("HH:mm");
            SimpleDateFormat dfOut = new SimpleDateFormat("hh:mma");
            Date date = null;
            try {
                date = df.parse(preftime);
            } catch (ParseException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            String outputWakeTime = dfOut.format(date);
            try {
                commitToFile(wakeHourText, wakeMinuteText, outputWakeTime);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    };
    private TimePickerDialog.OnTimeSetListener SleepTimeSetListener =
                          new TimePickerDialog.OnTimeSetListener() {
        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
            sleepHour = hourOfDay;
            sleepMinute = minute;
            String sleepHourText = Integer.toString(hourOfDay);
            String sleepMinuteText = Integer.toString(minute);
            String preftime = hourOfDay + ":" + minute;
            SimpleDateFormat df = new SimpleDateFormat("HH:mm");
            SimpleDateFormat dfOut = new SimpleDateFormat("hh:mma");
            Date date = null;
            try {
                date = df.parse(preftime);
            } catch (ParseException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            String sleepOutputTime = dfOut.format(date);
            try {
                commitToFile2(sleepHourText, sleepMinuteText, sleepOutputTime);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    };

    private void commitToFile(String wakeHourText, String wakeMinuteText,
            String outputWakeTime) throws IOException {
        final String entryString = new String("wakeHour=" + wakeHourText
                + ";wakeMinute=" + wakeMinuteText + ";wakeTime="
                + outputWakeTime + ";");
        FileOutputStream fOut = openFileOutput("savedData.txt",
                MODE_WORLD_READABLE);
        OutputStreamWriter osw = new OutputStreamWriter(fOut);
        osw.write(entryString);
        osw.flush();
        osw.close();
    }

    private void commitToFile2(String sleepHourText, String sleepMinuteText,
            String sleepOutputTime) throws IOException {
        final String entryString = new String("sleepHour=" + sleepHourText
                + ";sleepMinute=" + sleepMinuteText + ";sleepTime="
                + sleepOutputTime + ";");
        FileOutputStream fOut = openFileOutput("savedData.txt",
                MODE_WORLD_READABLE);
        OutputStreamWriter osw = new OutputStreamWriter(fOut);
        osw.write(entryString);
        osw.flush();
        osw.close();
    }
}

推荐答案

我想通了....我不得不换行

I figured it out....I had to change the line

FileOutputStream fOut = openFileOutput("savedData.txt", MODE_WORLD_READABLE);

FileOutputStream fOut = openFileOutput("savedData.txt",  MODE_APPEND);

之后,我能够在不覆盖文本文件中已经存在的数据的情况下附加文本文件.谢谢你们的帮助.我想有时去谷歌的第四页很有用.

After that I was able to append the text file without overwriting the data that was already inside the text file. Thanks for your assistance guys. I guess going to the 4th page on google IS useful sometimes.

这篇关于Android 附加文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Android 附加文本文件

基础教程推荐