Java Fileinputstream android循环无法加载数据活动被卡住
我的代码似乎在应用程序中循环,因此卡住了。 数据成功保存在outputstream中,没有任何问题,但我几乎可以肯定,当循环导致问题时 以下是我的输入流xml:Java Fileinputstream android循环无法加载数据活动被卡住,java,android,exception,fileinputstream,Java,Android,Exception,Fileinputstream,我的代码似乎在应用程序中循环,因此卡住了。 数据成功保存在outputstream中,没有任何问题,但我几乎可以肯定,当循环导致问题时 以下是我的输入流xml: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_w
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/edit_text_2_1"
android:hint="User name is gonna come here"
android:textSize="25dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/edit_text_2_2"
android:hint="pass name is gonna come here"
android:textSize="25dp"
android:layout_marginTop="25dp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn_edit_2_2"
android:text="load"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn_edit_2_3"
android:text="Go back"
/>
</LinearLayout>
package com.example.admin.file_view;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
/**
* Created by admin on 15-03-2016.
*/
public class activity_second extends Activity{
TextView t1,t2;
Button b1,b2;
String name, pass;
FileInputStream fos=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
t1=(TextView)findViewById(R.id.edit_text_2_1);
t2=(TextView)findViewById(R.id.edit_text_2_2);
b1=(Button)findViewById(R.id.btn_edit_2_2);
b2=(Button)findViewById(R.id.btn_edit_2_3);
b1.setOnClickListener(new View.OnClickListener() {
String temp;
int read=-1;
@Override
public void onClick(View v) {
try {
fos= openFileInput("haha.txt");
StringBuffer biffer=new StringBuffer();
while(fos.read()!=1)
{
biffer.append((char)fos.read());
}
// char c=(char)fos.read();
//Toast.makeText(getApplicationContext(),c+" is getting read",Toast.LENGTH_LONG).show();
/* while((read=fos.read())!=-1)
{
biffer.append((char)read);
}*/
name=biffer.substring(0,biffer.indexOf(" "));
pass=biffer.substring(biffer.indexOf(" ")+1);
t1.setText(name);
t2.setText(pass);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});}}
package com.example.admin.file_view;
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class MainActivity extends Activity {
TextView t1,t2;
Button b1,b2;
String name, pass;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t1=(TextView)findViewById(R.id.edit_txt1);
t2=(TextView)findViewById(R.id.edit_txt2);
b1=(Button)findViewById(R.id.btn_edit_1);
b2=(Button)findViewById(R.id.btn_edit_2);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
name= t1.getText().toString();
pass=t2.getText().toString();
FileOutputStream fos= null;
try {
fos = openFileOutput("haha.txt",MODE_PRIVATE);
fos.write(t1.getText().toString().getBytes());
fos.write(pass.getBytes());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
finally {
try {
if (fos != null) {
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
Toast.makeText(getApplicationContext(),"data saved successfully",Toast.LENGTH_SHORT).show();
}
});
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(getApplication(),activity_second.class);
startActivity(intent);
}
});}}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.admin.file_view.MainActivity">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/edit_txt1"
android:hint="Enter the username"
android:textSize="25dp"
/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/edit_txt2"
android:layout_below="@+id/edit_txt1"
android:hint="Enter pass"
android:textSize="25dp"
android:layout_marginTop="25dp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/edit_txt2"
android:id="@+id/btn_edit_1"
android:text="save"
android:textSize="25dp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/edit_txt2"
android:id="@+id/btn_edit_2"
android:layout_toRightOf="@id/btn_edit_1"
android:text="nextpage"
android:textSize="25dp"
/>
</RelativeLayout>
[1]: http://i.stack.imgur.com/bOqgl.png
如果我使用注释部分,我会得到索引数组越界异常,这很奇怪,也需要帮助。。如果您需要查看文件输出流:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/edit_text_2_1"
android:hint="User name is gonna come here"
android:textSize="25dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/edit_text_2_2"
android:hint="pass name is gonna come here"
android:textSize="25dp"
android:layout_marginTop="25dp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn_edit_2_2"
android:text="load"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn_edit_2_3"
android:text="Go back"
/>
</LinearLayout>
package com.example.admin.file_view;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
/**
* Created by admin on 15-03-2016.
*/
public class activity_second extends Activity{
TextView t1,t2;
Button b1,b2;
String name, pass;
FileInputStream fos=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
t1=(TextView)findViewById(R.id.edit_text_2_1);
t2=(TextView)findViewById(R.id.edit_text_2_2);
b1=(Button)findViewById(R.id.btn_edit_2_2);
b2=(Button)findViewById(R.id.btn_edit_2_3);
b1.setOnClickListener(new View.OnClickListener() {
String temp;
int read=-1;
@Override
public void onClick(View v) {
try {
fos= openFileInput("haha.txt");
StringBuffer biffer=new StringBuffer();
while(fos.read()!=1)
{
biffer.append((char)fos.read());
}
// char c=(char)fos.read();
//Toast.makeText(getApplicationContext(),c+" is getting read",Toast.LENGTH_LONG).show();
/* while((read=fos.read())!=-1)
{
biffer.append((char)read);
}*/
name=biffer.substring(0,biffer.indexOf(" "));
pass=biffer.substring(biffer.indexOf(" ")+1);
t1.setText(name);
t2.setText(pass);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});}}
package com.example.admin.file_view;
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class MainActivity extends Activity {
TextView t1,t2;
Button b1,b2;
String name, pass;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t1=(TextView)findViewById(R.id.edit_txt1);
t2=(TextView)findViewById(R.id.edit_txt2);
b1=(Button)findViewById(R.id.btn_edit_1);
b2=(Button)findViewById(R.id.btn_edit_2);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
name= t1.getText().toString();
pass=t2.getText().toString();
FileOutputStream fos= null;
try {
fos = openFileOutput("haha.txt",MODE_PRIVATE);
fos.write(t1.getText().toString().getBytes());
fos.write(pass.getBytes());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
finally {
try {
if (fos != null) {
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
Toast.makeText(getApplicationContext(),"data saved successfully",Toast.LENGTH_SHORT).show();
}
});
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(getApplication(),activity_second.class);
startActivity(intent);
}
});}}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.admin.file_view.MainActivity">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/edit_txt1"
android:hint="Enter the username"
android:textSize="25dp"
/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/edit_txt2"
android:layout_below="@+id/edit_txt1"
android:hint="Enter pass"
android:textSize="25dp"
android:layout_marginTop="25dp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/edit_txt2"
android:id="@+id/btn_edit_1"
android:text="save"
android:textSize="25dp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/edit_txt2"
android:id="@+id/btn_edit_2"
android:layout_toRightOf="@id/btn_edit_1"
android:text="nextpage"
android:textSize="25dp"
/>
</RelativeLayout>
[1]: http://i.stack.imgur.com/bOqgl.png
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/edit_text_2_1"
android:hint="User name is gonna come here"
android:textSize="25dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/edit_text_2_2"
android:hint="pass name is gonna come here"
android:textSize="25dp"
android:layout_marginTop="25dp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn_edit_2_2"
android:text="load"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn_edit_2_3"
android:text="Go back"
/>
</LinearLayout>
package com.example.admin.file_view;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
/**
* Created by admin on 15-03-2016.
*/
public class activity_second extends Activity{
TextView t1,t2;
Button b1,b2;
String name, pass;
FileInputStream fos=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
t1=(TextView)findViewById(R.id.edit_text_2_1);
t2=(TextView)findViewById(R.id.edit_text_2_2);
b1=(Button)findViewById(R.id.btn_edit_2_2);
b2=(Button)findViewById(R.id.btn_edit_2_3);
b1.setOnClickListener(new View.OnClickListener() {
String temp;
int read=-1;
@Override
public void onClick(View v) {
try {
fos= openFileInput("haha.txt");
StringBuffer biffer=new StringBuffer();
while(fos.read()!=1)
{
biffer.append((char)fos.read());
}
// char c=(char)fos.read();
//Toast.makeText(getApplicationContext(),c+" is getting read",Toast.LENGTH_LONG).show();
/* while((read=fos.read())!=-1)
{
biffer.append((char)read);
}*/
name=biffer.substring(0,biffer.indexOf(" "));
pass=biffer.substring(biffer.indexOf(" ")+1);
t1.setText(name);
t2.setText(pass);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});}}
package com.example.admin.file_view;
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class MainActivity extends Activity {
TextView t1,t2;
Button b1,b2;
String name, pass;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t1=(TextView)findViewById(R.id.edit_txt1);
t2=(TextView)findViewById(R.id.edit_txt2);
b1=(Button)findViewById(R.id.btn_edit_1);
b2=(Button)findViewById(R.id.btn_edit_2);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
name= t1.getText().toString();
pass=t2.getText().toString();
FileOutputStream fos= null;
try {
fos = openFileOutput("haha.txt",MODE_PRIVATE);
fos.write(t1.getText().toString().getBytes());
fos.write(pass.getBytes());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
finally {
try {
if (fos != null) {
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
Toast.makeText(getApplicationContext(),"data saved successfully",Toast.LENGTH_SHORT).show();
}
});
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(getApplication(),activity_second.class);
startActivity(intent);
}
});}}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.admin.file_view.MainActivity">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/edit_txt1"
android:hint="Enter the username"
android:textSize="25dp"
/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/edit_txt2"
android:layout_below="@+id/edit_txt1"
android:hint="Enter pass"
android:textSize="25dp"
android:layout_marginTop="25dp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/edit_txt2"
android:id="@+id/btn_edit_1"
android:text="save"
android:textSize="25dp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/edit_txt2"
android:id="@+id/btn_edit_2"
android:layout_toRightOf="@id/btn_edit_1"
android:text="nextpage"
android:textSize="25dp"
/>
</RelativeLayout>
[1]: http://i.stack.imgur.com/bOqgl.png
[1]: http://i.stack.imgur.com/bOqgl.png
这代码简直是胡说八道。它跳过每个奇数字节。您应该使用文件读取器
,以及缓冲区
int count;
char charBuffer = new char[8192];
FileReader reader = new FileReader(...);
while ((count = reader.read(charBuffer)) > 0)
{
buffer.append(charBuffer, 0, count);
}
reader.close();
由于格式的原因,这很难阅读。我们不需要看到您庞大的UI图形。您应该取消主线程。感谢所有帮助EJP,但字符串缓冲区不是一个好的选择,通常我的代码会发生什么?我从来没有使用过java exp,所以stackoveflow上的视频和一些东西让我很开心:|@ranjanjoishi我一点都不懂
StringBuffer
是你的选择,不是我的。我的回答中甚至没有提到。如果您不喜欢,请使用StringBuilder
。与我无关。“一般来说,我的代码会发生什么?”是没有意义的。当你尝试过这种方式时,请让我们知道。哈哈,好的,谢谢EJP:)