Sunday, April 17, 2011

Understanding Streams in Java


Understanding Streams in Java

PART I

By: Mohammed Saudi.

Download examples :

A Data Stream is a flow of data between data source and data sink (the data goes to it).
Data source or sink could be for example disk file, device or network socket.

Two types of streams available in java ; Byte streams and Character streams. They differ in the level they are dealing with data; either data is read/written in 8-bit fashion ( byte stream) or in 16-bit unicode character fashion (character based).

If you are not working with images or sounds, it would be easier and better to use character-based streams; they handle the conversion and encoding stuff.

There is also a helpful classes ( java.io.InputStreamReader and java.io.OutputStreamWriter) that handle the gap between those two types. They convert byte data into character-based.

Also there is a Data stream classes that reads data directly by type (int, string, float ,... ). They relay on a stream objects.

DataInputStream and DataOutputStream. are typical examples.
DataOutputStream(OutputStream out)
DataInputStream(InputStream in)

Examples of Byte Stream classes are
InputStreamReader, OutputStreamWriter, BufferedReader and BufferedWriter.

Examples of character Stream classes ( data sink streams) are
FileReader and FileWriter




===== Reading Files using byte-based streams ====

/*
This Program reads "myInputFile.txt" file byte by byte and write the data into the file "myOutputFile.txt"
you should have myInputFile.txt in the same path of this java file

By: Mohammed Saudi.
www.visiontutoring.net

*/
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class ReadBytes {
public static void main(String[] args) throws IOException {
FileInputStream inStream = null;
FileOutputStream outStream = null;
try {
inStream = new FileInputStream("myInputFile.txt");
outStream = new FileOutputStream("myOutputFile.txt");
int c; //I use int here because I use the read function blow which returns an integer.

while ((c = inStream.read()) != -1) {
outStream.write(c);
}

}

catch (Exception fe){

System.out.println(fe.getMessage());
}

finally {
if (inStream != null) {
inStream.close();
}
if (outStream != null) {
outStream.close();
}
}
}
}

===== Reading Files using character-based streams ====

/*
This Program is the same as "ReadBytes" except it uses filereader and filewritter classes
you should have myInputFile.txt in the same path of this java file

* note: you may have a problem in character encoding when using filewritter class.
If so, use the OutputStreamWriter on a FileOutputStream instead.

By: Mohammed Saudi.
www.visiontutoring.net

*/

import java.io.*;


// Displays contents of a file
//(e.g. java Type app.ini)
public class ReadChars
{
public static void main(
String args[]) throws Exception
{
// Open input/output and setup variables
FileReader fr = new FileReader("myInputFile.txt");
FileWriter fw = new FileWriter("myOutputFile.txt");

char cToReadIn[] = new char[1000];
int read = 0;

// Read (and print) till end of file
while ((read = fr.read(cToReadIn)) != -1)
{
fw.write(cToReadIn);
}


// Close shop
fr.close();
fw.close();
}
}

Download examples :


msaudi.blogspot.com

Wednesday, April 13, 2011

fix firefox youtube fullscreen error in ubuntu

Hello All,
There is a problem in firefox when playing youtube videos in full screen mode. Here is the solution :

sudo mkdir /etc/adobe
sudo echo "OverrideGPUValidation = 1" >> /etc/adobe/mms.cfg

That's it.
msaudi.blogspot.com

Friday, April 8, 2011

Install gcc windows


To install gcc compiler in windows operating system do the following :

1- go to www.mingw.org to download the mingw installer or simply go directly to http://sourceforge.net/projects/mingw/files/

2- click download mingw-get-inst-****
this will download a small exe to help you download and install the required files.
3- click the exe file downloaded and click next

4- keep clicking Next as there is no changes required.

NOTE: You can install the C++ compiler -If you want - by just clicking the check box besides C++ compiler


The installer will do the rest of the job for you. It will download and install the required files.
Then click finish.

///////////////

You will need to add environment variable to the system to be able to use the gcc command at the command prompt.
Follow the next steps to do this :

1- right click on my computer - > Properties -> Advanced - > Environment variables

2- click the PATH variable as indicated then click EDIT
3- Add " ; C:\MinGW\bin to the end of the line "
semicolon followed by the path

4 - Now you are able to compile your c applications using GCC compiler.

** To do this, Click Start -> RUN (or keyboard windows tbutton +R) and write CMD

Compile your code by writing the following command:

gcc c_file_name_path -o c_output_filename


That's it.
msaudi.blogspot.com

Thursday, April 7, 2011

Installing VMware player ubuntu


Hello All,
VMWare offers a free software called VMWare player which you can use to work with virtual machines. of course It has less features than the Work Station version like the ability to make a team and record your WM session but you still can have your work done with the FREE version.

To install it in ubuntu do the following:

first download the software from HERE,
note: you will need to register to be able to download.
then :
Open terminal
Change to the directory you downloaded your file to
issue the following command ,,,
gksudo bash ./VMware-Player-xxxxxxxx
where xxxx is the version details.
This will run a GUI installer, follow the instruction and after you finish, 
you will find it installed in Applications, System Tools , VMware player

Enjoy !.
msaudi.blogspot.com

Monday, April 4, 2011

Configuring Proxy Settings Manually Ubuntu

Steps:

sudo gedit /etc/apt/apt.conf &

then inside apt.conf file write your proxy settings in the following format:
for http:
Acquire::http::Proxy "http://proxy_server_here:port_here";

for ftp: example
Acquire::ftp::Proxy "ftp://10.200.100.100:8080";

If you want to change this, just remove the lines we just wrote.

have fun