gpt4 book ai didi

java - 在android中比较两个文件

转载 作者:行者123 更新时间:2023-12-01 14:12:25 24 4
gpt4 key购买 nike

我必须比较两个 .txt 文件。下面是我的代码..

activity_main.xml

<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=".MainActivity" >

<Button
android:id="@+id/file1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="File 1" />

<Button
android:id="@+id/file2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/file1"
android:layout_below="@+id/file1"
android:text="File 2" />

<Button
android:id="@+id/compare"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/file2"
android:layout_below="@+id/file2"
android:layout_marginTop="16dp"
android:text="Compare" />

</RelativeLayout>

MainActivity.java

public class MainActivity extends Activity {


FileOutputStream fos;
FileInputStream fOne, fTwo;
ArrayList<String> arr1 = new ArrayList<String>();
ArrayList<String> arr2 = new ArrayList<String>();

int count = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Button fileOne = (Button)findViewById(R.id.file1);
Button fileTwo = (Button)findViewById(R.id.file2);
Button compare = (Button)findViewById(R.id.compare);
arr1.add("1");
arr1.add("2");
arr1.add("3");

arr2.add("2");
arr2.add("3");


fileOne.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try
{
fos = openFileOutput("File1", Context.MODE_PRIVATE);

for(int temp = 0; temp< arr1.size(); temp++)
{
fos.write((arr1.get(temp).getBytes()) );
fos.write(System.getProperty("line.separator").getBytes());

}
fos.close();
fos.flush();

}

catch(Exception e)
{

}
}
});


fileTwo.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try
{
fos = openFileOutput("File2", Context.MODE_PRIVATE);

for(int temp = 0; temp< arr2.size(); temp++)
{
fos.write((arr2.get(temp).getBytes()) );
fos.write(System.getProperty("line.separator").getBytes());

}
fos.close();
fos.flush();

}

catch(Exception e)
{

}
}
});



compare.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try
{
fOne = openFileInput("File1");
fTwo = openFileInput("File2");

Scanner scanFileOne = new Scanner(new DataInputStream(fOne));
Scanner scanFileTwo = new Scanner(new DataInputStream(fTwo));


while (scanFileOne.hasNextLine())
{
String first = scanFileOne.next();
String second = scanFileTwo.next();
if(first.equals(second))
{
count++;
}
}

Toast.makeText(getBaseContext(), "" + count, 1000).show();

}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
}
});
}

}

我要做的是单击“比较”按钮,我必须比较两个文件的内容,并且应该提示消息。但在这里我的应用程序力量关闭了..需要做什么?

最佳答案

您的 while 循环需要检查两个文件是否都有可以检索的下一行

while (scanFileOne.hasNextLine() && scanFileTwo.hasNextLine())

关于java - 在android中比较两个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18400057/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com