// this is a very simple example of java serialization example
//Serializations: Assume this is my class to be serialized
import java.util.ArrayList;
public class Dish {
private String dishName;
private ArrayList<String> ingredient;
public String getDishName(){
return this.className;
}
public ArrayList<String> getIngredient(){
return this.variableNames;
}
public void setDishName(String dishName){
this.dishName = dishName;
}
public void setIngredient(ArrayList<String> ingredient){
this.ingredient = ingredient;
}
}
// This class will read and write. I reused the one found here which is awesome.
// Thanks Awesome person who did this
import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
public class Builder {
public static void writeXML(Schema f, String filename) throws Exception{
XMLEncoder encoder =
new XMLEncoder(
new BufferedOutputStream(
new FileOutputStream(filename)));
encoder.writeObject(f);
encoder.close();
}
public static Schema readXML(String filename) throws Exception {
XMLDecoder decoder =
new XMLDecoder(new BufferedInputStream(
new FileInputStream(filename)));
Schema o = (Schema)decoder.readObject();
decoder.close();
return o;
}
}
// This is a class using both
public class Food {
public static void main(String[] args) {
String filename = "/home/...../Dishes.xml";
String name = "Pizza";
ArrayList<String> ingredients= new ArrayList<String>();
vars.add("dough");
vars.add("cheese");
vars.add("sauce");
Schema s = new Schema();
s.setDishName(name);
s.setIngredient(ingredients);
try { Builder.writeXML(s, filename); }
catch (Exception ex){ System.out.println(ex); }
try {
Schema s2 = Builder.readXML(filename);
System.out.println(s2.getDishName());
System.out.println(s2.getIngredient().toString());
} catch (Exception ex){ System.out.println(ex); }
}
}//Food
Sunday, October 25, 2015
// Java read from a file and return the string
public static String readFile(String fileName){
try (BufferedReader reader = new BufferedReader(new FileReader(fileName))) {
String content = "";
String line = null;
while ((line = reader.readLine()) != null) {
content = content + line;
}
reader.close();
return content;
} catch (IOException x) {
System.err.format("IOException: %s%n", x);
return null;
}
}
public static String readFile(String fileName){
try (BufferedReader reader = new BufferedReader(new FileReader(fileName))) {
String content = "";
String line = null;
while ((line = reader.readLine()) != null) {
content = content + line;
}
reader.close();
return content;
} catch (IOException x) {
System.err.format("IOException: %s%n", x);
return null;
}
}
Thursday, December 13, 2012
Tuesday, June 19, 2012
Google Master Search terms
-- Search on specific site
site: websiteNameHere yourTextHere
-- Related word; Synonyms
~yourTextHere
-- Exact words
"yourWordHere"
-- Exclude term
-yourWordHere
-- Between two dates
2008..2010 yourTextHere
-- File type search
filetype:PDF yourTextHere
-- Show results with the word in title
intitle:yourTextHere
-- * Will be replaced with other words common with the searched term
*yourTextHere
yourTextHere*PartTextHere*OtherPartTextHere
-- Search for papers with specific author
author:AutherNameHere
-- Definition
define: yourWordHere
-- Calculator
2*2
(3/2)*100
-- Unit converter
10 KM in Miles
-- research, use: http://www.jstor.org/
site: websiteNameHere yourTextHere
-- Related word; Synonyms
~yourTextHere
-- Exact words
"yourWordHere"
-- Exclude term
-yourWordHere
-- Between two dates
2008..2010 yourTextHere
-- File type search
filetype:PDF yourTextHere
-- Show results with the word in title
intitle:yourTextHere
-- * Will be replaced with other words common with the searched term
*yourTextHere
yourTextHere*PartTextHere*OtherPartTextHere
-- Search for papers with specific author
author:AutherNameHere
-- Definition
define: yourWordHere
-- Calculator
2*2
(3/2)*100
-- Unit converter
10 KM in Miles
-- research, use: http://www.jstor.org/
Tuesday, June 5, 2012
Floating-point errors. They cannot precisely represent all real numbers. Here is an example.
"Floating-point numbers cannot precisely represent all real numbers, and that floating-point operations cannot precisely represent true arithmetic operations, leads to many surprising situations. This is related to the finite precision with which computers generally represent numbers." (From Wiki)
Read more about my example here.
http://majedline.blogspot.ca/2012/06/floating-point-errors-they-cannot.html
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:
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; |
| } |
| } |
Subscribe to:
Posts (Atom)