为什么我在for循环中写入文本文件的行被多次写入文本文件

本文关键字:文本 文件 for 为什么 循环 | 更新日期: 2023-09-27 18:05:36

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ConsoleApplication_FileComparison
{
    class Program
    {
        static void Main(string[] args)
        {
            //renameFiles();
            duplicateFilesFinder();
        }
        public static string duplicateFilesFinder()
        {
            Console.WindowWidth = 107;
            byte[] a, b;
            StreamWriter sw;
            string sf;
            string[] images;
            int x = 0;
            int y = 0;
            sw = new StreamWriter(@"d:'stream.txt");
            sf = @"d:'test";
            images = Directory.GetFiles(sf, "*.jpg");
            for (x = 0; x < images.Length - 1; x++)
            {
                for (y = x + 1; y < images.Length; y++)
                {
                    Console.Write("Working on file " + images[x] + " please wait'r");
                    if (!File.Exists(images[x]))
                    {
                        Console.Write("The file " + images[x] + " is not exist'r");
                        sw.WriteLine("The file " + images[x] + " is not exist");
                    }
                    else
                    {
                        if (File.Exists(images[y]))
                        {
                            a = File.ReadAllBytes(images[x]);
                            b = File.ReadAllBytes(images[y]);
                            if (a.Length == b.Length)
                            {
                                Console.WriteLine("File " + images[x] + " is the same size as" + "  " + images[y]);
                                sw.WriteLine("File " + images[x] + " is the same size as" + "  " + images[y]);
                                File.Delete(images[y]);
                                Console.WriteLine("File " + images[y] + " have been deleted");
                                sw.WriteLine("File " + images[y] + " have been deleted");
                            }
                        }
                    }
                }
            }
            sw.Close();
            Console.WriteLine(Environment.NewLine + "Process finished please press any key to continue");
            Console.ReadKey();
            return sf;
        }

这就是问题所在:

if (!File.Exists(images[x]))
                        {
                            Console.Write("The file " + images[x] + " is not exist'r");
                            sw.WriteLine("The file " + images[x] + " is not exist");
                        }

如果我不把'r放在控制台上。编写和使用控制台。我在控制台窗口中多次看到这个images[x]文件!第二行也是一样。WriteLine在文本文件中,我看到它很多次。我只想看它一次如果文件不存在,显示它一次。为什么它会显示这么多次?如何解决这个问题?

谢谢。

为什么我在for循环中写入文本文件的行被多次写入文本文件

这是因为您正在测试Y循环中的X文件。把这个测试放到外循环中:

for (x = 0; x < images.Length - 1; x++) {
  Console.Write("Working on file " + images[x] + " please wait'r");
  if (!File.Exists(images[x])) {
    Console.Write("The file " + images[x] + " is not exist'r");
    sw.WriteLine("The file " + images[x] + " is not exist");
  } else {
    for (y = x + 1; y < images.Length; y++) {
      ...

你的内循环的索引是Y而不是X。所以你现在做的是在内循环中打印索引X, Y次的元素——也就是说,你在X, Y次处理元素,而不是一次,为了你的输出语句的目的。

你应该做的是将代码块移到第二个循环之前。检查X处的文件只存在一次就足够了,直到X发生变化。所以尝试:

Console.Write("Working on file " + images[x] + " please wait'r");
if (!File.Exists(images[x]))
{
    Console.Write("The file " + images[x] + " is not exist'r");
    sw.WriteLine("The file " + images[x] + " is not exist");
} else {
    for (y = x + 1; y < images.Length; y++)
    {
       // Etc
    }
}