Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
短c脚本到java等价物的翻译_Java_C#_Code Translation - Fatal编程技术网

短c脚本到java等价物的翻译

短c脚本到java等价物的翻译,java,c#,code-translation,Java,C#,Code Translation,我需要将下面发布的这个简短脚本从c转换成java,但是我对c非常不熟悉。我尝试了中建议的一些自动工具,但其中一些是专有的,而另一些则没有产生任何有用的结果 也许对c有更深入了解的人可以帮助我快速理解重要组件的等效性,也就是说,很明显从c使用等同于在java中导入,但还有什么?我一直在寻找一种解释这一点的表格,但没有用。例如,什么是名称空间 我不想公布我失败的尝试,因为它们太痛苦了,无法认真思考 using ProtoBuf; using System; using System.IO; nam

我需要将下面发布的这个简短脚本从c转换成java,但是我对c非常不熟悉。我尝试了中建议的一些自动工具,但其中一些是专有的,而另一些则没有产生任何有用的结果


也许对c有更深入了解的人可以帮助我快速理解重要组件的等效性,也就是说,很明显从c使用等同于在java中导入,但还有什么?我一直在寻找一种解释这一点的表格,但没有用。例如,什么是名称空间

我不想公布我失败的尝试,因为它们太痛苦了,无法认真思考

using ProtoBuf;
using System;
using System.IO;

namespace SO29531899
{
    class Program
    {
        static void Main()
        {
            ProcessFile("test-multiple.pb");
            ProcessFile("testNegative.pb");
            ProcessFile("testPositive.pb");
            ProcessFile("trainNegative.pb");
            ProcessFile("trainPositive.pb");
        }
        static void ProcessFile(string path)
        {
            try
            {
                Console.WriteLine("Processing: {0}", path);
                using (var file = File.OpenRead(path))
                {
                    int len, count = 0;
                    while(Serializer.TryReadLengthPrefix(file, PrefixStyle.Base128, out len))
                    {
                        Console.WriteLine("Fragment: {0} bytes", len);
                        using (var reader = new ProtoReader(file, null, null, len))
                        {
                            ProcessRelation(reader);
                            count++;
                        }
                    }
                    Console.WriteLine("{0}, {1} Relation objects parsed", path, count);
                    Console.Error.WriteLine("{0}, {1} Relation objects parsed", path, count);
                }
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine(ex.Message);
            }
            finally
            {
                Console.WriteLine();
            }
        }
        private static void ProcessRelation(ProtoReader reader)
        {
            Console.WriteLine("> Relation");
            while (reader.ReadFieldHeader() > 0)
            {
                Console.Write("{0}: {1}", reader.FieldNumber, reader.WireType);
                switch (reader.FieldNumber)
                {
                    case 1:
                        Console.WriteLine(", sourceGuid, {0}", reader.ReadString());
                        break;
                    case 2:
                        Console.WriteLine(", destGuid, {0}", reader.ReadString());
                        break;
                    case 3:
                        Console.WriteLine(", relType, {0}", reader.ReadString());
                        break;
                    case 4:
                        Console.WriteLine(", mention");
                        var tok = ProtoReader.StartSubItem(reader);
                        ProcessRelationMentionRef(reader);
                        ProtoReader.EndSubItem(tok, reader);
                        break;
                    default:
                        Console.WriteLine(", Unexpected field");
                        reader.SkipField();
                        break;
                }
            }
            Console.WriteLine("< Relation");
        }

        private static void ProcessRelationMentionRef(ProtoReader reader)
        {
            Console.WriteLine("> RelationMentionRef");
            while (reader.ReadFieldHeader() > 0)
            {
                Console.Write("{0}: {1}", reader.FieldNumber, reader.WireType);
                switch (reader.FieldNumber)
                {
                    case 1:
                        Console.WriteLine(", filename, {0}", reader.ReadString());
                        break;
                    case 2:
                        Console.WriteLine(", sourceId, {0}", reader.ReadInt32());
                        break;
                    case 3:
                        Console.WriteLine(", destId, {0}", reader.ReadInt32());
                        break;
                    case 4:
                        Console.WriteLine(", feature, {0}", reader.ReadString());
                        break;
                    case 5:
                        Console.WriteLine(", sentence, {0}", reader.ReadString());
                        break;
                    default:
                        Console.WriteLine(", Unexpected field");
                        reader.SkipField();
                        break;
                }
            }
            Console.WriteLine("< RelationMentionRef");
        }
    }
}

很明显,从c使用等同于在java中导入No,这是using指令。using var file=file.OpenReadpath是一个using语句,这是完全不同的。这类似于在Java中使用参考资料,最好在代码中指定您不清楚的内容,并提出具体问题。否则,这里使用的c类和方法及其java等价物的一些比较表将至少有几页文本。我真的怀疑有人会费心写这样一个表格。@Dennis_E我想,这部分问题是关于使用系统;,不使用var file=file.OpenReadpath@是的,我意识到太晚了。我对之前的评论进行了一些编辑。您有哪些具体问题?有没有什么不容易理解的东西跳出来了?发布的代码看起来非常简单。您有函数、函数调用、while循环、switch语句、写入控制台、try/catch/finally块——我认为其中大部分都是以非常类似的方式用java编写的