当我通过 Print Writer 将数据存储到内部存储时,缓冲的 Reader 返回空数据
亲爱的 StackOverflow 会员,祝您今天愉快。
实际上,我是 Android 的初学者,在下面的代码中,当我尝试通过缓冲读取器读取内部存储中存储的一些数据时,我遇到了以下问题。
当我尝试从“TestFile”文件中读取时,缓冲读取器返回空数据。
我花了很多时间试图解决这个问题,但仍未解决。
所以,请帮助我:(
package com.example.internalstorage;
import androidx.appcompat.app.AppCompatActivity;
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.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
public class MainActivity extends AppCompatActivity {
FileOutputStream fileOutputStream;
PrintWriter printWriter;
public static final String fileName = "TestFile";
String allText = "";
EditText editText;
Button storeBtn;
Button restoreBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = findViewById(R.id.editText);
storeBtn = findViewById(R.id.storeBtn);
restoreBtn = findViewById(R.id.restoreBtn);
try {
File file = new File(getFilesDir(), fileName);
if (file == null){
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
fileOutputStream = new FileOutputStream(fileName);
// openFileOutput(fileName, MODE_PRIVATE);
printWriter = new PrintWriter(fileOutputStream);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
storeBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
printWriter.println(editText.getText().toString());
printWriter.println(editText.getText().toString());
printWriter.println(editText.getText().toString());
Toast.makeText(getBaseContext(), allText, Toast.LENGTH_SHORT).show();
}
});
printWriter.close();
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
restoreBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FileInputStream fileInputStream = null;
InputStreamReader inputStreamReader = null;
BufferedReader bufferedReader = null;
// getFilesDir().get
try {
fileInputStream = openFileInput(fileName);
inputStreamReader = new InputStreamReader(fileInputStream);
bufferedReader = new BufferedReader(inputStreamReader);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
String line = "";
try {
line = bufferedReader.readLine();
String s = bufferedReader.readLine();
Toast.makeText(getBaseContext(), s +"321", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
}
while(line != null){
allText = allText + line + "\n";
try {
line = bufferedReader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
inputStreamReader.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
Toast.makeText(getBaseContext(), allText, Toast.LENGTH_SHORT).show();
//
}
});
}
}
这是预期的 XML 文件
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<EditText
android:id="@+id/editText"
android:layout_width="245dp"
android:layout_height="73dp"
android:layout_marginTop="356dp"
android:layout_marginEnd="80dp"
android:ems="10"
android:inputType="textPersonName"
android:text="Name"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/storeBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="132dp"
android:text="Store"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.479"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="@+id/restoreBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="36dp"
android:text="restore"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.458"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Dear StackOverflow members have a good day.
Actually, I am a beginner learner in Android, And in the following code, I have gotten the following issue When I try to read some data stored in the internal storage by the buffered reader.
The buffered reader returns null data when I try to read from the "TestFile" file.
I have spent a lot of time trying to fix this issue, but it is not fixed.
So, please help me :(
package com.example.internalstorage;
import androidx.appcompat.app.AppCompatActivity;
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.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
public class MainActivity extends AppCompatActivity {
FileOutputStream fileOutputStream;
PrintWriter printWriter;
public static final String fileName = "TestFile";
String allText = "";
EditText editText;
Button storeBtn;
Button restoreBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = findViewById(R.id.editText);
storeBtn = findViewById(R.id.storeBtn);
restoreBtn = findViewById(R.id.restoreBtn);
try {
File file = new File(getFilesDir(), fileName);
if (file == null){
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
fileOutputStream = new FileOutputStream(fileName);
// openFileOutput(fileName, MODE_PRIVATE);
printWriter = new PrintWriter(fileOutputStream);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
storeBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
printWriter.println(editText.getText().toString());
printWriter.println(editText.getText().toString());
printWriter.println(editText.getText().toString());
Toast.makeText(getBaseContext(), allText, Toast.LENGTH_SHORT).show();
}
});
printWriter.close();
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
restoreBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FileInputStream fileInputStream = null;
InputStreamReader inputStreamReader = null;
BufferedReader bufferedReader = null;
// getFilesDir().get
try {
fileInputStream = openFileInput(fileName);
inputStreamReader = new InputStreamReader(fileInputStream);
bufferedReader = new BufferedReader(inputStreamReader);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
String line = "";
try {
line = bufferedReader.readLine();
String s = bufferedReader.readLine();
Toast.makeText(getBaseContext(), s +"321", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
}
while(line != null){
allText = allText + line + "\n";
try {
line = bufferedReader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
inputStreamReader.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
Toast.makeText(getBaseContext(), allText, Toast.LENGTH_SHORT).show();
//
}
});
}
}
And this is the intended XML file
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<EditText
android:id="@+id/editText"
android:layout_width="245dp"
android:layout_height="73dp"
android:layout_marginTop="356dp"
android:layout_marginEnd="80dp"
android:ems="10"
android:inputType="textPersonName"
android:text="Name"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/storeBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="132dp"
android:text="Store"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.479"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="@+id/restoreBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="36dp"
android:text="restore"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.458"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论