C# use using Statement to define its scope outside of which an object or objects are disposed.  But there is no equivalent syntax statement in Java. CodePorting C#2Java engine intelligently translates the C# code and handle this situation by producing try/catch block in java code.  This Technique converts C# code to java code with same formatting ready to be complied.

Following example shows migration of C# using statement in java:

C# Code:

 

Code
using System.IO;
Code
Code
namespace CsPorter.Tests.Convert.LanguageConstructs.UsingStatement
Code
{
Code
    public class Test2
Code
    {
Code
        void Method()
Code
        {
Code
Code
            //some comments (vertical indent)
Code
            using (Stream stream = new FileStream("nowhere", FileMode.Open))
Code
            {
Code
                stream.ReadByte();
Code
            }
Code
Code
                //horisontal indent
Code
                using (Stream stream = new FileStream("nowhere", FileMode.Open))
Code
                {
Code
                    stream.ReadByte();
Code
                }
Code
        }
Code
    }
Code
}

 

 

Java code generated by CodePorting:

Code
Code
package CsPorter.Tests.Convert.LanguageConstructs.UsingStatement;
Code
Code
// ********* THIS FILE IS AUTO PORTED FORM C# USING CODEPORTING.COM *********
Code
Code
import com.codeporting.csharp2java.System.IO.Stream;
Code
import com.codeporting.csharp2java.System.IO.FileStream;
Code
import com.codeporting.csharp2java.System.IO.FileMode;
Code
Code
public class Test2
Code
{
Code
    private void method() throws Exception
Code
    {
Code
Code
        //some comments (vertical indent)
Code
        Stream stream = new FileStream("nowhere", FileMode.OPEN);
Code
        try /*JAVA: was using*/
Code
        {
Code
            stream.readByte();
Code
        }
Code
        finally { if (stream != null) stream.close(); }
Code
Code
            //horisontal indent
Code
            Stream stream1 = new FileStream("nowhere", FileMode.OPEN);
Code
            try /*JAVA: was using*/
Code
            {
Code
                stream1.readByte();
Code
            }
Code
            finally { if (stream1 != null) stream1.close(); }
Code
    }
Code
}

 

 

It is clear from above example that CodePorting C#2Java engine automatically generated the try/catch code to handle the using statement.