将短c#脚本翻译成java等价物

时间:2015-04-09 11:22:12

标签: java c# code-translation

我需要将下面发布的这个短脚本从c#转换为java,但是我对c#非常不熟悉。我尝试了this question中建议的一些自动工具,但其中一些是专有的,而其他一些工具没有任何用处。

也许对c#有更深入理解的人可以很快帮助我理解重要组件的等效性,即很清楚c#中的using等同于java中的import,但是还有什么?我一直在寻找一种解释这一点的表,但无济于事。例如,名称空间是什么?

我不想发布失败的尝试,因为他们因严肃的沉思而过于悲惨。

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");
        }
    }
}

0 个答案:

没有答案