繁体   English   中英

比较android中的两个文件

[英]Compare two files in android

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM