Tuesday, May 29, 2012

Online Compilers

Two cool online compilers for code snippets:
http://codepad.org and http://ideone.com

I tried this C++ Fibonacci function and the compiler worked:

#include <iostream>
 
int fib(int n)
{
     if ( n == 0 ) return 0;
     if ( n == 1 ) return 1;
 
     return fib(n-1) + fib(n-2);
}
 
int main(){
 printf("Fib Val: %i", fib(7));
 return 0;
}

Thursday, May 17, 2012

.NET C# Get Byte array from a file


public static byte[] GetBytesFromFile(string file)
       {
            try
            {
                // check if this is a file name;
                if (file == null)
                    return null;

                if (file.Trim().Length == 0)
                    return null;

                // get the size of the buffer
                int bufferSize = (int)(new System.IO.FileInfo(file).Length);

                // check if the file has anything.
                if (bufferSize == 0)
                    return null;

                // create the buffer with the proper size
                byte[] buffer = new byte[bufferSize];

                // create the stream
                System.IO.FileStream fs = 
new System.IO.FileStream(file, System.IO.FileMode.Open);

                // create the reader of the stream
                System.IO.BinaryReader reader = new System.IO.BinaryReader(fs);

                // populate the buffer
                buffer = reader.ReadBytes(bufferSize);

                // Close the stream
                fs.Close();
                fs.Dispose();

                // close the reader
                reader.Close();
                fs.Dispose();

                return buffer;
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show("Please Make sure that the file is valid or is not in use. Details:\r\n" + ex.Message);
                return null;
            }

        }

Monday, May 14, 2012

.NET C# Create a file and open it using Default Application Type


public static void OpenFileWithDefaultApplication(string fullfileLocationAndName, byte[] data)
{
  try
  {
    System.IO.FileStream fs =   new System.IO.FileStream(fullfileLocationAndName, System.IO.FileMode.Create, System.IO.FileAccess.ReadWrite);
      fs.Write(data, 0, data.Length - 1);
      fs.Close();
      fs.Dispose();
      System.Diagnostics.Process.Start(fullfileLocationAndName);
  }
catch (Exception ex)
   {
       MessageBox.Show("There was an error opening the file: " + ex.Message);
}
}

Tuesday, May 8, 2012

JAVA Write to a file

// Write x to file
public void writeFile(String x, String file){
    try{
        // Create file
        FileWriter fstream = new FileWriter(file);
        // Create Writer
        BufferedWriter out = new BufferedWriter(fstream);
        // Write x to the file        
        out.write(x);
        //Close the output stream
        out.close();
    }catch (Exception e){
        //Catch exception if any
        System.err.println("Error: " + e.getMessage());
    }
}

Monday, May 7, 2012

JAVA Date and time formatting

public static String getDateNow(){
  DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
return (dateFormat.format(date))+"";
}

Sunday, May 6, 2012

JAVA Manipulate a String using regular expressions

// Example 1, remove all instances of a word with another in a String.
// Example 2, remove all white spaces from a String
public static String manipulateString(String s){
  String result = s;
  
  //1 replace all instances of the word foo with the word bee 
  result = result.replaceAll("(?:foo)", "bee");

  // remove all white space
  result = result.replaceAll("\\s+", "");
   
  return result;
}

JAVA Open a file using the default application editor.

// Example Foo.xls will be opened with Excel
// Make sure to import the class java.awt.Desktop

public static void OpenFile(String filename){
  try{
    // Check if class 'Desktop' is supported on the platform 
    if (Desktop.isDesktopSupported()) {
      Desktop.getDesktop().open(new File(filename));
    }else{
      System.out.println("Class Desktop is unsupported");
    }
  } catch (Exception ex){
    System.out.println("Error opening "+ filename);
  }
}

.NET C# Write to a file

public static bool WriteToFile(string fileName, string fileContent)
{
    fileName = fileName;
    System.IO.TextWriter tw;
    try
    {
        tw = new System.IO.StreamWriter(fileName, true);
        tw.WriteLine(fileContent);
        tw.Close();
        return true;
    }
    catch{}
    return false;

.NET C# Read from a file

public static string ReadFromFile(string fileName)
{
    string ret = "";
    System.IO.StreamReader sr;
    try
    {
        sr = new System.IO.StreamReader(fileName);           
        ret = sr.ReadToEnd();
        sr.Close();
        return ret;
    }
    catch{ return null; }
}