Android的附加文本文件 [英] Android append text file
问题描述
我特林记录从对话框选择器这样的一个文本文件中的唤醒时间和睡眠时间,但在调用方法commitToFile2不附加文本文件savedData.txt。
我知道这code是非常非常脏。我是新来的Java所以任何其他建议,将不胜AP preciated。
包com.buttinyourface;
进口的java.io.File;
进口java.io.FileInputStream中;
进口java.io.FileOutputStream中;
进口java.io.FileWriter;
进口java.io.IOException异常;
进口的java.io.InputStream;
进口java.io.OutputStreamWriter中;
进口java.text.ParseException;
进口java.text.SimpleDateFormat的;
进口java.util.Date;
进口android.app.Activity;
进口android.app.AlertDialog;
进口android.app.Dialog;
进口android.app.TimePickerDialog;
进口android.content.Context;
进口android.os.Bundle;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.widget.AdapterView;
进口android.widget.ArrayAdapter;
进口android.widget.ListView;
进口android.widget.TextView;
进口android.widget.TimePicker;
进口android.widget.AdapterView.OnItemClickListener;
公共类SettingsActivity延伸活动{
公众诠释wakeHour;
公众诠释wakeMinute;
公众诠释sleepHour;
公众诠释sleepMinute;
公共字符串sleepHourText =没有数据;
公共字符串sleepMinuteText =没有数据;
公共字符串outputTime =没有数据;
公共字符串wakeHourText =没有数据;
私人的ListView LV;
对话newDialogBox;
语境appContext;
保护上下文的背景下;
static final的私人诠释WAKE_TIME = 0;
static final的私人诠释SLEEP_TIME = 1;
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.settings);
最终的String [] settingsList = getResources()。getStringArray(
R.array.settingsStringArray);
LV =(ListView控件)findViewById(R.id.list);
TextView的wakeHourTextView =(TextView中)findViewById(R.id.TextView01);
lv.setOnItemClickListener(新OnItemClickListener(){
公共无效onItemClick(适配器视图<>母公司视图中查看,
INT位置,长的id){
INT settingsPosition =位置;
如果(settingsPosition == 0){
的ShowDialog(WAKE_TIME);
wakeHourText = Integer.toString(wakeHour);
}
如果(settingsPosition == 1){
的ShowDialog(SLEEP_TIME);
}
}
});
lv.setAdapter(新ArrayAdapter<字符串>(这一点,
android.R.layout.simple_list_item_1,settingsList));
wakeHourTextView.setText(outputTime);
}
在prepareDialog(INT ID,对话对话框){公共无效
开关(ID){
案例WAKE_TIME:
打破;
案例SLEEP_TIME:
打破;
}
}
@覆盖
公共对话onCreateDialog(INT ID){
开关(ID){
案例WAKE_TIME:
返回新TimePickerDialog(这一点,WakeTimeSetListener,wakeHour,
wakeMinute,假);
案例SLEEP_TIME:
返回新TimePickerDialog(这一点,SleepTimeSetListener,sleepHour,
sleepMinute,假);
}
返回null;
}
私人TimePickerDialog.OnTimeSetListener WakeTimeSetListener =
新TimePickerDialog.OnTimeSetListener(){
公共无效onTimeSet(TimePicker观点,诠释hourOfDay,INT分钟){
wakeHour = hourOfDay;
wakeMinute =分钟;
字符串wakeHourText = Integer.toString(hourOfDay);
字符串wakeMinuteText = Integer.toString(分钟);
字符串preftime = hourOfDay +:+分钟;
SimpleDateFormat的DF =新的SimpleDateFormat(HH:MM);
SimpleDateFormat的dfOut =新的SimpleDateFormat(HH:MMA);
日期日期= NULL;
尝试 {
日期= df.parse(preftime);
}赶上(ParseException的E1){
// TODO自动生成的catch块
e1.printStackTrace();
}
字符串outputWakeTime = dfOut.format(日期);
尝试 {
commitToFile(wakeHourText,wakeMinuteText,outputWakeTime);
}赶上(IOException异常E){
// TODO自动生成的catch块
e.printStackTrace();
}
}
};
私人TimePickerDialog.OnTimeSetListener SleepTimeSetListener =
新TimePickerDialog.OnTimeSetListener(){
公共无效onTimeSet(TimePicker观点,诠释hourOfDay,INT分钟){
sleepHour = hourOfDay;
sleepMinute =分钟;
字符串sleepHourText = Integer.toString(hourOfDay);
字符串sleepMinuteText = Integer.toString(分钟);
字符串preftime = hourOfDay +:+分钟;
SimpleDateFormat的DF =新的SimpleDateFormat(HH:MM);
SimpleDateFormat的dfOut =新的SimpleDateFormat(HH:MMA);
日期日期= NULL;
尝试 {
日期= df.parse(preftime);
}赶上(ParseException的E1){
// TODO自动生成的catch块
e1.printStackTrace();
}
字符串sleepOutputTime = dfOut.format(日期);
尝试 {
commitToFile2(sleepHourText,sleepMinuteText,sleepOutputTime);
}赶上(IOException异常E){
// TODO自动生成的catch块
e.printStackTrace();
}
}
};
私人无效commitToFile(字符串wakeHourText,字符串wakeMinuteText,
字符串outputWakeTime)抛出IOException异常{
最后弦乐entryString =新的String(wakeHour =+ wakeHourText
+; wakeMinute =+ wakeMinuteText +; wakeTime =
+ outputWakeTime +;);
FileOutputStream中FOUT = openFileOutput(savedData.txt
MODE_WORLD_READABLE);
OutputStreamWriter OSW =新OutputStreamWriter(FOUT);
osw.write(entryString);
osw.flush();
osw.close();
}
私人无效commitToFile2(字符串sleepHourText,字符串sleepMinuteText,
字符串sleepOutputTime)抛出IOException异常{
最后弦乐entryString =新的String(sleepHour =+ sleepHourText
+; sleepMinute =+ sleepMinuteText +;睡眠时间=
+ sleepOutputTime +;);
FileOutputStream中FOUT = openFileOutput(savedData.txt
MODE_WORLD_READABLE);
OutputStreamWriter OSW =新OutputStreamWriter(FOUT);
osw.write(entryString);
osw.flush();
osw.close();
}
}
我想它了....我不得不改变线路
的FileOutputStream FOUT = openFileOutput(savedData.txt,MODE_WORLD_READABLE);
到
的FileOutputStream FOUT = openFileOutput(savedData.txt,MODE_APPEND);
在那之后我就能够追加文本文件没有覆盖的已经是文本文件中的数据。感谢您的帮助球员。我估计要到第4页上的谷歌有时是很有用的。
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."
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);
to
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的附加文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!