对于java–ANDROID:如何格式化必须保存在文本文件中的文本感兴趣的读者,本文将提供您所需要的所有信息,我们将详细讲解格式化输出java,并且为您提供关于AndroidGPS启用/禁用时间保存在
对于java – ANDROID:如何格式化必须保存在文本文件中的文本感兴趣的读者,本文将提供您所需要的所有信息,我们将详细讲解格式化输出java,并且为您提供关于Android GPS启用/禁用时间保存在带有多条线的SDCARD内的文本文件中、Android Studio Java 在新行中显示文本文件内容、android – 如何将数组写入内部存储上的文本文件?、android – 如何格式化strings.xml中的长文本?的宝贵知识。
本文目录一览:- java – ANDROID:如何格式化必须保存在文本文件中的文本(格式化输出java)
- Android GPS启用/禁用时间保存在带有多条线的SDCARD内的文本文件中
- Android Studio Java 在新行中显示文本文件内容
- android – 如何将数组写入内部存储上的文本文件?
- android – 如何格式化strings.xml中的长文本?
java – ANDROID:如何格式化必须保存在文本文件中的文本(格式化输出java)
我有一个应用程序,我可以从数据库中选择数据,然后在TableView中显示结果.接下来我要做的是将所选数据(当前显示在TableView中)保存到文本文件中,这样它就可以复制到PC上并打印到纸上.我想要实现的是每列之间的空格相等.我正在使用这样的东西将文本保存到文件中:
out.write("\n "+name+" \t"+amount+" \t"+price);
但是在文本编辑器中打开文件后看起来不太好.这是正确的方法吗?如果不是什么?
解决方法:
您可以使用函数String.format来执行此操作.
例如:
out.write ( String.format("%s %10d %10d", name, amount, price) );
此代码将打印name.toString(),然后在10个字符宽度的字段中整数金额和价格.
您可以阅读有关格式字符串here的信息.
Android GPS启用/禁用时间保存在带有多条线的SDCARD内的文本文件中
我想打印多行文本,而不删除上一行数据.
下面的代码是完美打印多行,但当应用程序保持睡眠和重新启动时.当GPS开启或关闭时,与先前数据一起打印一行.
帮我解决一下.
required Output:(after Restart the app) << GPs Enabled At :2016/06/08 17.15.00>> << GPs disabled At :2016/06/08 17.45.00>> << GPs Enabled At :2016/06/08 17.49.00>> << GPs disabled At :2016/06/08 17.52.00>> Getting Output:(after Restart the app) << GPs Enabled At :2016/06/08 17.15.00>> << GPs disabled At :2016/06/08 17.45.00>> << GPs Enabled At :2016/06/08 17.49.00>> << GPs disabled At :2016/06/08 17.52.00>> << GPs disabled At :2016/06/08 17.52.00>> << GPs disabled At :2016/06/08 17.52.00>>
日志文件的方法:
public void Logmethod(String noramalStr,String Dateval) { /* Log FOlder Text files Starts here*/ try { File newFolder = new File(Environment.getExternalStorageDirectory(),"GpsFolder"); if (!newFolder.exists()) { newFolder.mkdir(); } try { File file = new File(newFolder,"GPsstatus" + ".txt"); file.createNewFile(); /*Writing the Log in specific files*/ BufferedWriter out; try { String separator1 = System.getProperty("line.separator"); FileOutputStream fOut = new FileOutputStream(new File(newFolder + "/GPsstatus.txt"),true); OutputStreamWriter osw = new OutputStreamWriter(fOut); osw.append("<< " + noramalStr +" : "+Dateval + " >>"); osw.append(separator1); // this will add new line ; osw.flush(); osw.close(); fOut.close(); Log.i(noramalStr," : "+Dateval); } catch (FileNotFoundException e) { e.printstacktrace(); } catch (IOException e) { e.printstacktrace(); } } catch (Exception ex) { System.out.println("ex: " + ex); } } catch (Exception e) { System.out.println("e: " + e); } }
解决方法
你总是做createNewFile尝试检查文件是否存在
public void Logmethod(String noramalStr,String Dateval) { try { File newFolder = new File(Environment.getExternalStorageDirectory(),"GpsFolder"); boolean directoryExists = newFolder.exists(); Log.d(TAG," directoryExists = " + directoryExists); if (!directoryExists) { boolean dirCreate = newFolder.mkdir(); Log.d(TAG," dirCreate = " + dirCreate); } File file = new File(newFolder,"GPsstatus" + ".txt"); boolean fileExists = file.exists(); Log.d(TAG," fileExists = " + fileExists); if (!fileExists) { boolean fileCreateResult = file.createNewFile(); Log.d(TAG," fileCreateResult = " + fileCreateResult); } String separator1 = System.getProperty("line.separator"); FileOutputStream fOut = new FileOutputStream(new File(newFolder + "/GPsstatus.txt"),true); OutputStreamWriter osw = new OutputStreamWriter(fOut); osw.append("<< "); osw.append(noramalStr); osw.append(" : "); osw.append(Dateval); osw.append(" >>"); osw.append(separator1); // this will add new line ; osw.flush(); osw.close(); fOut.close(); Log.i(TAG," : " + Dateval); } catch (Exception e) { e.printstacktrace(); } }
如果是关于位置提供者的永久监听启用/禁用
看看How do I receive the system broadcast when GPS status has changed?
并在清单文件中使用接收器
<receiver android:name=".GPStStatusReceiver"> <intent-filter> <action android:name="android.location.PROVIDERS_CHANGED" /> </intent-filter> </receiver>
Android Studio Java 在新行中显示文本文件内容
如何解决Android Studio Java 在新行中显示文本文件内容?
我正在创建一个体重跟踪应用程序,用户在其中输入体重,点击保存按钮,然后保存体重。还有一个加载按钮可以加载所有以前的输入。我遇到的问题是,一旦点击加载,它就会加载屏幕上的权重,但它会在一行中完成,而不是每行单独一行。
我检查了文本文件,所有的权重都存储在一行中,所以存储输入的函数没有问题。
这是“体重跟踪器”的代码
package com.example.workouttracker;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class Weighttracking extends AppCompatActivity {
private static final String FILE_NAME = "Weighttracking.txt";
EditText mEditText;
EditText mEditText2;
private Button button_back_home;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_weight_tracking);
mEditText = findViewById(R.id.weight);
mEditText2 = findViewById(R.id.weight2);
button_back_home=(Button) findViewById(R.id.button_back_home);
button_back_home.setonClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
homePage();
}
private void homePage(){
startActivity(new Intent(getApplicationContext(),MainActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
}
});
}
public void save(View v) {
String text = mEditText.getText().toString();
FileOutputStream fos = null;
try {
fos = openFileOutput(FILE_NAME,MODE_APPEND);
fos.write((text + "kg''s\n").getBytes());
mEditText.getText().clear();
Toast.makeText(this,"Saved to " + getFilesDir() + "/" + FILE_NAME,Toast.LENGTH_LONG).show();
} catch (FileNotFoundException e) {
e.printstacktrace();
} catch (IOException e) {
e.printstacktrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printstacktrace();
}
}
}
}
public void load(View v) {
FileInputStream fis = null;
try {
fis = openFileInput(FILE_NAME);
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
StringBuilder sb = new StringBuilder();
String text;
while ((text = br.readLine()) != null) {
sb.append(text);
sb.append(''\n'');
}
mEditText2.setText(sb.toString());
} catch (FileNotFoundException e) {
e.printstacktrace();
} catch (IOException e) {
e.printstacktrace();
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printstacktrace();
}
}
}
}
}
有人知道如何让它逐行加载每个重量吗? 谢谢
`
解决方法
如果您在编辑文本中添加了属性 android:inputType="textMultiLine"
,请检查您的 xml 文件
尝试像这样更改您的代码:
String sb = "";
String text;
while ((text = br.readLine()) != null) {
sb = sb + text + "\n";
}
android – 如何将数组写入内部存储上的文本文件?
例如下面,我有一个按钮,我想在每次单击按钮时将一些数据保存在文本文件中.我正在测试一个例子:
http://developer.android.com/training/basics/data-storage/files.html
import java.io.FileOutputStream; import android.os.Bundle; import android.app.Activity; import android.content.Context; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity implements OnClickListener { Button b; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); b = (Button) findViewById(R.id.button1); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main,menu); return true; } @Override public void onClick(View v) { String filename = "myfile"; String string = "Hello World!"; FileOutputStream outputStream; try { outputStream = openFileOutput(filename,Context.MODE_APPEND); outputStream.write(string.getBytes()); outputStream.close(); } catch (Exception e) { e.printstacktrace(); } } }
我只将“mode_private”更改为“mode_append”,所以我可以看到保存的文件,但是我找不到保存的文件,我甚至找不到文件应该保存的目录.
另外,更具体地说,我需要在文本文件中存储一个数字数组(比如i = {1,2,3}到output.txt).
如果你能帮助我,我将不胜感激.
解决方法
b = (Button) findViewById(R.id.button1); b.setonClickListener(this);
“但是我找不到保存的文件,我甚至找不到保存文件的目录.”
getApplicationContext().getFilesDir()将告诉您文件的保存位置.
Android DOCS说……
Your app’s internal storage directory is specified by your app’s package name in a special location of the Android file system.
要将数组存储在文件中,请尝试以下操作…
String filename = "myfile"; String[] numbers = new String[] {"1,3"}; FileOutputStream outputStream; try { outputStream = openFileOutput(filename,Context.MODE_APPEND); for (String s : numbers) { outputStream.write(s.getBytes()); } outputStream.close(); } catch (Exception e) { e.printstacktrace(); }
android – 如何格式化strings.xml中的长文本?
然后我收到一个错误.
我想知道如何将这么长的文本格式化为多行文本而不会产生错误?
编辑
在我的Q中我并不准确.似乎这可以在Eclipse中解决.问题是IntelliJ IDEA及其格式化命令Ctrl Alt L,它不会通过文件strings.xml中的编译器传递.快照来自该IDE.
解决方法
今天的关于java – ANDROID:如何格式化必须保存在文本文件中的文本和格式化输出java的分享已经结束,谢谢您的关注,如果想了解更多关于Android GPS启用/禁用时间保存在带有多条线的SDCARD内的文本文件中、Android Studio Java 在新行中显示文本文件内容、android – 如何将数组写入内部存储上的文本文件?、android – 如何格式化strings.xml中的长文本?的相关知识,请在本站进行查询。
本文标签: