0

我必须比较两个 .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();
                }
            }
        });
    }

}

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

4

1 回答 1

1

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

while (scanFileOne.hasNextLine() && scanFileTwo.hasNextLine())
于 2013-08-23T10:12:25.960 回答